[英]How to compare the sum of the square of the elements of two arrays?
I'm doing katas (practices) at codewars.com to practice the learning I'm getting in Python.我在 codewars.com 做 katas(练习)来练习我在Python中获得的学习。 This is the kata:
这是卡塔:
Given two integer arrays
a
,b
, both of length>= 1
, create a program that returnsTrue
if the sum of the squares of each element ina
is strictly greater than the sum of the square of each element inb
.给定两个 integer arrays
a
,b
,长度>= 1
,创建一个程序,如果a
中每个元素的平方和严格大于b
中每个元素的平方和,则返回True
。
This is the code that I tried:这是我尝试过的代码:
def array_madness(a,b):
a = sum(i**2 for i in a)
b = sum(i**2 for i in b)
return True if a > b else False
This is the test I need to pass:这是我需要通过的测试:
test.assert_equals(array_madness([4, 5, 6], [1, 2, 3]),True)
test.assert_equals(array_madness( [1, 2, 3],[4, 5, 6]),False)
You copied the problem description wrong.您错误地复制了问题描述。 It's the sum of the squares of
a
, and the sum of the cubes of b
(it's doing a different calculation for the two arrays).它是
a
的平方和和b
的立方和(它对两个数组进行不同的计算)。 The following should work:以下应该有效:
def array_madness(a,b):
return sum(i**2 for i in a) > sum(i**3 for i in b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.