Prev: Dynamic Hill cipher
Next: PE Scrambler
From: J.D. on 27 Apr 2010 20:20 On Apr 27, 8:13 pm, Maaartin <grajc...(a)seznam.cz> wrote: > > "Deal" > > 8) er...the deal step is a little confusing... > > It's very simple: Assume you hold all cards in your hand and there are > two initally empty decks on the table. > - Take 1 uppermost card from your hand, put it on the left deck. > - Take 2 uppermost cards from your hand, put them on the right deck. > - Repeat as long as you have cards in your hand (take less cards if > not enough present). > - Put the left deck on the right one, take all cards in your hand > again. > > I'll check if this corresponds with the program exactly (but again, > minor differences are unimportant). All right, excellent. Your full algorithm is crystal clear to me now.
From: Maaartin on 27 Apr 2010 20:44 There was an error in my card_view. On Apr 28, 2:16 am, "J.D." <degolyer...(a)yahoo.com> wrote: > If you want to interpret the cards for your bury step the same way I > do for my deal step then that would be great. Of course, I do. I changed the function already. > So, for the bury step, if the top card is an ace, then set aside the > ace, count off 14 cards, and put them on the bottom, then put the ace > on the bottom. If the top card is a 2, set aside the 2, count off 2 > cards, put them on the bottom, then put the 2 on the bottom. If the > top card is a king, set aside the king, count off 13 cards and put > them on the bottom, and then put the king on the bottom. > > Does that work for you? Sure, it used to move one less card, but I gladly make the little change. It's surely no worse than before and it was my intend. Have a look at demo_bury_example. > As for this part: > > > > *Instances are as follows: > > > A = ace of hearts, ace of clubs > > > B = 2 of hearts, 2 of clubs > > > C = 3 of hearts, 3 of clubs > > > .... > > > M = king of hearts, king of clubs > > > N = ace of diamonds, ace of spades > > > O = 2 of diamonds, 2 of spades > > > .... > > > Z = king of diamonds, king of spades > > Does the above quoted way of interpreting letters into cards work for > your find_shuffle step? Now it does, have a look at demo_interpretation. At the of my program below, there are calls to three demos, I think you can use something like this for other programs, too. ######### def new_deck(n=26, occurrences=2): deck = ""; for i in range(occurrences): for j in range(n): deck = deck + chr(ord("aA"[i&1]) + j) return deck; # For number cards, (e.g. 2 of clubs) use the number on the face, Jacks = 11, Queens = 12, Kings = 13, Aces = 14 def card_to_number(card): n = ord(card.lower()) - ord("a") n %= 13 return n+1 if n>0 else 14 def bury(deck): n = card_to_number(deck[0]) + 1 return deck[n:] + deck[1:n] + deck[0] def find_shuffle(deck, letter): for n in range(len(deck)): if deck[n].lower() == letter: break return deck[n:] + deck[:n] def deal(deck, list): result = [] for n in list: result += [""]; while len(deck)>0: for i in range(len(list)): n = min(list[i], len(deck)) result[i] = deck[:n] + result[i] deck = deck[n:] result = "".join(result) return result; def mg_shuffle1(deck, letter, shouldPrint=False): deck = find_shuffle(deck, letter) if shouldPrint: print letter, "=>", deck deck = bury(deck) if shouldPrint: print " ", deck deck = find_shuffle(deck, letter) if shouldPrint: print " ", deck deck = deal(deck, (1, 2)) if shouldPrint: print " ", deck return deck def mg_postprocess1(deck, shouldPrint=False): for i in range(3): deck = bury(deck) if shouldPrint: print "* ", deck return deck #puts spaces around each card, so it's aligned with the card_view def spaced_view(deck): result = "" for card in deck: result += " " + card + " " return result[1:] #converts a letter representation of a card into a normal one (c=clubs, etc., O denotes 10) def card_view(deck): result = "" for card in deck: n = 2 if card<="Z" else 0; card = card.lower() diff = ord(card) - ord("a") if diff>=13: diff -= 13; n += 1 shape = "cdhs"[n] val = "A23456789OJQK"[diff] result += " " + shape + val return result[1:] def number_view(deck): result = "" for card in deck: result += "%3d" % card_to_number(card) return result[1:] def demo_interpretation(): print "\n", "print the interpretation" deck = new_deck() print spaced_view(deck) print card_view(deck) print number_view(deck) def demo_bury_example(): print "\n", "bury example" for i in range(2): deck = new_deck() if i==1: deck = deck[1] + deck[0] + deck[2:] print card_view(deck), "<- original" deck = bury(deck) print card_view(deck), "<- after bury" def demo(): s = "qwerty"; print "\n", "processing", repr(s), "in detail" deck = new_deck() for letter in s: deck = mg_shuffle1(deck, letter, True) for s in ["pw", "qw", "rw", "sw"]: print "\n", "state after processing", repr(s); deck = new_deck() for letter in s: deck = mg_shuffle1(deck, letter) print deck deck = mg_postprocess1(deck) print deck demo() demo_interpretation() demo_bury_example() #########
From: Maaartin on 27 Apr 2010 21:18 I've found an unnecessary weakness in the algorithm: The topmost (i.e., the most important) card in bury depends only on the last input. The remedy is simple: move it in find_shuffle to the bottom, too. It means: 2) Take all cards before that instance and including the instance and move them to the end; Below the whole program again: ######### def new_deck(n=26, occurrences=2): deck = ""; for i in range(occurrences): for j in range(n): deck = deck + chr(ord("aA"[i&1]) + j) return deck; # For number cards, (e.g. 2 of clubs) use the number on the face, Jacks = 11, Queens = 12, Kings = 13, Aces = 14 def card_to_number(card): n = ord(card.lower()) - ord("a") n %= 13 return n+1 if n>0 else 14 def bury(deck): n = card_to_number(deck[0]) + 1 return deck[n:] + deck[1:n] + deck[0] def find_shuffle(deck, letter): for n in range(len(deck)): if deck[n].lower() == letter: break n += 1 return deck[n:] + deck[:n] def deal(deck, list): result = [] for n in list: result += [""]; while len(deck)>0: for i in range(len(list)): n = min(list[i], len(deck)) result[i] = deck[:n] + result[i] deck = deck[n:] result = "".join(result) return result; def mg_shuffle1(deck, letter, shouldPrint=False): deck = find_shuffle(deck, letter) if shouldPrint: print letter, "=>", deck deck = bury(deck) if shouldPrint: print " ", deck deck = find_shuffle(deck, letter) if shouldPrint: print " ", deck deck = deal(deck, (1, 2)) if shouldPrint: print " ", deck return deck def mg_postprocess1(deck, shouldPrint=False): for i in range(3): deck = bury(deck) if shouldPrint: print "* ", deck return deck #puts spaces around each card, so it's aligned with the card_view def spaced_view(deck): result = "" for card in deck: result += " " + card + " " return result[1:] #converts a letter representation of a card into a normal one (c=clubs, etc., O denotes 10) def card_view(deck): result = "" for card in deck: n = 2 if card<="Z" else 0; card = card.lower() diff = ord(card) - ord("a") if diff>=13: diff -= 13; n += 1 shape = "cdhs"[n] val = "A23456789OJQK"[diff] result += " " + shape + val return result[1:] def number_view(deck): result = "" for card in deck: result += "%3d" % card_to_number(card) return result[1:] def demo_interpretation(): print "\n", "print the interpretation" deck = new_deck() print spaced_view(deck), "<- as letters" print card_view(deck), "<- as cards" print number_view(deck), "<- as numbers" def demo_bury_example(): print "\n", "bury example" for i in range(2): deck = new_deck() if i==1: deck = deck[1] + deck[0] + deck[2:] print card_view(deck), "<- original" deck = bury(deck) print card_view(deck), "<- after bury" def demo(): s = "qwerty"; print "\n", "processing", repr(s), "in detail" deck = new_deck() for letter in s: deck = mg_shuffle1(deck, letter, True) print "\n", "states after input to the initial state" for i in range(10): s = chr(ord("a") + i) deck = new_deck() for letter in s: deck = mg_shuffle1(deck, letter) print deck, "<- after input", repr(s) deck = mg_postprocess1(deck) print deck, "<- after final 3 buries" demo() demo_interpretation() demo_bury_example() #########
From: J.D. on 27 Apr 2010 21:30 On Apr 27, 9:18 pm, Maaartin <grajc...(a)seznam.cz> wrote: > I've found an unnecessary weakness in the algorithm: The topmost > (i.e., the most important) card in bury depends only on the last > input. The remedy is simple: move it in find_shuffle to the bottom, > too. > > It means: > 2) Take all cards before that instance and including the instance and > move them to the end; > Noted.
From: bmearns on 27 Apr 2010 21:45
Man, you guys are hard to keep up with! But I appreciate all the great input, I feel like we're getting close to something good. Re: mg_shuffle1 This looks great Maaartin. I think we can integrate a variation on J.D.'s shuffle, in place of the simpler find_shuffle you used. find_shuffle() does: [ h ] A [ i ] B [ t ] --> A [ i ] B [ t ] [ h ] let find_shuffle_2() do : [ h ] A [ i ] B [ t ] --> [ i ] B [ h ] A [ t ] I've tried this by hand and it's pretty easy and quick. I know it looks a little weak, because it leaves the tail alone. However, on either side of your bury function, I think it will work out pretty well. I prefer it over the original find_shuffle because it reverses the order of A and B (the two occurrences), and (more importantly) changes the interval between them. It also doesn't leave A or B at any obvious location. I'm having trouble understanding your deal function, though, and I've lost track of which version that you described is actually implemented. Can you describe it again in terms of what you actually do with a deck of cards? I'm still wondering if we need to include the deal, especially for every input. If we use your three part algorithm (shuffle, bury, shuffle), and then use a variation of the bury routine to choose the outputs as we previously discussed, I expect we can get good results, though obviously we'll need to test it to be sure. I think my brain is full for the moment. I'll see what kind of testing I can get done tomorrow. -Brian |