1,a is an ordered array, but after moving the digits to the right, it is now pre-positioned in a to find the position of the element key, which, if not present, returns 0. For example a=[5,6.7.8,1,2,3,4].
Realize:
1 #Quna12 defFindpos (a,key):3min=a[0];4 forIinchRange (len (a)):5 ifa[i]<min:6min=A[i];7minpos=i;8 PrintMin,minpos#find the starting point, thus dividing the array into two halves9 ifKey>a[minpos-1]:Ten return0; One elifKey>a[-1]: A returnPOS (A[0:minpos],key) +1; - Else: - returnPOS (A[minpos:],key) +Minpos; the defPOS (Que,key):#binary search, complexity O (logn) -Low=0;high=len (que)-1; - while(low<=High ): -Mid= (Low+high)/2; + ifque[mid]<Key: -low=mid; + elifQue[mid]>Key: AHigh=mid; at Else: - returnmid; - returnLow ; - -a=[3,4,5,6,7,8,9,0,1,2,3] - PrintFindpos (a,2)
2, String decryption. For example ' ab2e3d ' Output ' ababeeed '
Realize:
1 #Quna22 defDecode (s):3rs="'4i=0;k=0;flag=05 whilei<Len (s):6 ifs[i].isdigit ():7D=i;8 whileS[d].isdigit ():#What 's the back count ?9 ifD<len (s) -1:d+=1;Ten Else: flag=1; Break#Flag Flag If the length of S is exceeded One ifFlag:n=int (s[i:]); rs=rs+s[k:i]*n; Break A Else: N=int (S[i:d]); rs=rs+s[k:i]*n;i=d;k=D - - Else: theI+=1 - Else: -rs=rs+S[k:] - returnRS + PrintDecode'3ABC13DD2EE')
Where to go for the written test two questions