Posts: 4
Threads: 1
Joined: Apr 2017
Apr-12-2017, 04:45 AM
(This post was last modified: Apr-12-2017, 08:32 AM by Kebap.)
Hi
I need a little help.
How to check whether a matrix corresponding to the rules?
1 2 3 4 5 |
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)
Posts: 2,342
Threads: 62
Joined: Sep 2016
Posts: 4
Threads: 1
Joined: Apr 2017
Apr-12-2017, 05:35 AM
(This post was last modified: Apr-12-2017, 08:33 AM by Kebap.)
I find the columns, but I do not know how to control them,....
I find the columns like this:
1 2 3 4 5 6 7 8 9 10 11 |
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 ()
|
Posts: 687
Threads: 37
Joined: Sep 2016
Apr-12-2017, 08:02 AM
(This post was last modified: Apr-12-2017, 08:33 AM by Kebap.)
Put the ranges in a list:
1 |
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:
1 2 3 |
iterate lines
zip (line,ranges)
iterate result which are (line_item, ( min , max )) and compare
|
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Posts: 4
Threads: 1
Joined: Apr 2017
I stack in if . What do I need to write there to control columns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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))
|
Posts: 687
Threads: 37
Joined: Sep 2016
Apr-13-2017, 09:40 PM
(This post was last modified: Apr-13-2017, 09:40 PM by Ofnuts.)
(Apr-13-2017, 07:59 AM)jkk30 Wrote: I stack in if . What do I need to write there to control columns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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):
1 2 |
def check(bingo):
return any ( any ( not minvalue < = actual < = maxvalue for actual,(minvalue,maxvalue) in zip (line,ranges)) for line in bingo)
|
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Posts: 4
Threads: 1
Joined: Apr 2017
I found a solution :D
1 2 3 4 5 6 |
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
|