Reputation: 159
I want to riffle shuffle items of a list without necessary importing any module. So a function should return a riffle shuffled list, riffle shuffle is where it will first break it into two lists then interleave them into one list.
for example the list= [ a, b ,c ,d]
should be [ c,a,d,b] or [a, c,b,d] after riffle shuffling it
Upvotes: 0
Views: 751
Reputation: 16029
This is fun! No imports!
The problem is that we need a coin flip without importing anything. Sounds like a test for <some random int> % 2 == 0
. The hard part is <some random int>
. A pointer on the heap maybe?
input_list = ['a', 'b', 'c', 'd']
#you should empty this once and awhile
fill_my_heap = []
#nothing to see here
class Dummy():
pass
for x in range(0,10):
#give me a new pointer
foo = Dummy()
#prevent reuse of heap memory location
fill_my_heap.append(foo)
#get id of new class and strip its last digit because that was always even
ptr_int = int(str(id(foo))[:-1])
#test to see if this is even. Should be 50% of the time. Sort of... ;)
is_even = ptr_int%2==0
#split list
a = input_list[:len(input_list)/2]
b = input_list[len(input_list)/2:]
#and assemble output based on even-switch
if is_even:
output = a + b
else:
output = b + a
print(output)
Gives:
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['c', 'd', 'a', 'b']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['c', 'd', 'a', 'b']
['a', 'b', 'c', 'd']
['c', 'd', 'a', 'b']
['a', 'b', 'c', 'd']
Upvotes: 1
Reputation: 214969
If you don't like imports, a simple LCG is pretty straightforward to code:
def lcg(_):
lcg.val = (1664525 * lcg.val + 1013904223) & 0xffffffff
return lcg.val
lcg.val = id('') # seed
and then:
print sorted(range(52), key=lcg)
Upvotes: 0
Reputation: 304175
cards = range(52)
a = cards[:len(cards)/2]
b = cards[len(cards)/2:]
if id('')/0xffff&1:
a, b = b, a
cards[::2] = a
cards[1::2] = b
print cards
cards = list(range(52))
a = cards[:len(cards)//2]
b = cards[len(cards)//2:]
if id('')//0xffff&1:
a, b = b, a
cards[::2] = a
cards[1::2] = b
print(cards)
Upvotes: 3