开发者

Help me simplify this code (Python)

开发者 https://www.devze.com 2023-03-25 03:45 出处:网络
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:

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. 'abcd开发者_运维百科e', 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?


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.


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


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:]


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]


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


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:]


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
0

精彩评论

暂无评论...
验证码 换一张
取 消