-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path51_Facebook_Shuffle_Cards.py
More file actions
executable file
·42 lines (25 loc) · 1.05 KB
/
51_Facebook_Shuffle_Cards.py
File metadata and controls
executable file
·42 lines (25 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
This problem was asked by Facebook.
Given a function that generates perfectly random numbers between 1 and k (inclusive),
where k is an input, write a function that shuffles a deck of cards represented as an array using only swaps.
It should run in O(N) time.
Hint: Make sure each one of the 52! permutations of the deck is equally likely.
"""
from random import randint
def generate_random_num(k):
return randint(1, k)
def shuffle_deck():
cards = [i for i in range(52)]
# shuffle
for old_card_pos in range(52):
new_card_position = generate_random_num(52 - old_card_pos) -1 # now the range is [0, k-1],
# need to change the range as the array of cards starts from zero and goes to 51
#swap
cards[old_card_pos], cards[new_card_position] = cards[new_card_position], cards[old_card_pos]
return cards
if __name__ == '__main__':
# cards = shuffle_deck()
# print(cards)
# print(len(cards))
# check all the cards are present in 52 shuffles
assert all(x in shuffle_deck() for x in range(52))