Skip to content

Instantly share code, notes, and snippets.

@mharju
Created December 19, 2010 19:04
Show Gist options
  • Save mharju/747591 to your computer and use it in GitHub Desktop.
Save mharju/747591 to your computer and use it in GitHub Desktop.
Tässä vähän tämmöstä pikaista testiä! :)
from itertools import chain
import random
def shift(cards, shift):
return [ cards[(i - shift) % len(cards)] for i in range(0, len(cards) ) ]
def shuffle(cards, m):
return [ [ card for pos, card in enumerate(chain(*cards)) if pos % m == p ] for p in range(0, m) ]
def shift_amt(position, m, n):
return (m // 2 - position) % m
def selection_trick(m, n):
cards = list(partition( range(0, n*m), n ))
selection = random.randint(0, m*n - 1)
for i in range(0, m):
position = [ p for p, cds in enumerate(cards) for card in cds if card == selection ][0]
cards = shuffle( shift(cards, shift_amt( position, m, n )), m)
result = list(chain(*cards))[ m*n // 2]
return selection == result
if __name__ == '__main__':
print [ (m, n) for m in range(1, 10) for n in range(1, 10) if selection_trick(m, n) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment