本文介绍了清理挑战答案的提示?加权数字和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在经历一些代码挑战.其中一个让我印象特别深刻.CodeAbbey 有一个加权数字总和挑战.这就是我的答案.
# 打开并读取文件、硬编码数字等.这是一个示例原始数据 = (6, 19, 64, 6527226, 12345146)对于 raw_data 中的数字:wsd = 0 # 存储总和number_list = list(str(number)) # 将数字转换为列表.for i, k in enumerate(number_list): # 枚举每个数字wsd += (int(i+1) * int(k)) # 乘积并将乘积添加到 wsd打印(WSD)输出 >>>6、19、14、114、137
任何有更多经验的人都能看到更好的求和方式?
解决方案
如果使用地图:
>>>map(lambda d: sum([ (i+1) * int(v) for i,v in enumerate(list(str(d)))]), raw_data)[6, 19, 14, 114, 137]I've been going through some code challenges. And one struck me in particular. CodeAbbey has a Weighted Sum of Digits challenge. And this was my answer.
# open and read a file, hardcoded numbers, etc. Here is a sample
raw_data = (6, 19, 64, 6527226, 12345146)
for number in raw_data:
wsd = 0 # Stores sum
number_list = list(str(number)) # Converts the number into a list.
for i, k in enumerate(number_list): # Enumerates each number
wsd += (int(i+1) * int(k)) # Multiplies and adds product to wsd
print(wsd)
output >>> 6, 19, 14, 114, 137
Anyone with more experience able to see a better way of getting the sum?
解决方案
If using map:
>>> map(lambda d: sum([ (i+1) * int(v) for i,v in enumerate(list(str(d)))]), raw_data)
[6, 19, 14, 114, 137]
这篇关于清理挑战答案的提示?加权数字和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!