-2

So I have two lists:

def function(w,w2): # => this is how I want to define my function (no more inputs than this 2 lists)

I want to know the biggest prefix of w which is also suffix of w2.

How can I do this only with logic (without importing anything)

BhusalC_Bipin
  • 801
  • 3
  • 12
  • Where are you stuck implementing this? – 9769953 Jun 20 '22 at 00:11
  • I can't call the suffix of w2, I've been around and around! – Margarida Mendonça Jun 20 '22 at 00:25
  • There is no *specific* suffix of a string, but you can always do `mystring[-i:]` to get the last `i` characters of `mystring`. – 9769953 Jun 20 '22 at 00:26
  • And how do I know if they are the same size of the prefix o listB? – Margarida Mendonça Jun 20 '22 at 00:33
  • listB? What is listB? You mentioned two strings, w and w2, not a listB. – 9769953 Jun 20 '22 at 00:42
  • Welcome to Stack Overflow. Try to think about the program logically, step by step. Can you write code that tells you the first element in `w`, and the last element in `w2`? Can you write code that checks if they are the same? If they are the different, what does that tell you about the longest prefix? If they are the same, can you set a lower bound on the length of the prefix? Now, can you write code that tells you the first `N` elements in `w`, and the last `N` elements in `w2`, for some specific number `N`? What if you try repeating the comparison, for increasing values of N? – Karl Knechtel Jun 20 '22 at 03:39
  • Can you think of a way to write code in order to repeat the test and make N bigger each time it is tested? If you do this, do you see how that gives the desired answer? – Karl Knechtel Jun 20 '22 at 03:40
  • "I can't call the suffix of w2" `call` is what you do to a function. It does not mean "find out" or "determine" or anything like that. – Karl Knechtel Jun 20 '22 at 03:41

2 Answers2

0

I can try and help get you started on this problem, but it sort of sounds like a homework question so I won't give you a complete answer (per these guidelines).

If I were you I'd start with a small case and build up from there. Lets start with:

w = "ab"
w2 = "ba"

The function for this might look like:

def function(w,w2):
    prefix = ""
    # Does the first letter of w equal the last letter of w2?
    if w[0] == w2[-1]:
        prefix += w[0]
        
    # What about the second letter?
    if w[1] == w2[-2]:
        prefix += w[1]

    return prefix

Then when you run print(function(w,w2)) you get ab.

This code should work for 2 letter words, but what if the words are longer? This is when we would introduce a loop.

def function(w,w2):
    prefix = ""
    
    for i in range(0, len(w)):
        if w[i] == w2[(i+1)*-1]:
            prefix+= w[i]
        else:
            return prefix
            
    return prefix

Hopefully this code will offer a good starting place for you! One issue with what I have written is what if w2 is shorter than w. Then you will get an index error! There are a few ways to solve this, but one way is to make sure that w is always the shorter word. Best of luck, and feel free to DM me if you have other questions.

0

A simple iterative approach could be:

  1. Start from the longest possible prefix (i.e. all of w), and test it against a w2 suffix of the same length.
  2. If they match, you can return it immediately, since it must be the longest possible match.
  3. If they don't match, shorten it by one, and repeat.
  4. If you never find a match, the answer is an empty string.

In code, this looks like:

>>> def function(w, w2):
...     for i in range(len(w), 0, -1):
...         if w[:i] == w2[-i:]:
...             return w[:i]
...     return ''
...
>>> function("asdfasdf", "qwertyasdf")
'asdf'

The slice operator (w[:i] for a prefix of length i, w2[-i:] for a suffix of length i) gracefully handles mismatched lengths by just giving you a shorter string if i is out of the range of the given string (which means they won't match, so the iteration is forced to continue until the lengths do match).

>>> function("aaaaaba", "ba")
'a'
>>> function("a", "abbbaababaa")
'a'
Samwise
  • 68,105
  • 3
  • 30
  • 44