以特定方式将元素追加到列表中



我有一个任务,我必须得到一个特定的列表。任务是我得到一个字符串的输入,它只包含0-9的数字。现在,我们必须以特定的方式附加这些元素。我们需要切断字符串,以便组只有 2 到 4 位数字长。仅当必须时,组才应以 0 开头。每个字符串的长度为 2 到 30 位。

我已经设法获得了组不以 0 开头的列表,但我的组太大了。这就是我遇到麻烦的地方。如何管理此长度仅为 2 到 4 位数字的组。

输入和输出的一些示例:

输入:01365400606我的输出:[0, 1, ' ', 3, 6, ' ', 5, 4, ' ', 0, 0, 6, 0, 6]期望输出:[0, 1, ' ', 3, 6, ' ', 5, 4, 0, 0, ' ', 6, 0, 6]

组必须以 0 开头的示例,因为出现 0 的次数超过四次。

输入:011000000011000100111111101011期望输出:[0, 1, " ", 1, 0, 0, 0, " ", 0, 0, 0, 0, " ", 1, 1, 0, " ", 0, 0, " ", 1, 0, 0, 1, " ", 1, 1, 1, 1, " ", 1, 1, 0, " ", 1, 0, 1, 1]

每个字符串都有更正确的解决方案,即01365400606,可以采用多种方式切割:

  1. 0136 5400 606
  2. 01 36 5400 606

我的代码:

def NumberCutter(number):
count = 0
numberList = []
numberListSolution = []
for e in number:
e = int(e)
numberList.append(e)       
for e in numberList:
count += 1
numberListSolution.append(e)
if count % 2 == 0 and e != 0:
numberListSolution.append(" ")
return numberListSolution

试试这个:

def NumberCutter(number):
count = 0
# use list comprehensive more readable than for loop
numberList = [int(e) for e in number]
numberListSolution = []
def break_group():
""" add space and return 0 to reset the count."""
numberListSolution.append(' ')
return 0
# decision depends on the elements around current element we must know the index
for i, e in enumerate(numberList):
count += 1
numberListSolution.append(e)
if i == len(numberList) - 1:
continue  # last element no need to add a space after it
if count == 2:  # check for good decision when the count is two
# if you want to short the group that start with 0 to the minimum uncomment this
# but as you said priority to group length
# try:
#     # 0e1AA  == [0e 1AA] better than [0e1 AA]
#     if not numberListSolution[-2] and numberList[i+1] and len(numberList) - i >= 2:
#         numberListSolution.append(" ")
#         count = 0
# except IndexError:
#     pass
try:
# Pe100 -->  Pe 100
if numberList[i+1] and not numberList[i+2] and not numberList[i+3] and len(numberList) - (i + 1) == 3:
count = break_group()
continue
except IndexError:
pass
try:
# Pe101 -->  Pe 101
if numberList[i+1] and not numberList[i+2] and numberList[i+3] and len(numberList) - (i + 1) == 3:
count = break_group()
except IndexError:
pass
if count == 3:  # check for good decision when the count is three
# if you want to short the group that start with 0 to the minimum uncomment this
# but as you said priority to group length
# try:
#     # 0e1AA  == [0e 1AA] better than [0e1 AA]
#     if not numberListSolution[-3] and numberList[i+1] and len(numberList) - i >= 2:
#         numberListSolution.append(" ")
#         count = 0
#         continue
# except IndexError:
#     pass
try:
# PPeA1A --> PPeA 1A
if numberList[i+2] and (len(numberList) - (i + 1) >= 2):
# priority to group length
continue
except IndexError:
pass
try:
# PP0111  PPe 111
if not e and not numberList[i+1] and not numberList[i+2] and numberList[i+3]:
count = break_group()
continue
except IndexError:
pass
try:
# PPe1A...  PPE 1A....  at least there is two element after e and the first element is not zero
# PPeAA] force PPE AA  there is exactly two element force the break
if numberList[i + 1] and (len(numberList) - (i + 1) >= 2) or (len(numberList) - (i + 1) == 2):
count = break_group()
continue
except IndexError:
pass
# we have 4 element force the group
if count == 4:
count = break_group()
continue
return numberListSolution
print(NumberCutter('011000000011000100111111101011'))
# [0, 1, 1, 0, ' ', 0, 0, 0, 0, ' ', 0, 0, 1, ' ', 1, 0, 0, 0, ' ', 1, 0, 0, 1, ' ', 1, 1, 1, 1, ' ', 1, 1, 0, ' ', 1, 0, 1, 1]
print(NumberCutter('01365400606'))
# [0, 1, 3, 6, ' ', 5, 4, 0, 0, ' ', 6, 0, 6]

注意:在评论中,我用特殊字母解释了特殊情况以做出正确的决定:

  • P是以前的数字>=0
  • e是当前元素
  • A是 e>=0之后的数字
  • previous number (P)Next number (A)Zero时,我使用1: 就像1Pe1A1一样,我用0表示相反的情况0PeA0AA
  • e0我使用 0 时,就像P0A

最新更新