Python divide odd length string and combine

I'm going through the free Python class Google has available (Link if interested) and I'm stuck on one of the exercises.

Goal: Consider dividing a string into two halves. If the length is even, the front and back halves are the same length. If the length is odd, we'll say that the extra char goes in the front half. eg 'abcde', the front half is 'abc', the back half 'de'. Given 2 strings, a and b, return a string of the form a-front + b-front + a-back + b-back

This is what I have so far:

def front_back(a, b):
    if len(a)%2 == 0 or len(b)%2 == 0:
        firstpart , secondpart = a[:len(a)/2] , a[len(a)/2:]
        thirdpart , fourthpart = b[:len(b)/2] , b[len(b)/2:]
    else:
        firstpart , secondpart = a[:len(a)+1/2] , a[len(a)+1/2:]
        thirdpart , fourthpart = b[:len(b)+1/2] , b[len(b)+1/2:]
    return firstpart+thirdpart+secondpart+fourthpart

These are the test strings:

 test(front_back('abcd', 'xy'), 'abxcdy')
 test(front_back('abcde', 'xyz'), 'abcxydez')
 test(front_back('Kitten', 'Donut'), 'KitDontenut')

The third string in each is the expected string.

What am I doing wrong? I know that the issue is not being able to account for odd-length strings, but I have been searching for a few days now and I've had no luck finding an answer online. I'm also open to more efficient solutions, but I want to know why my particular setup is not working out, for future reference.

Thanks!


Simpler method:

def front_back(a, b):
    hlena, hlenb = (len(a) + 1)/2, (len(b) + 1)/2
    return a[:hlena] + b[:hlenb] + a[hlena:] + b[hlenb:]

No need for if clause to handle empty string case. Order of operations for slice calculation was not correct in your code.

Tip: Defore diving in and coding, play with the problem in the REPL shell until you have a succinct representation of the problem. One of the biggest challenges that beginners have is the code they create is much larger than needed, making it harder to understand and debug. Start with the smallest workable chuck and build from there.


Your problem is that you're not treating a and b as separate cases. Consider the case where a has length 4 and b has length 5. In that case you would always take the first branch, which would treat b incorrectly.

def front_back(a, b):
    if len(a)%2 == 0:
        firstpart , secondpart = a[:len(a)/2] , a[len(a)/2:]
    else:
        firstpart , secondpart = a[:len(a)+1/2] , a[len(a)+1/2:]

    if len(b)%2 == 0:
        thirdpart , fourthpart = b[:len(b)+1/2] , b[len(b)+1/2:]
    else
        thirdpart , fourthpart = b[:len(b)/2] , b[len(b)/2:]

    return firstpart+thirdpart+secondpart+fourthpart

You should split the implementation into a splitting function and a merging. Easier to test that way. Also, the way the split is defined, you can simplify the first function quite a bit:

If the length of the string s is even, len(s)//2 in integer division is the same as (len(s)+1)//2 . If it is odd, len(s)//2 is one less than (len(s)+1)//2 . Since you need the longer string as first part, the splitting function can be written as:

def splitter(s):
    mid = (len(s)+1)//2
    return s[:mid], s[mid:]

Then for the merging:

def front_back(a,b):
    a_front, a_back = splitter(a)
    b_front, b_back = splitter(b)
    return "".join((a_front, b_front, a_back, b_back))
链接地址: http://www.djcxy.com/p/26782.html

上一篇: Python的“超级”如何做正确的事情?

下一篇: Python分长度字符串和结合