Check matrix... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Check matrix... (/thread-2810.html) |
Check matrix... - jkk30 - Apr-12-2017 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) RE: Check matrix... - micseydel - Apr-12-2017 What have you tried? RE: Check matrix... - jkk30 - Apr-12-2017 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() RE: Check matrix... - Ofnuts - Apr-12-2017 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 RE: Check matrix... - jkk30 - Apr-13-2017 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)) RE: Check matrix... - Ofnuts - Apr-13-2017 (Apr-13-2017, 07:59 AM)jkk30 Wrote: I stack in if . What do I need to write there to control columns 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) RE: Check matrix... - jkk30 - Apr-14-2017 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 TrueThanks to all who contribute to thinking :D |