1

I'm a beginner in Python, teaching myself off of Google Code University. I had this problem as an exercise, and was able to solve it using the solution shown below:

# F. front_back
# 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.
# e.g. '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
def front_back(a, b):
  if len(a) % 2 == 0:
    ad = len(a) / 2
    if len(b) % 2 == 0:
      bd = len(b) / 2
    else:
      bd = (len(b) / 2) + 1
  else:
    ad = (len(a) / 2) + 1
    if len(b) % 2 == 0: 
      bd = len(b) / 2
    else:
      bd = (len(b) / 2) + 1

  return a[:ad] + b[:bd] + a[ad:] + b[bd:]

This produces the correct output and solves the problem. However, I am duplicating the logic of whether to split a string evenly or add the odd number to the first half, and this seems redundant. There has to be a more efficient way of doing this. The same exact check and logic is being applied to a and b. Anyone?

7 Answers 7

12
def front_back(a, b):
    ad = (len(a) + 1) // 2
    bd = (len(b) + 1) // 2
    return a[:ad] + b[:bd] + a[ad:] + b[bd:]

Using // for division makes this code work in both Python 2.x and 3.x.

4
  • that's pretty, pretty much what I came up with
    – nflacco
    Aug 4, 2011 at 19:22
  • @Sven, can you explain us how does this works? ( particularly the // operator )
    – OscarRyz
    Aug 4, 2011 at 19:50
  • 1
    @OscarRyz: a // b is the integer division of a / b -- it rounds down.
    – hughdbrown
    Aug 4, 2011 at 20:21
  • 1
    If len(x) is even, then len(x)+1) // 2 is the same number. If len(x) is odd, then (len(x)+1) // 2 is 1 more, as the code requires. So the same expression can be used for odd and even numbers.
    – hughdbrown
    Aug 4, 2011 at 20:45
4

Well, put it in a separate function.

def front_back(string):
    offset = len(string) / 2
    if len(string) % 2 != 0:
        offset += 1
    return string[:offset], string[offset:]

def solution(a, b):
    front_a, back_a = front_back(a)
    front_b, back_b = front_back(b)
    return front_a + back_a + front_b + back_b
3

Since you're adding 1 to the length if it's odd, and 'odd' means that len(a)%2 == 1...

def front_back2(a, b):
    ad = (len(a) + len(a)%2) / 2
    bd = (len(b) + len(b)%2) / 2
    return a[:ad]+b[:bd]+a[ad:]+b[bd:]

Of course, you could even condense it to one line just for kicks (although, it's significantly less readable):

def front_back2(a, b):
    return a[:(len(a)+len(a)%2)/2]+b[:(len(b)+len(b)%2)/2]+a[(len(a)+len(a)%2)/2:]+b[(len(b)+len(b)%2)/2:]
2

You can get the maximum index by using ceil

In [1]: l = [1,2,3]
In [2]: import math
In [4]: math.ceil(len(l)/2.0)
Out[4]: 2.0
In [5]: l.append(4)
In [6]: math.ceil(len(l)/2.0)
Out[6]: 2.0
In [7]: l.append(5)
In [8]: math.ceil(len(l)/2.0)
Out[8]: 3.0
In [9]: l[0:3]
Out[9]: [1, 2, 3]
In [10]: l[3:]
Out[10]: [4, 5]
2

Mhh trying to understand @Sven answer I got this:

len( s ) + 1 / 2 

Will always give you the correct index.

So if we put that in a function:

def d( s ):
   return ( len(s) + 1 ) / 2

We can use it in the solution:

def front_back( a, b ): 
    return a[:d(a)] + b[:d(b)] + a[d(a):] + b[d(b):]

Ok, I got it now.

I'm not quite sure what's the difference between / and // though

2
  • That makes sense. I also still don't understand the difference between / and //
    – Dan
    Aug 4, 2011 at 20:31
  • I commented on the difference between / and // below my answer. Aug 7, 2011 at 9:23
1
from math import ceil

def front_back(a, b):
    divide = lambda s: int(ceil(len(s) / 2.0)) # or lambda s: (len(s) + 1) // 2
    a_divide, b_divide = divide(a), divide(b)
    return a[:a_divide] + b[:b_divide] + a[a_divide:] + b[b_divide:]
1

Here's mine:

def front_back( a, b ) :
    return of(a)[0] + of(b)[0] + of(a)[1] + of(b)[1]

def of( s ):
   index = len( s ) / 2 + ( 1 if len( s ) % 2 == 1 else 0 )
   return ( s[ : index ] , s[ index : ] )


print front_back('abcde','hola')

Prints:

abchodela

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.