Hi
I need a little help.
How to check whether a matrix corresponding to the rules?
Matrix = [[1, 30, 34, 55, 75],
[10, 16, 40, 50, 67],
[5, 20, 38, 48, 61],
[4, 26, 43, 49, 70],
[15, 17, 33, 51, 66]]
rules :The first column 1- 15 , second column 16-30,third column 31-45,fourth column 46-60,fifth column 61-75.... (like bingo ticket)
I find the columns, but I do not know how to control them,....
I find the columns like this:
tabel = [[1, 30, 34, 55, 75],
[10, 16, 40, 50, 67],
[5, 20, 38, 48, 61],
[4, 26, 43, 49, 70],
[15, 17, 33, 51, 66]]
for veerg in range(len(tabel[0])):
for rida in range(len(tabel)):
print(tabel[rida][veerg])
print()
Put the ranges in a list:
ranges=[(1,15) ,(16,30),(31,45),(46,60),(61,75)]
Then when you scan a line, you use the index of the item to retrieve the corresponding element in
ranges
and compare its value with the two limits.
An even more pythonic way is to:
iterate lines
zip(line,ranges)
iterate result which are (line_item, (min,max)) and compare
I stack in if . What do I need to write there to
control columns
def on_bingo_tabel(bingo):
for i in range(len(bingo)):
for j in range(len(bingo[i])):
if ????????:
return False
return True
bingo = [[1, 30, 34, 55, 75],
[10, 16, 40, 50, 67],
[5, 20, 38, 48, 61],
[4, 26, 43, 49, 70],
[15, 17, 33, 51, 66]]
ranges=[(1,15) ,(16,30),(31,45),(46,60),(61,75)]
print(on_bingo_tabel(bingo))
(Apr-13-2017, 07:59 AM)jkk30 Wrote: [ -> ]I stack in if . What do I need to write there to control columns
def on_bingo_tabel(bingo):
for i in range(len(bingo)):
for j in range(len(bingo[i])):
if ????????:
return False
return True
bingo = [[1, 30, 34, 55, 75],
[10, 16, 40, 50, 67],
[5, 20, 38, 48, 61],
[4, 26, 43, 49, 70],
[15, 17, 33, 51, 66]]
ranges=[(1,15) ,(16,30),(31,45),(46,60),(61,75)]
print(on_bingo_tabel(bingo))
i-th element of line compared to the bounds from i-th element of
ranges
The one-liner (don't put in your holework unless you really understand how it works):
def check(bingo):
return any(any(not minvalue <= actual <= maxvalue for actual,(minvalue,maxvalue) in zip(line,ranges)) for line in bingo)
I found a solution :D
def on_bingo_tabel(tabel):
for i in range(len(tabel)):
for j in range(len(tabel[i])):
if tabel[i][j] < j*15+1 or tabel[i][j] > j*15+15 :
return False
return True
Thanks to all who contribute to thinking :D