我有一个像这样的Python列表,
arr = [110, 60, 30, 10, 5]
Run Code Online (Sandbox Code Playgroud)
我需要做的实际上是找到每个数字与所有其他数字的差异,然后找到所有这些差异的平均值。
因此,对于这种情况,它将首先找到 之间的差异110,然后找到所有剩余元素 ie 之间的差异,然后它将找到与剩余元素 ie等之间60, 30, 10, 5的差异。6030, 10, 5
之后,它将计算所有这些差异的平均值。
现在,这可以通过两个 For 循环轻松完成,但O(n^2)时间复杂度较高,而且代码也有点“混乱”。我想知道是否有更快、更有效的方法来做同样的事情?
我需要一种算法的帮助,该算法可以有效地将人们分组,并确保以前的配对不会重复。
例如,假设我们有 10 位候选人;
candidates = [0,1,2,3,4,5,6,7,8,9]
并假设我们有一个先前匹配的字典,使得每个键值对 iecandidate:matches代表一个候选者和迄今为止与它们配对的候选者数组;
prev_matches = {0: [6, 5, 1, 2], 1: [4, 9, 0, 7], 2: [9, 8, 6, 0], 3: [5, 4, 8, 9], 4: [1, 3, 9, 6], 5: [3, 0, 7, 8], 6: [0, 7, 2, 4], 7: [8, 6, 5, 1], 8: [7, 2, 3, 5], 9: [2, 1, 4, 3]}
因此,对于Candidate 0,它们首先与 、 、 和 配对,在随后的配对轮中,它们与、、 和Candidate 6配对。字典中的其他键值对也是如此。Candidate 5Candidate 1Candidate …
我想将函数列表fs = [ f, g, h ]按顺序应用于字符串text=' abCdEf '
就像是f( g( h( text) ) )。
这可以通过以下代码轻松完成:
# initial text
text = ' abCDef '
# list of functions to apply sequentially
fs = [str.rstrip, str.lstrip, str.lower]
for f in fs:
text = f(text)
# expected result is 'abcdef' with spaces stripped, and all lowercase
print(text)
Run Code Online (Sandbox Code Playgroud)
似乎functools.reduce应该在这里完成工作,因为它在每次迭代时“消耗”函数列表。
from functools import reduce
# I know `reduce` requires two arguments, but I don't …Run Code Online (Sandbox Code Playgroud) 我有 sympy 的问题,我需要求解两个方程组:
B1=2.51/(Re(f^(0.5)))
f=(1/(-2*log10((epsilon/D/3.7)+B1)))^2
Run Code Online (Sandbox Code Playgroud)
math.log10我尝试使用 sympy、numpy 和 fsolve,但是使用返回浮点数的 时出现问题:
B1=2.51/(Re(f^(0.5)))
f=(1/(-2*log10((epsilon/D/3.7)+B1)))^2
Run Code Online (Sandbox Code Playgroud)
返回:
TypeError: can't convert expression to float
Run Code Online (Sandbox Code Playgroud) 给定一个 1024 位模和几个长整数,我想找出其中哪个值是二次余数。
Legendre 符号应该返回 -1、0 或 1,但我的代码返回的值比这些值大几个数量级。
p = 101524035174539890485408575671085261788758965189060164484385690801466167356667036677932998889725476582421738788500738738503134356158197247473850273565349249573867251280253564698939768700489401960767007716413932851838937641880157263936985954881657889497583485535527613578457628399173971810541670838543309159139
ints = [25081841204695904475894082974192007718642931811040324543182130088804239047149283334700530600468528298920930150221871666297194395061462592781551275161695411167049544771049769000895119729307495913024360169904315078028798025169985966732789207320203861858234048872508633514498384390497048416012928086480326832803, 45471765180330439060504647480621449634904192839383897212809808339619841633826534856109999027962620381874878086991125854247108359699799913776917227058286090426484548349388138935504299609200377899052716663351188664096302672712078508601311725863678223874157861163196340391008634419348573975841578359355931590555, 17364140182001694956465593533200623738590196990236340894554145562517924989208719245429557645254953527658049246737589538280332010533027062477684237933221198639948938784244510469138826808187365678322547992099715229218615475923754896960363138890331502811292427146595752813297603265829581292183917027983351121325, 14388109104985808487337749876058284426747816961971581447380608277949200244660381570568531129775053684256071819837294436069133592772543582735985855506250660938574234958754211349215293281645205354069970790155237033436065434572020652955666855773232074749487007626050323967496732359278657193580493324467258802863, 4379499308310772821004090447650785095356643590411706358119239166662089428685562719233435615196994728767593223519226235062647670077854687031681041462632566890129595506430188602238753450337691441293042716909901692570971955078924699306873191983953501093343423248482960643055943413031768521782634679536276233318, 85256449776780591202928235662805033201684571648990042997557084658000067050672130152734911919581661523957075992761662315262685030115255938352540032297113615687815976039390537716707854569980516690246592112936796917504034711418465442893323439490171095447109457355598873230115172636184525449905022174536414781771, 50576597458517451578431293746926099486388286246142012476814190030935689430726042810458344828563913001012415702876199708216875020997112089693759638454900092580746638631062117961876611545851157613835724635005253792316142379239047654392970415343694657580353333217547079551304961116837545648785312490665576832987, 96868738830341112368094632337476840272563704408573054404213766500407517251810212494515862176356916912627172280446141202661640191237336568731069327906100896178776245311689857997012187599140875912026589672629935267844696976980890380730867520071059572350667913710344648377601017758188404474812654737363275994871, 4881261656846638800623549662943393234361061827128610120046315649707078244180313661063004390750821317096754282796876479695558644108492317407662131441224257537276274962372021273583478509416358764706098471849536036184924640593888902859441388472856822541452041181244337124767666161645827145408781917658423571721, 18237936726367556664171427575475596460727369368246286138804284742124256700367133250078608537129877968287885457417957868580553371999414227484737603688992620953200143688061024092623556471053006464123205133894607923801371986027458274343737860395496260538663183193877539815179246700525865152165600985105257601565]
def Legendre(arr,modulo):
exponent = ((modulo-1)/2)
for i in range(len(arr)):
candidate = int(pow(int(arr[i]),int(exponent),modulo))
print("{} : {}\n".format(candidate,arr[i]))
Legendre(ints,p)
Run Code Online (Sandbox Code Playgroud) 我刚刚和我的朋友一起尝试了一些数据结构问题。我从一位朋友那里遇到了这个问题,他也无法解决。
问题:反转数组而不改变零的位置。示例:如果数组有 0 5 7 8 0 9 那么结果应该是 0 9 8 7 0 5。
我尝试过,但它在所有情况下都不能正确执行,如果代码看起来很难看,我很抱歉我现在是新手。
#include<iostream>
using namespace std;
int main()
{
int arr[100], tot, i, j, temp;
cout<<"Enter the Size for Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
cout<<"\nThe Original Array is:\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
j = tot-1;
for(i=0; i<j; i++, j--)
{
if(arr[i] == 0) {
i++;
continue;
}else if(arr[j] == 0) {
j--;
continue;
}
else {
temp = …Run Code Online (Sandbox Code Playgroud) 问题陈述
我想从我的列表中获取所有可能的组合(包括空列表)。
到目前为止我的代码是:
def combination(l):
result = []
for item in range(len(l)):
cut_list = l[:item] + l[item + 1:]
if len(cut_list) > 1:
combination(cut_list)
elif len(cut_list) == 1:
result += cut_list
return result
print(combination([1, 2, 3]))
Run Code Online (Sandbox Code Playgroud)
我的输出是一个空列表
[]
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
Run Code Online (Sandbox Code Playgroud)
我很确定我的退货有些不对劲。
非常感谢任何帮助。