2

This is in reference to problem that is done in Java:
Finding the intersection between two list of string candidates.

I tried to find an equivalent solution in python:

def solution(S):
    length = len(S)
    if length == 0:
        return 1
    count = 0
    i = 0
    while i <= length:
        prefix = S[0:i:]
        suffix = S[length-i:length]
        print suffix
        if prefix == suffix:
            count += 1
        i += 1
    return count

print solution("")
print solution("abbabba")    
print solution("codility")

I know I am not doing the step in terms of suffix.

I am getting the values as 1,4,2 instead of 1,4,0

Community
  • 1
  • 1
paddu
  • 693
  • 1
  • 7
  • 19
  • 2
    Well, that question you linked had a bad wording to begin with. Just look at the number of different "right" answers that have been suggested. It's so bad the question did not have any final answer, simply because it was unclear what the function was supposed to do. I strongly suggest you add some clarification of you have some, otherwise the question will have the same fate as that you linked. – spectras Aug 29 '15 at 21:50
  • What @spectras said. If you're counting the case of an empty string (that is, prefix/suffix length == 0), then "codility" should be at least 1. If you're also counting the case where the prefix length == the input string length, then "codility" should be at least 2 (but if you're not, then "abbabba" should be 3 and not 4). Although your code isn't quite idiomatic, it looks like your logic is sound. I think the problem is just poorly specified. – Kirk Strauser Aug 29 '15 at 22:49

1 Answers1

2

Your current code runs through giving the following prefix, suffix pairs for the second two examples:

a a
ab ba
abb bba
abba abba
abbab babba
abbabb bbabba
abbabba abbabba

c y
co ty
cod ity
codi lity
codil ility
codili dility
codilit odility
codility codility

I presume you are trying to return the number of times the first n characters of a string are the same as the last n. The reason the word codility is returning 2 is because you are starting the index from zero, so it is matching the empty string and the full string. Try instead:

def solution(S):
    length = len(S)
    if length == 0:
        return 1
    count = 0
    i = 1 # Don't test the empty string!
    while i < length: # Don't test the whole string! Use '<'
        prefix = S[:i] # Up to i
        suffix = S[length-i:] # length - i to the end
        print prefix, suffix
        if prefix == suffix:
            count += 1
        i += 1
    return count

print solution("")
print solution("abbabba")    
print solution("codility")

This returns 1, 2, 0.

Perhaps you were looking to test if the prefix is the same as the suffix backwards, and only test half the length of the string? In this case you should try the following:

def solution(S):
    length = len(S)
    if length == 0:
        return 1
    count = 0
    i = 1 # Don't test the empty string!
    while i <= (length + 1)/2: # Test up to halfway
        prefix = S[:i] # Up to i
        suffix = S[:length-i-1:-1] # Reverse string, up to length - i - 1
        print prefix, suffix
        if prefix == suffix:
            count += 1
        i += 1
    return count

print solution("")
print solution("abbabba")    
print solution("codility")

This returns 1, 4, 0.

Siwel
  • 705
  • 10
  • 25
  • Yes, that is correct. This code is to check the palindromes. It makes perfect sense to use the last solution as it does check suffix/prefix backwards. I am not sure about the half length.BTW, thanks for the solutions with descriptions – paddu Aug 29 '15 at 23:21