帮我简化这段代码(Python)

发布于 2024-11-28 00:04:46 字数 888 浏览 0 评论 0原文

我是 Python 初学者,在 Google Code University 自学。我将这个问题作为练习,并能够使用如下所示的解决方案来解决它:

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

这会产生正确的输出并解决问题。但是,我重复了是否均匀分割字符串或将奇数添加到前半部分的逻辑,这似乎是多余的。必须有一种更有效的方法来做到这一点。同样的检查和逻辑被应用于 a 和 b。有人吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

何以笙箫默 2024-12-05 00:04:46
def front_back(a, b):
    ad = (len(a) + 1) // 2
    bd = (len(b) + 1) // 2
    return a[:ad] + b[:bd] + a[ad:] + b[bd:]

使用 // 进行除法使得此代码在 Python 2.x 和 3.x 中都可以工作。

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.

习ぎ惯性依靠 2024-12-05 00:04:46

好吧,把它放在一个单独的函数中。

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

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
南…巷孤猫 2024-12-05 00:04:46

因为如果长度是奇数,则要在长度上加 1,而“奇数”意味着 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:]

当然,您甚至可以将其压缩为一行为了好玩(尽管它的可读性明显较差):

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

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:]
旧人哭 2024-12-05 00:04:46

您可以使用 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]

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]
感受沵的脚步 2024-12-05 00:04:46

Mhh试图理解@Sven答案我得到了这个:

len( s ) + 1 / 2 

威尔总是给你正确的索引。

因此,如果我们将其放入一个函数中:

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

我们可以在解决方案中使用它:

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

好的,我现在明白了。

我不太确定 /// 之间有什么区别

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

花伊自在美 2024-12-05 00:04:46
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:]
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:]
爱她像谁 2024-12-05 00:04:46

这是我的:

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')

打印:

abchodela

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文