首页 > 其他分享 >Square(n) Sum

Square(n) Sum

时间:2023-03-13 10:44:59浏览次数:35  
标签:Square return Sum value square numbers sum

Instructions
Complete the square sum function so that it squares each number passed into it and then sums the results together.
For example, for[1, 2, 2] it should return 9 because 1^2 + 2^2 +2^2 = 9
Solution

def square_sum(numbers):
    return sum(value**2 for value in numbers)

使用列表解析使得代码更加简洁

标签:Square,return,Sum,value,square,numbers,sum
From: https://www.cnblogs.com/artwalker/p/17210529.html

相关文章

  • ARC158C All Pair Digit Sums 题解
    题目链接题意设\(f(x)\)表示\(x\)的各位之和。例如\(f(158)=1+5+8=14,f(2023)=2+0+2+3=7,f(1)=1\)等。给定一个正整数序列\(A=(A_1,...,A_N)\),求\(\sum_{i=1}^N......
  • CF1785D Range = √Sum 题解
    题目传送门(第一次CF场切绿欸)题意考虑将这段序列的平均数设为\(4n\),那么总和就会是\(4n^2\),这时候就需要让最值差等于\(2n\),直接让他等于\(3n\)和\(5n\)就可......
  • [vp记录] 2021 Summer Petrozavodsk Camp, Day 3: IQ test (XXII Open Cup, Grand Pri
    2021SummerPetrozavodskCamp,Day3:IQtest(XXIIOpenCup,GrandPrixofIMO)A(性质,转化)发现如果存在\(b\)中存在\(0\),那么直接构造\(b_1,0,b_2,0,\dots......
  • Java Set Summary
    JavaSetSummary一、概要Set6个类名since线程安全elementnull特点Set1.2HashSet1.2NoYes基于HashMap实现TreeSet1.2NoNo基于TreeMa......
  • Clickhouse实现累计求和cumulative sum
    源表数据如下:timeprovinceorder_cnt20200601shandong10020200601jiangsu20020200601zhejiang30020200602shandong20020200602jiangsu30020200602zhejiang40020200603shandon......
  • 209. Minimum Size Subarray Sum
    #题目Givenanarrayofnpositiveintegersandapositiveintegers,findtheminimallengthofacontiguoussubarrayofwhichthesum≥s.Ifthereisn’t......
  • 560. Subarray Sum Equals K
    560.SubarraySumEqualsK标签(空格分隔):leetcodearraymedium题目Givenanarrayofintegersandanintegerk,youneedtofindthetotalnumberofcont......
  • 228. Summary Ranges
    #题目Givenasortedintegerarraywithoutduplicates,returnthesummaryofitsranges.Example1:Input:[0,1,2,4,5,7]Output:[“0->2”,“4->5”,“7”]......
  • golang 升级 1.16.3 之后,编译报错 missing go.sum entry for module providing packag
    问题现象在开发机上升级到了最新golang1.16.3版本,在为一个基于golang1.13的历史项目添加excel依赖包后gogetgithub.com/360EntSecGroup-Skylar/excelize/v2......
  • 容斥定理 AtCoder——FizzBuzz Sum Hard
    题目传送门ProblemStatementFindthesumofintegersbetween 1 and N(inclusive)thatarenotmultiplesof Aor B.Constraints1≤N,A,B≤109 Allvalue......