[英]How to compare the sum of the square of the elements of two arrays?
我在 codewars.com 做 katas(練習)來練習我在Python中獲得的學習。 這是卡塔:
給定兩個 integer arrays
a
,b
,長度>= 1
,創建一個程序,如果a
中每個元素的平方和嚴格大於b
中每個元素的平方和,則返回True
。
這是我嘗試過的代碼:
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
這是我需要通過的測試:
test.assert_equals(array_madness([4, 5, 6], [1, 2, 3]),True)
test.assert_equals(array_madness( [1, 2, 3],[4, 5, 6]),False)
您錯誤地復制了問題描述。 它是a
的平方和和b
的立方和(它對兩個數組進行不同的計算)。 以下應該有效:
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.