 # Python,coding problem

so here is a problem :

Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 ≤ x, y ≤ n and ax, y ≠ 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.

Help Okabe determine whether a given lab is good!

Input
The first line of input contains the integer n (1 ≤ n ≤ 50) — the size of the lab.

The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 ≤ ai, j ≤ 105).

Output
Print “Yes” if the given lab is good and “No” otherwise.

You can output each letter in upper or lower case.

Examples
input
3
1 1 2
2 3 1
6 4 1
output
Yes

here is my code:

``````from random import randint

x = int(input("number: "))
board = []
for a in range(x):
row = []
for b in range(x):
row.append(str(randint(2,3)))
board.append(row)

def boardy(board):
for x in board:
print(" ".join(x))

boardy(board)

q=int in range(0,x)
r=int in range(0,x)
e=int in range(0,x)
o=int in range(0,x)
if board[q][r]==board[q][e]+board[o][r]:
print(True)
else:
print( False)

``````

please help me with answer , i have been stuck in this problem sice yesterday and i am loosing motivation to continue coding as i cant move forward… thanking you in anticipation

here:

``````row.append(str(randint(2,3)))
``````

why do you convert to string? We need to do math with this numbers, for this we want the value to be integers, right?

this line is so confusing, can you explain it?

The next n lines contain n space-separated integers denoting a row of the grid simply means a grid of 3 by 3 (or whatever `n` is), so far i get, but then it gets weird

1 Like

i converted them to string beacuse earleir my output looked like:
[ 3 , 3 , 2 ]
[ 3 , 2 , 2 ]
[ 2 , 3 , 2 ]
where as i wanted them to look like:
3 3 2
3 2 2
2 3 2

it says that there should be n integers which are in grid form eg.
3 3 2
3 2 2
2 3 2
and a1,2 would mean number 3 i.e first row second column element
and these integers should be in range 1 to 105
so i can make change in my code randint(1,10
5)

but we will still need to do math with this integers, so we don’t want them to be strings. Appending them as integers to the list, only convert them to string when you want to print them pretty

a2, 2 will then be the middle one (second row, second column)

Now i understand the notation they use

I think i understand, can you post an updated version of your code which contains the right values generated by randint

so all we need to do is check if every entry in list is between 1 and 105?

so this the exact problem :

i will explain it to you in my language:
so we have to first take users input thats going to be a number
lets say user enters 3 then,our programm should generat 3X3 grid of integers using randint
eg
1 1 2
2 3 1
6 4 1
now our code should return true if every element of this grid can be represented as sum of two numbers: one from its row and other from its column
like say 2 in first row is equal to 1 from first row and one from 3rd collumn
similarly all elements in this grid can be obatained the same way
so if our grid of integer satisfies this condition :the out put should be TRUE
(if an element is 1 it does not have to satisfy this condition )

(i know my question is not precise but please stick with me , @stetim94 your inputs will be valuable for me thanx)

so:

``````1 1 2
2 3 1
6 4 1
``````

would be true, right? i can make 6 (4 of the right of 6 and 2 above 6), and all other numbers i can make as well. Pff… that is not a easy one

1 Like

yep u have got it right ! let me go and fight with this

1 Like

What a difficult problem, seriously.

1 Like

yeah seriously ,but its quite a relief to hear this from you, take your time no need to put a lot of effort on it , there is no urgency ,but if u find somethiong intresting to tell plz share.
thanxs a lot for being patient , going through my never ending not so precise questions ``````from random import randint

x = int(input("number: "))
board = []
for a in range(x):
row = []
for b in range(x):
row.append(randint(2,25))
board.append(row)
print(board)
result = []
for a in range(x):
for b in range(x):
for c in range(x):
print((b,c),(c,b),(a,b),(c,a))
print("first loop ended")
``````

this code produces all scenarios we want to test, but a few too many, still struggling how to filter them out

for `0,0` (a1,1 with that stupid notation of the exercise) we want to the the following sums of the following indexes:

``````0,1 + 1,0
0,1 + 2,0
0,2 + 1,0
0,2 + 2,0
``````

which we get using the following:

``````bc + cb
ab + ca
ab + ca
bc + cb
``````

but i am unsure of at which point you should execute this math operations. (where they should be nested in the loop and how to filter out the remaining faulty things we don’t want)

1 Like
``````from random import randint
from itertools import combinations

class lab():
def __init__(self, gridSize):
if 1<= gridSize <=50:
self.gridSize = gridSize
else:
raise ValueError("Lab size must be an integer between 1 and 50, inclusive.")
self.gsRange = range(gridSize)
self.lab = [[randint(1,(10**5+1)) for row in self.gsRange] for height in xrange(self.gridSize)]
self.columns = [[self.lab[column][row] for column in self.gsRange] for row in self.gsRange]

def PrintLab(self):
for row in self.lab:
row = ["{: ^6}".format(str(i)) for i in row]
print " ".join(row)

def CheckLab(self):
for column in self.gsRange:
for row in self.gsRange:
cell = self.lab[row][column]
if cell == 1:
continue
combos = [(x,y) for x in self.columns[column] if x != cell and x < cell for y in self.lab[row] if y != cell and x < cell]
for x,y in combos:
if x+y != cell:
status = False
continue
else:
status = True
break
if not status:
return False
else:
continue
if not status:
return False
else:
return True

newLab = lab(10)
newLab.PrintLab()
print newLab.CheckLab()
``````

Not going to claim it is anything like efficient, but it was not planned out, rather plan as I go and see where it takes me. I literally ran with the first thing that came to my head. That said it works.

EDIT: I think I miss read I thought it was just one item in the row/column passing meant that whole row/column passed. This should only give true if all bar 1s meet the criteria. Noticed it should return “Yes” or “No” not True/False too.

``````    def CheckLab(self):
for column in self.gsRange:
for row in self.gsRange:
cell = self.lab[row][column]
if cell == 1:
continue
combos = [(x,y) for x in self.columns[column] if x != cell and x < cell for y in self.lab[row] if y != cell and x < cell]
for x,y in combos:
if x+y == cell:
status = True
break
else:
status = False
if not status:
return "No"
return "Yes"
``````
2 Likes

still really impressive. Well done Solving this problem is already difficult

1 Like