一只名叫tom的猫
递归查找符合规则的元素集合:部分逻辑是建立在认为集合中所有元素都是正数的基础上(()=>{vararr=[0,1,2,3,4,5,6,7,8,9]console.log(calc(arr,3,3))//0,1,2console.log(calc(arr,3,1))//3functioncalc(arr,total,count,feed=[]){if(count===1){//checkreturnarr.includes(total)?feed.concat(total):null}elseif(count>1){//removebignumberarr=arr.filter(item=>item
红糖糍粑
请参考以下python代码实现#-*-coding:utf-8-*-"""author:李毅"""fromunittestimportTestCasedefpermutation(array,nsum):'''假设数组元素不重复。'''#排序(升序)sarray=sorted(array)#找出最大下标max_idx=len(sarray)fori,einenumerate(sarray):ife>nsum:max_idx=ibreak#穷举result=[]foriinrange(max_idx):forjinrange(i,max_idx):forkinrange(j,max_idx):ifi==jandj==k:continueifsarray[i]+sarray[j]+sarray[k]==nsum:result.append((sarray[i],sarray[j],sarray[k]))returnresultclassTest(TestCase):"""单元测试"""deftest_permutation(self):self.assertEqual(permutation(range(10),3),[(0,0,3),(0,1,2)])self.assertEqual(permutation(range(10),2),[(0,0,2),(0,1,1)])#边界值self.assertEqual(permutation(range(3),3),[(0,1,2)])self.assertEqual(permutation(range(1,4),4),[(1,1,2)])