Last active
May 2, 2016 15:51
-
-
Save shivdhar/a83bb2e13bde644d96490ae0d4031bb3 to your computer and use it in GitHub Desktop.
Generate Bingo cards using numpy arrays.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import random | |
CARD_SIZE = 5 | |
# Chooses from twice as many numbers as a card can have. | |
# For example, a card with size 5 can have numbers between 1 and 25*2 -> 50, inclusive (hence the '+ 1') | |
MIN = 1 | |
MAX = CARD_SIZE**2 * 2 + 1 | |
card = np.array( random.sample(range(MIN, MAX + 1), CARD_SIZE**2) ).reshape(CARD_SIZE, CARD_SIZE) | |
# Pretty print the card, using either (1) or (2) | |
# Choose one! | |
# (1) Print numpy's default string representation, with '[' and ']' removed | |
print np.array_str(card).replace('[', ' ').replace(']', '') | |
# (2) Iterate over each element in array | |
for row in card: | |
for element in row: | |
print element, '\t', | |
''' | |
CARD_SIZE = 5 | |
Highlight both ouputs to see spacing differences | |
Note the spaces at the beginning of each line | |
Sample output (1): | |
2 33 41 6 16 | |
30 32 43 35 46 | |
29 14 23 31 48 | |
51 39 21 10 17 | |
26 3 1 24 49 | |
Note the tabs at the end of each line | |
Sample output (2): | |
2 33 41 6 16 | |
30 32 43 35 46 | |
29 14 23 31 48 | |
51 39 21 10 17 | |
26 3 1 24 49 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment