John has a deck of 52 cards, stacked in a pile with their backs facing up. He separates the small pile consisting of the seven cards on the top of the deck, turns it upside down, and places it at the bottom of the deck. All cards are again in one pile, but not all of them face down; the seven cards at the bottom do, in fact, face up. He repeats this move until all cards have their backs facing up again. In total, how many moves did John make?
(In reply to
re(2): computer verification  simulation by Charlie)
Nicely done, Charlie. Indeed 112 is the correct answer.
With my first answer (104), more than a guess but less than rigorous (and before coffee) I was wrong.
I was wrong again when I first looked at your post with the binary representation of the deck at each stage, specifically here:
7 0001111111111111111111111111111111111111111111111111
8 1111111111111111111111111111111111111111111110000111
At first, I thought the last seven of row
8 should be
1110000, which would be the case if the 7 cards were individually
flipped upside down and kept in order. But then I reread the
problem and saw that the small pile of 7, as a
group, was flipped
upside down then placed at the bottom. I wondered if this is the
detail that makes the correct answer 112 instead of 104?
So I wrote a simulation with the shuffle function written two
different ways. w_shuffle_Group() is what the problem calls for.
But w_shuffle_Indiv() is the modification where the 7 moved cards
remain in order.
The correct method yields 112 as the correct answer.
And the modified algorithm indeed shows 104, as expected.
(the # comments out the rest of the line so either function can be used)
code follows:
def w_shuffle_Group(a):
first = a[:7] # select first 7
first = first.replace('1','_')
first = first.replace('0','1')
first = first.replace('_','0') # flip 1's and 0's
return a[7:] + first [::1] # last 45 plus reversed & flipped first 7
def w_shuffle_Indiv(a): # NOT what the problem asks for
first = a[:7] # select first 7
first = first.replace('1','_')
first = first.replace('0','1')
first = first.replace('_','0') # flip 1's and 0's
return a[7:] + first # last 45 plus flipped first 7 in original order
count = 0
deck0 = deck = '0'*52
for i in range (150):
count += 1
deck = w_shuffle_Group(deck) # comment out one of these two lines
# deck = w_shuffle_Indiv(deck) # comment out one of these two lines
print (count,deck)
if deck == deck0:
break

Posted by Larry
on 20201130 09:04:48 