#!/usr/bin/python#-*-coding:utf-8-*- fromCollectionsImportdequedefz69 ():" "card Tricks (1) magician, the top one is black A, the 2nd time from the top to the next 2 in the bottom, and then open is the black K, and then from the top to the next number 3 is placed at the bottom, is the black Q, the K number of K Zhang, turn, to get black a~1; Ask the original card sequence" "SS=13D= Deque (Range (1,ss+1)) F=[1]*SS forIinchRange (ss): D.rotate (-(i%Len (d))) F[d[0]-1]=i+1D.popleft ()PrintFdefz70 ():" "card Tricks (2) The magician's top one is black a, and then from the top to the next number 2 are placed at the bottom, and then open is the black K, and then from the top to the next number 2 is placed at the bottom, is the black Q, turn, get black a~1, red a~1; Ask the original card sequence" " defPai (x):ifX>13: Ch='R'x-=13Else: Ch='b' returnch+Str (x) SS=26D= Deque (Range (1,ss+1)) F=[1]*SS F[0]=pai (1) D.popleft () forIinchRange (2,ss+1): D.rotate (-(2%Len (d))) F[d[0]-1]=Pai (i) d.popleft ()PrintFdefZ71 ():#11 people form a ring, count off, every number to 7, the person will come out, ask 7 times after the remaining 4 peopless=11nn=4KK=7D= Deque (Range (1,ss+1)) forIinchRange (KK): D.rotate (-((nn-1)%Len (d))) PrintD.popleft ()PrintDdefz72 ():#4 of 3 and 3 5 can be composed of several different numbers of how manyE=[] forIinchRange (5): forJinchRange (4): E+=[i*3+j*5] Printset (e)defNtom (x,size,mod): t=[0]*(size) J=0 whileX andj<Size:x,t[j]=Divmod (x,mod) J+=1returnTdefz73 ():#five different numbers and 23, several of which add up to represent all the natural numbers within the 1~23, ask them how much defh73 (XX): t=[] n=5 forNninchRange (1,2**N): Y=sum (Map (LambdaX,y:x*y,xx,ntom (nn,n,2)) ) t.append (y)returnLen (set (t)) SS=23 forIinchRange (ss): forJinchRange (i+1, ss): forMinchRange (j+1, ss): forNinchRange (m+1, ss): KK=ss-i-j-m-NifKk>n:kt=[I,J,M,N,KK]ifh73 (kt) = =SS:Printktdefz74 ():#Weights of 40 weight are broken into four pieces, each is an integer, and with these four can measure 1~40 any weight, ask them how much weight defh74 (XX): t=[] n=4 forNninchRange (1,3**N): Y=sum (Map (LambdaX,y:x*y,xx,ntom (nn,n,3)) ) t.append (y)returnLen (set (t)) SS=40 forIinchRange (ss): forJinchRange (i+1, ss): forMinchRange (j+1, ss): KK=ss-i-j-mifKk>m:kt=[I,J,M,KK]ifh74 (kt) = =SS:Printktdefz75 ():" "10 of people in a ring, divided things. They had {10,2,8,22,16,4,10,6,14,20} things at first, and all the people would have one if they were odd, and then all the people would give their half to the person on the right, and ask them the same number of things after a few times ." "D=deque ([10, 2, 8, 22, 16, 4, 10, 6, 14, 20]) PrintD while[d[0]]*10!=d:d=map (Lambdax:x%2 and(x+1)/2orX/2, D) D2=deque (d) d2.rotate (1) d=map (Lambdax,y:x+y,d,d2)PrintDdefz76 ():#Select a number of 6 numbers, where they are the closest to 10, ask how to choose {3.1,1.7,2.0,5.3,0.9,7.2}m2=[3.1,1.7,2.0,5.3,0.9,7.2] M2=[x*10 forXinchm2] n=6T=[] forNninchRange (1,2**N): TT=ntom (nn,n,2) y=abs (sum (map) (LambdaX,Y:X*Y,M2,TT))-100) T+=[[Y,tt]] t=filter (LambdaX,y=min (t) [0]:x[0]<y+0.01, T) forIincht:m=filter (LambdaX:x,map (LambdaX,y:x andYor0,i[1],m2))Printmdefz77 ():#water splitting problem: Someone has 12 liters of water, want to divide him into 2 6 liters, but only 8 liters and 5 liters of containers, for at least several times can be, how to divide defnexts (a,b,c,d): TT=[] Ke1=reduce (LambdaX,y:x+y,map (Lambdax:str (x), a)) forIinchRange (0,3): ifa[i]!=0: forJinchRange (0,3): ifI!=j anda[j]<B[j]: M=[]+a T=b[j]-A[j]ifm[i]>=T:m[i]-=T M[j]=B[j]Else: M[j]+=M[i] M[i]=0ifM not inchC:tt+=[m] Ke=reduce (LambdaX,y:x+y,map (Lambdax:str (x), M)) D[ke]=Ke1returntt a=[12, 0,0] b=[12,8,5] DD=[A, b] EE={} t=nexts (a,b,dd,ee) DD+=T while1: M=[] forIincht:m+=nexts (i,b,dd,ee) DD+=T T=mif[6,6,0]inchR | Breake=ee["660"] Print[6,6, 0] whilee!=' -': PrintMapLambdax:int (x), List (e)) e=Ee[e]Print[12, 0,0]#raw_input ('---')if __name__=='__main__': S="" forIinchRange (69,78): S+='Z'+str (i) +'() \ n' exec(s)
Python Exercise 5