SUMMER OF '69:返回数组中数字的总和,除了 忽略以6开头并扩展到下一个数字的部分 9(每6个后接至少9个)。返回0,否 数字。
summer_69([1、3、5])-> 9
summer_69([4,5,6,7,8,9])-> 9
summer_69([2,1,6,9,11])-> 14
def summer_69(arr):
if 6 in arr and 9 in arr:
return sum(arr[:arr.index(6)])+sum(arr[arr.index(9):])
else:
total=0
for x in arr:
total+=x
return total
我希望输出summer_69([4,5,6,7,8,8,9])-> 9,但是 实际输出是18。
我希望输出summer_69([2,1,6, 9,11])-> 14,但实际输出是23
答案 0 :(得分:0)
由于字符串转换,因此不确定这是否是一种好方法。
import re
def summer_69(arr):
if 6 in arr and 9 in arr:
#Using Regex to remove everything between 6 & 9 and convert object back to list of ints
arr = [int(i) for i in re.sub(r"(6.*?9)", "", "_".join(map(str, arr))).split("_") if i]
return sum(arr)
else:
return sum(arr)
print(summer_69([1, 3, 5])) #--> 9
print(summer_69([4, 5, 6, 7, 8, 9])) #--> 9
print(summer_69([2, 1, 6, 9, 11])) #--> 14
答案 1 :(得分:0)
您可以通过删除6到9之间的所有项目(对于所有间隔)来做到这一点,并只对列表中剩余的元素求和。
要删除6到9之间的零件,可以使用slice() Function
slice() function
返回一个切片对象。切片对象用于指定如何切片序列。您可以 指定切片的开始位置和结束位置。
def summer_69(arr):
while 6 in arr: # while 6 in array
idx6 = arr.index(6)
idx9 = arr.index(9)
del arr[idx6:idx9+1] # delete part of array between index of 6 and index of 9
print (arr) # [4, 5, 1, 1, 4, 3]
return sum(arr)
print (summer_69([4, 5, 6, 7, 8, 9,1,1,6,2,3,9,4,3]))
输出:
18
例如。
print (summer_69([1, 3, 5])) # 9
print (summer_69([4, 5, 6, 7, 8, 9])) # 9
print (summer_69([2, 1, 6, 9, 11])) # 14
答案 2 :(得分:0)
我是编程的新手,我针对该问题的解决方案(考虑了“所有”可能的变化)如下:
def summer_69(*arr):
a=arr
sum_a=0
sum_a6=0
sum_a69=0
sum_a96=0
sum_a9=0
sum_each=[]
print("given array: {}" .format(a))
if 6 not in a:
sum_a=sum((a))
sum_each.append(sum_a)
print("\nif 6 not in a:{} " .format(a))
print("sum_a={}" .format(sum_a))
else:
while len(a)>0:
if 6 in a and 9 in a:
if a.index(6)<a.index(9):
a69=a[:a.index(6)]
sum_a69=sum((a69))
a69_pri=a[a.index(9):]
a=a69_pri
sum_each.append(sum_a69)
print("\nif a.index(6)<a.index(9): {}" .format(a69))
print("sum_a69={}" .format(sum_a69))
continue
elif a.index(9)<a.index(6):
a96=a[a.index(9)+1:a.index(6)]
sum_a96=sum((a96))
a96_pri=a[a.index(6):]
a=a96_pri
sum_each.append(sum_a96)
print("\nif a.index(9)<a.index(6): {}" .format(a96))
print("sum_a96={}" .format(sum_a96))
continue
elif 6 in a and 9 not in a:
a6=a[:a.index(6)]
sum_a6=sum((a6))
a=a6
sum_each.append(sum_a6)
print("\nelif 6 in a and 9 not in a: {}" .format(a6))
print("sum_a6={}" .format(sum_a6))
break
elif 6 not in a and 9 in a:
a9=a[a.index(9)+1:]
sum_a=sum((a9))
a=a9
sum_each.append(sum_a)
print("\nelif 6 not in a and 9 in a: {}" .format(a9))
print("sum_a9={}" .format(sum_a9))
break
sum_all=sum((sum_each))
print(sum_each)
print("sum_all={}" .format(sum_all))
return sum_all
答案 3 :(得分:0)
查看我简单而直接的运行代码
def summer_69(arr):
result=0
if 6 not in arr:
result=result+sum(arr)
return result
elif len(arr)==0:
return 0
else:
return sum(arr)-sum(arr[arr.index(6):arr.index(9)+1])
答案 4 :(得分:0)
def summer_69(arr):
total = 0
add = True
for num in arr:
while add:
if num != 6:
total += num
break
else:
add = False
while not add:
if num != 9:
break
else:
add = True
break
return total
答案 5 :(得分:0)
def summer_69(arr):
if 6 and 9 in arr:
c=sum(arr[arr.index(6):arr.index(9)+1)
return sum(arr)-c
else:
return sum(arr)
print(summer_69([4,5,6,7,8,9]))