generated from tomorrowdevs-projects/programming-basics
-
Notifications
You must be signed in to change notification settings - Fork 3
solution m3-026-update #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Stefano-A
wants to merge
1
commit into
tomorrowdevs-projects:main
Choose a base branch
from
Stefano-A:solution/m3-026
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
# import random | ||
# import copy | ||
|
||
# def create_bingo_card(): | ||
# bingo_card = {} | ||
# step = 0 | ||
# for char in 'BINGO': | ||
# bingo_card.update({char : random.sample(range(1+step, 15+step), k = 5)}) | ||
# step += 15 | ||
# return bingo_card | ||
|
||
# def display_card(dict_card): | ||
# print('{:^3} {:^3} {:^3} {:^3} {:^3}'.format('B', 'I', 'N', 'G', 'O')) | ||
# for i in range(0,5): | ||
# row = list(item[i] | ||
# for item in dict_card.values()) | ||
# print('{:^3} {:^3} {:^3} {:^3} {:^3}'.format(row[0], row[1], row[2], row[3], row[4])) | ||
|
||
# def check_card(dict_card): | ||
# is_win = False | ||
# # check columns | ||
# for n_column in dict_card.values(): | ||
# if is_win: | ||
# break | ||
# else: | ||
# for c in n_column: | ||
# if c == 0: | ||
# is_win = True | ||
# else: | ||
# is_win = False | ||
# break | ||
# # check rows | ||
# if not is_win: | ||
# for i in range(len(dict_card.keys())): | ||
# if is_win: | ||
# break | ||
# else: | ||
# n_row = list(item[i] | ||
# for item in dict_card.values()) | ||
# for r in n_row: | ||
# if r == 0: | ||
# is_win = True | ||
# else: | ||
# is_win = False | ||
# break | ||
# # check diagonals | ||
# if not is_win: | ||
# diagonal_1 = dict_card['B'][0], dict_card['I'][1], dict_card['N'][2], dict_card['G'][3], dict_card['O'][4] | ||
# for d in diagonal_1: | ||
# if d == 0: | ||
# is_win = True | ||
# else: | ||
# is_win = False | ||
# diagonal_2 = dict_card['B'][4], dict_card['I'][3], dict_card['N'][2], dict_card['G'][1], dict_card['O'][0] | ||
# for d in diagonal_2: | ||
# if d == 0: | ||
# is_win = True | ||
# else: | ||
# is_win = False | ||
# break | ||
# break | ||
# return is_win | ||
|
||
# def cross_num(dict_card, called_num): | ||
# for list_num in dict_card.values(): | ||
# for n in range(len(list_num)): | ||
# if list_num[n] == called_num: | ||
# list_num[n] = 0 | ||
# break | ||
# return dict_card | ||
|
||
# my_bingo_card = create_bingo_card() | ||
# copy_card = copy.deepcopy(my_bingo_card) | ||
# calls_list = [] | ||
|
||
# for g in range(0, 1000): | ||
# numbers_list = random.sample(range(1,76), k=75) | ||
# call = random.choice(numbers_list) | ||
# calls_ingame = 1 | ||
# numbers_list.remove(call) | ||
# crossed_card = cross_num(copy_card, call) | ||
# is_card_win = check_card(crossed_card) | ||
|
||
# while is_card_win == False: | ||
# call = random.choice(numbers_list) | ||
# calls_ingame += 1 | ||
# numbers_list.remove(call) | ||
# cross_num(crossed_card, call) | ||
# is_card_win = check_card(crossed_card) | ||
|
||
# copy_card = copy.deepcopy(my_bingo_card) | ||
# calls_list.append(calls_ingame) | ||
|
||
# minimum = min(calls_list) | ||
# maximum = max(calls_list) | ||
# avarage = sum(calls_list)/len(calls_list) | ||
|
||
# print(f'The minimum number of calls for the win is: {minimum}') | ||
# print(f'The maximum number of calls for the win is: {maximum}') | ||
# print(f'The avarage number of calls for the win is: {int(avarage)}') | ||
|
||
import random | ||
import copy | ||
|
||
def check_card_elements(card): | ||
x = len(card[0]) | ||
y = len(card) | ||
to_check = [ | ||
[[],[],[],[],[]], | ||
[[],[],[],[],[]], | ||
[[],[]] | ||
] | ||
for row in range(x): | ||
for column in range(y): | ||
to_check[0][row].append(card[row][column]) | ||
to_check[1][row].append(card[column][row]) | ||
|
||
if row == column: | ||
to_check[2][0].append(card[column][row]) | ||
|
||
if row + column == 4: | ||
to_check[2][1].append(card[column][row]) | ||
for element in to_check: | ||
for sub_element in element: | ||
if sum(sub_element) == 0: | ||
return True | ||
|
||
return False | ||
|
||
def create_bingo_card(): | ||
bingo_card = [] | ||
step = 0 | ||
for letter in range(len('BINGO')): | ||
bingo_card.append(random.sample(range(1+step, 15+step), k = 5)) | ||
step += 15 | ||
return bingo_card | ||
|
||
def cross_card(card, call_number): | ||
for line in range(len(card)): | ||
for number in range(len(card[line])): | ||
if card[line][number] == call_number: | ||
card[line][number] = 0 | ||
return card | ||
return card | ||
|
||
def display_card(card): | ||
x = len(card[0]) | ||
y = len(card) | ||
to_print = [ | ||
[[],[],[],[],[]], | ||
[[],[],[],[],[]], | ||
[[],[],[],[],[]], | ||
[[],[],[],[],[]], | ||
[[],[],[],[],[]], | ||
] | ||
for column in range(x): | ||
for row in range(y): | ||
to_print[row][column].append(card[column][row]) | ||
|
||
print(' B I N G O') | ||
for line in to_print: | ||
n1, n2, n3, n4, n5 = line | ||
line_print = '{:2d} {} {} {} {}'.format(*n1, *n2, *n3, *n4, *n5) | ||
print(line_print) | ||
|
||
generated_card = create_bingo_card() | ||
bingo_calls = random.sample(range(1,76), k=75) | ||
calls_list = [] | ||
|
||
for games in range(0,1000): | ||
playing_card = copy.deepcopy(generated_card) | ||
random.shuffle(bingo_calls) | ||
for numbers in range(len(bingo_calls)): | ||
n_call = bingo_calls[numbers] | ||
cross = cross_card(playing_card, n_call) | ||
check = check_card_elements(cross) | ||
if check: | ||
calls_list.append(numbers+1) | ||
break | ||
|
||
minimum = min(calls_list) | ||
maximum = max(calls_list) | ||
avarage = sum(calls_list)/len(calls_list) | ||
|
||
display_card(generated_card) | ||
print(f'The minimum number of calls for the win is: {minimum}') | ||
print(f'The maximum number of calls for the win is: {maximum}') | ||
print(f'The avarage number of calls for the win is: {int(avarage)}') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want a float number as you used the "/" operator instead "//", why you do a cast like this
int(avarage)
inthe
print` statement at line 188?P.S. you can format the output without change the variable value. Basically is discouraged to use casting caused by performance issues