Implementation 1 def PF1x n lenx m n2 top listxm bot listxm for k in rangem x2k

Implementation 1 def pf1x n lenx m n2 top listxm bot

This preview shows page 40 - 45 out of 45 pages.

Implementation 1 def PF1(x): n = len(x) m = n/2 top = list(x[:m]) bot = list(x[m:]) for k in range(m): x[2*k] = top[k] x[2*k+1] = bot[k] This is a Void function. It returns None. However, it permutes the values in the list referenced by x according to the perfect shuffle. Make a copy of the top and bottom halves They become the even-indexed and odd-indexed entries
Image of page 40
Implementation 2 def PF2(x): n = len(x) m = n/2 y = [] for k in range(m): y.append(x[k]) y.append(x[k+m]) return y This is a fruitful function. It returns a reference to a list that is the perfect shuffle of the list referenced by x Build y up through repeated appending x[k] comes from the top half of the list, x[k+m] comes from the bottom half.
Image of page 41
Perfect Shuffle Cycles Question: Given a length-n list x where n is even, how many perfect shuffle updates are required before we cycle back to the original x?
Image of page 42
Perfect Shuffle Cycles # Assume x0 is a given list x = list(x0) PF1(x) numPFs = 1 while x!=x0: PF1(x) numPFs+=1 print numPFs Solution Using the Void function PF1:
Image of page 43
Perfect Shuffle Cycles # Assume x0 is a given list x = PF2(x0) numPFs = 1 while x!=x0: x = PF2(x) numPFs+=1 print numPFs Solution Using the Fruitful function PF2:
Image of page 44
Sample Outputs n numPFs -------------------- 8 3 52 8 444 442 1000 36 10000 300 100000 540
Image of page 45

You've reached the end of your free preview.

Want to read all 45 pages?

  • Fall '19
  • Harshad number, Parameter x

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes