- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Python 中查找两个列表/数组的总和。
例如:
你得到两个随机整数列表,分别是 lst1
和 lst2
,大小分别为 n
和 m
。这两个列表都包含从 0 到 9
的数字(即,每个索引都存在一位整数)。
这里的想法是将每个列表表示为数字 N 和 M 本身的整数。
您需要找到两个输入列表的总和,将它们视为两个整数并将结果放入另一个列表中,即输出列表也将在每个索引处仅包含单个数字。
以下是我尝试过的代码:
def list_sum(lst1, n, lst2, m) :
i, j, sum, carry = 0, 0, 0, 0
new_lst = []
if n == 0 and m == 0:
new_lst.append(0)
elif n > 0 and m>0:
while n > 0 and m > 0:
sum = lst1[n - 1] + lst2[m - 1] + carry
if sum >= 10:
carry = 1
else:
carry = 0
new_lst.append(sum % 10)
n -= 1
m -= 1
while n > 0:
if (lst1[n-1] + carry) >= 10:
new_lst.append((lst1[n-1] + carry) % 10)
carry = 1
else:
new_lst.append(lst1[n-1])
carry = 0
n -= 1
while m > 0:
if (lst2[m-1] + carry) >= 10:
new_lst.append((lst2[m-1] + carry) % 10)
carry = 1
else:
new_lst.append(lst1[m-1])
carry = 0
m -= 1
if carry == 1:
new_lst.append(1)
new_lst.reverse()
elif n == 0 and m > 0:
new_lst.append(0)
new_lst = new_lst + lst2
elif n > 0 and m == 0:
new_lst.append(0)
new_lst = new_lst + lst1
print(new_lst)
但是我觉得我在这里遗漏了一些东西,这并没有给我正确的组合答案。有时它错误列出了索引错误。我不知道为什么。
示例输入:
n = 3
lst1 = [6, 9, 8]
m = 3
lst2 = [5, 9, 2]
输出:
[1, 2, 9, 0]
在这里,每个元素被求和,然后如果 sum >=10
则我们得到一个 carry = 1
并将与下一个总和相加。
即
1. 8+2= 10 >=10 hence carry=1 in first sum
2. 9+9+1( carry) = 19 >=10 hence carry=1
3. 6+5+1( carry) = 12>=10 hence carry=1
4. upend the carry to next position as 1
Hence resultant list would be [1, 2, 9, 0]
接下来我可以尝试什么?
最佳答案
嗯,所有其他答案都非常适合添加 2 个数字(数字列表)。
但是如果你想创建一个可以处理任意数量的“数字”的程序
,
Here's what you can do...
def addNums(lst1, lst2, *args):
numsIters = [iter(num[::-1]) for num in [lst1, lst2] + list(args)] # make the iterators for each list
carry, final = 0, [] # Initially carry is 0, 'final' will store the result
while True:
nums = [next(num, None) for num in numsIters] # for every num in numIters, get the next element if exists, else None
if all(nxt is None for nxt in nums): break # If all numIters returned None, it means all numbers have exhausted, hence break from the loop
nums = [(0 if num is None else num) for num in nums] # Convert all 'None' to '0'
digit = sum(nums) + carry # Sum up all digits and carry
final.append(digit % 10) # Insert the 'ones' digit of result into final list
carry = digit // 10 # get the 'tens' digit and update it to carry
if carry: final.append(carry) # If carry is non-zero, insert it
return final[::-1] # return the fully generated final list
print(addNums([6, 9, 8], [5, 9, 2])) # [1, 2, 9, 0]
print(addNums([7, 6, 9, 8, 8], [5, 9, 2], [3, 5, 1, 7, 4])) # [1, 1, 2, 7, 5, 4]
希望这是有道理的!
关于python - 求两个数组的和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70629597/
for (i = 0; i <= 1000; i++) { if ( i % 3 === 0){ console.log(i); } if ( i % 5 ==
对于一项作业,我需要解决一个数学问题。我将其缩小为以下内容: 令 A[1, ... ,n] 为 n 整数数组。 令y 为整数常量。 现在,我必须编写一个算法,在 O(n) 时间内找到 M(y) 的最小
我可以使用 iOS MediaPlayer 并通过这种方式播放电影。但我需要,寻找一秒钟的电影。我该怎么做,我像这样通过 MediaPlayer 播放电影: NSURL *videoURL =
我听说过 eCos看起来作为一个爱好项目来玩会很有趣。 任何人都可以推荐一个价格合理的开发板。如果它不会增加太多成本,我想要几个按钮来按下(并以编程方式检测按下)和一些调试输出的 LCD。以太网会很好
给定 a 到 b 的范围和数字 k ,找到 a 到 b [包括两者]之间的所有 k-素数。 k-素数的定义:如果一个数恰好有 k 个不同的素数因子,则该数是 k-素数。 即 a=4 , b=10 k=
这是对 my previous question 的重新措辞尝试作为它收到的反馈的结果。 我想要一个简单的网络通信,我可以将其用作底层框架,而无需再次查看。我只想将一个字符串从一台 PC 推送到另一台
我有许多节点通过其他类型的中间节点连接。如图所示,中间节点可以有多个。我需要找到给定数量的节点的所有中间节点,并按初始节点之间的链接数量对其进行排序。在我的示例中,给定 A、B、C、D,它应该返回节点
我的代码遇到问题。我试图找到这个 5x5 数组的总和,但它总是给我总计 0。当我使用 2x2 数组时,它可以工作,但对于 5x5 数组则不起作用。有人可以帮忙吗? import java.util.*
我们有一个给定的数组,我们想要打印 BST 中每个节点的级别。 例如,如果给定数组为:{15, 6, 2, 10, 9, 7, 13} 那么答案是: 1 2 3 3 4 5 4 (表示存储15的节点级
我对 R 和编程非常陌生,所以请留在我身边:) 我正在尝试使用迭代来查找无限迭代到小数点后第四位的值。 IE。其中小数点后第四位不变。所以 1.4223,其中 3 不再改变,所以小数点后 3 位的结果
我的问题与 Fastest way of computing the power that a "power of 2" number used? 非常相似: 将 x=2^y 作为输入,我想输出 y。
如何找到三个非零数字中最小的一个。 我尝试引入一个非常小的数字eps = 1e-6(我的数字为零或明显大于eps)并在min(x,eps)、min(y,eps)之间进行测试)等我什么也没得到。有没有办
我有一个类(class),他们计算矩阵中最大的“1”岛,但他的岛概念是“如果两个单元在水平、垂直或对角线上彼此相邻,则称它们是相连的。 “ 我需要帮助来删除对角台阶。 class GFG {
我开始使用 IDE Jupyter && Python 3.6 并出现了一个问题。我必须通过IDE绘制Petersen子图中的哈密顿路径,但我不知道该怎么做。 我显示有关该图的信息: Petersen
public static void main(String[] args) { int sum = 2; int isPrime; for(int x = 3; x Mat
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How much time should it take to find the sum of all prime
我想找到给定节点到链表二叉搜索树中根的距离。我有下面的代码来计算树的高度(root.getHeightN()),从根到叶子,但我现在需要的是从叶子到根。 public int getHeightN()
是否有一种优雅的方法使用预先计算的 KDTree 来查找连接组件的数量?现在使用呼吸优先搜索算法以及 k 最近邻的 KDTree 给出的邻接矩阵来查找连接的组件,但是是否有更好的可能性? import
我有一个要求,我需要找到具有相同名称的不同对象中 amt 值的总和。下面是代码片段 traveler = [ { description: 'Senior', Amount: 50}, {
我正在尝试使用 pandas 对某些列进行求和,同时保留其他列。例如: member_no, data_1, data_2, data_3, dat_1, dat_2, other_1, other_
我是一名优秀的程序员,十分优秀!