-2

I'm confused as to how the words relate to each other, and how that number is calculated in the algorithm. Here's the full problem (I'm supposed to use for loops primarily. I think I'm supposed to avoid built-in functions.)

" For this task, we are providing you several examples of how this program should behave. We are not going to tell you exactly what the program code should contain: but your program should work as follows:

The user enters two strings (on two separate lines) "dizzy" and "zygote". Your program prints out: 2.

The user enters two strings (on two separate lines) "essentialness" and "essence". Your program prints out: 3.

The user enters two strings (on two separate lines) "platypus" and "saltwater crocodile". Your program prints out: 1.

The user enters two strings (on two separate lines) "bar" and "battery". Your program prints out: 0.

The above are examples: your program should behave similarly for any string input. Here are some more example runs:

RESTART: type something, then press return/enter: definite now type something else, then press return/enter again: finite the calculated output is 6

RESTART: type something, then press return/enter: make now type something else, then press return/enter again: matt the calculated output is 0

RESTART: type something, then press return/enter: bassline now type something else, then press return/enter again: linebass the calculated output is 4

RESTART: type something, then press return/enter: cringing now type something else, then press return/enter again: ingenious the calculated output is 3

RESTART: type something, then press return/enter: banana now type something else, then press return/enter again: nanaimo the calculated output is 4

RESTART: type something, then press return/enter: superfluous now type something else, then press return/enter again: tossup the calculated output is 0

"

Here's what I tried (it's not complete + doesn't give the answer):

word1 = input("Please enter a word! ")
word2 = input("Please enter another word! ")
count = 0
backword1 = word1[::-1] 

len1 = int(len(word1))

len2 = int(len(word2))

for i in word1:
   word1[:-1]
   for i in word2:
      word2[1:len2]
         if word1[

print(count)

  • (1) I have to admit that the first "riddle" is annoying at best. (2) It looks like the the function needs to output the the number of character that are in common between the end of the last string and the beginning of the second string. (3) As you said the program will not work, but can you provide some rationale behind the code you wrote? – norok2 Sep 24 '17 at 19:22
  • First it asks the user to input 2 different words. Then I put a variable that makes the first word backwards, so that it could be analyzed from back to front while the other one is analyzed from front to back (I'm guessing that's what's supposed to happen?). Then it finds the lengths. I wanted the for loop to compare the two words at each individual character, but then I got super confused... – Jack Ritter Sep 25 '17 at 13:33
  • the underlying idea seems valid, but for example I do not quite get why you need the first word to be backward and what is the rationale behind some lines like `word1[:-1]` as they have no effect. – norok2 Sep 26 '17 at 05:45

1 Answers1

1

I would do it like this:

def count_back_front(
        text1,
        text2):
    """
    Count the common chars backward for 1st and forward for 2nd strings.

    Args:
        text1 (str): The first input.
        text2 (str): The second input.

    Returns:
        result (int): The number of common characters.

    Examples:
        >>> count_back_front('definite', 'finite')
        6
        >>> count_back_front('make', 'matt')
        0
        >>> count_back_front('bassline', 'linebass')
        4
        >>> count_back_front('cringing', 'ingenious')
        3
        >>> count_back_front('banana', 'nanaimo')
        4
        >>> count_back_front('superfluous', 'tossup')
        0
    """
    for i in range(min(len(text1), len(text2)), -1, -1):
        # : uncomment the following line to see how it works
        # print(i, text1[-i:], text2[:i])
        if text1[-i:] == text2[:i]:
            break
    return i


# get the input data
text1 = input('Please enter first word:  ')
text2 = input('Please enter second word: ')

# calculate the "riddle" number
count = count_back_front(text1, text2)
print('Common chars back/front: {}'.format(count))

The idea is to:

  • define an index i which start from the maximum length of the two common string and ends at 0 (in range the stop extreme is excluded, hence you have to use -1 as second argument) in -1 steps.
  • check if the last i characters of the first string match the first i characters of the second string: if they do, we can exit the loop
  • the value of i at the end of the loop will tell us how chars are in common.

Notes:

  • I used a function to better separate the code actually doing the computation from the input/output part.
  • I provided you with a decent docstring (enclosed in """) in Google style (it is good practice to always have one).
  • The code in the Examples section can be tested automatically.
norok2
  • 18,523
  • 3
  • 47
  • 78