Where to go for the written test two questions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.