Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check matrix...
#1
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)
Reply
#2
What have you tried?
Reply
#3
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()
Reply
#4
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
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
Reply
#5
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)) 
Reply
#6
(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)  
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
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  define a diagonal matrix from a matrix amalalaoui 1 2,318 May-15-2019, 01:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020