Why is (x,y) NoneType? - python

I'm having problem with my code (Jupyter). Ordered pattern vacuum cleaner with obstacles. I am loading an array from a file (see picture if needed)
Then I have a function:
x_lim = 9
y_lim = 9
def ordered_move(x,y,gulv):
if gulv[x,y] == 0:
if x < 9:
x +=1
return (x,y)
if x == x_lim:
y +=1
return (x,y)
if gulv[x, y] > 0:
x == 0
y += 1
return (x,y)
if x == 9 and y == 9:
x == 6
y == 3
x += 1
return (x,y)
if x == 9 and y == 3:
x == 7
y == 4
x += 1
return (x,y)
if x == 9 and y == 4:
x == 6
y == 5
x += 1
return (x,y)
if x == 9 and y == 5:
x == 7
y == 6
x += 1
return (x,y)
if x == 9 and y == 6:
x == 6
y == 7
x += 1
return (x,y)
and then I use the code in:
x = 0
y = 0
time = 0
dust_in_room = 100
dust_removed = 0
time_passed_vec = []
dust_in_room_vec = range(100)
while dust_in_room > 7:
if array_from_file[x,y] == -1:
dust_in_room -= 1
array_from_file[x,y] = -2
time_passed_vec = time_passed_vec + [time]
dust_removed +=1
print(time)
time += 1
(x,y)=ordered_move(x,y,array_from_file)
I get the Type Error
"cannot unpack non-iterable NoneType object"
in the last line.
As you can tell from the code I am new to python, so maybe you are able to help with a simple explanation. Thank you.

The explanation is that none of the if conditions is verified.
If a function ends without a return statement the result is like if there was a return None.

Related

How would I implement backtracking here?

I've been struggling to find the right logic for my sudoku solver. So far I've created a function to take (x,y) coordinates and the number and check if it is a legal move.
Though I don't understand how I can iterate through the grid replacing every 0 with a legal number, then go back and fix numbers that make the solve incorrect.
For example sometimes I will be left with 0's on the grid because some numbers can no longer be played. I took a deeper look at it and realized that numbers played previously in the same row were legal but ruined the puzzle. Instead, if those numbers were a bit higher the puzzle would be solved.
I understand backtracking would be my friend here but, I have no clue on how to implement it. So far here is what I have.
solve.py
import numpy as np
import time
class sodoku:
def __init__(self,grid,boxRange):
self.grid = grid
self.boxRange = boxRange
def show(self):
for row in self.grid:
print(row)
def solve(self):
def possible(num,x,y):
def box(x,y):
board = np.array(self.grid)
result = {}
size = 3
for i in range(len(board) // size):
for j in range(len(board) // size):
values = board[j * size:(j + 1) * size, i * size:(i + 1) * size]
result[i * size + j + 1] = values.flatten()
if y <= 2 and x <= 2:
squareBox = result[1]
if (y <= 5 and y > 2) and x <= 2:
squareBox = result[2]
if (y <= 8 and y > 5) and x <= 2:
squareBox = result[3]
if (y <= 2 ) and (x <= 5 and x > 2):
squareBox = result[4]
if (y <= 5 and y > 2)and (x <= 5 and x > 2):
squareBox = result[5]
if (y <= 8 and y > 5)and (x <= 5 and x > 2):
squareBox = result[6]
if (y <= 2) and (x <= 8 and x > 5):
squareBox = result[7]
if (y <= 5 and y > 2)and (x <= 8 and x > 5):
squareBox = result[8]
if (y <= 8 and y > 5)and (x <= 8 and x > 5):
squareBox = result[9]
return squareBox
row = self.grid[y]
column= [r[x] for r in self.grid]
square = box(x,y)
if (num not in row) and (num not in column) and (num not in square):
return True
else:
return False
y = 0
for row in self.grid:
x = 0
for number in row:
if number == 0:
for i in range(1,10):
if possible(i,x,y):
row[x] = i
elif i == 9 and possible(i,x,y) == False: pass
#what would I do here now
x += 1
y += 1
boxRange = "3x3"
bxd = []
with open('board.txt', 'r') as f:
for line in f:
line = line.strip()
line = line.split(' ')
bLine = [int(x) for x in line]
bxd.append(bLine)
# brd = [[3,0,0,2],[0,4,1,0],[0,3,2,0],[4,0,0,1]]
brd = sodoku(bxd,boxRange)
brd.show()
brd.solve()
print('-----Solved------')
brd.show()
board.txt
5 3 0 0 7 0 1 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
Recursive pseudocode backtracing sudoku solver:
#solve will return a solved board, or None if it fails
def solve(board):
#case 1: board is solved
if board.is_solved: #simple check for leftover empty spaces
return board #board is solved. unzip the call stack
pos = board.next_empty_space()
valid = [i for i in range(1,10) if board.is_valid(pos, i)]
#case 2: not solved and no more valid moves
if not valid:
return None #no valid moves left
new_board = copy(board) #don't step on the original data in case this isn't the solution
for value in valid:
new_board[pos] = value
result = solve(new_board)
#case 3: one of the valid moves led to a valid solution
if result is not None: #we found a fully solved board
return result #here's where we unzip the call stack
#case 4: none of the valid moves led to a valid solution
return None #none of the valid moves panned out
Basically you consider each empty space on the board as a branch in a tree, and sub-branches from each branch you insert a new number which is currently valid at that point in the tree. If you get to the end of a branch, and there are no more valid moves left (sub-branches) you have either successfully filled in all the blank spaces or one of the numbers is wrong. When None gets returned, and execution goes back to the caller (up a frame in the call stack), the local position in the for loop going over valid moves is what "remembers" where you're at, and what the next possible valid move should be. It's basically a depth-first tree search algorithm for a correct board state.

Comparing each char in two strings and incrementing: Python

for x in predRslt:
for y in actRslt:
if x == y and x =='1':
trueP += 1
elif x == y and x =='0':
trueN += 1
elif x != y and x == '1':
falseN += 1
elif x != y and x == '0':
falseP += 1
charNum += 1
totalActN = trueN + falseP
totalActP = falseN + trueP
totalPredN = trueN + falseN
totalPredP = trueP + falseP
print falseP
cmp_rslt('0110101001','1100100101')
actual output: 25
expected output: 2
Im trying to go through each string sequentailly from the beginning and compare the results. Then increment the corresponding TrueN, TrueP, FalseN, or FalseP.
for some reason, I keep getting an output of 25 when I should be getting way less than that because there's only 10 comparisons I should be making
you should iterate using the index of the first string like this:
for idx, x in enumerate(predRslt);
y = actRslt[idx]
if x == y and x =='1':
trueP += 1
elif x == y and x =='0':
trueN += 1
elif x != y and x == '1':
falseN += 1
elif x != y and x == '0':
falseP += 1

How to use a variable to find another variable in a list - python

(Python 3.x)
z=[]
x=0
while 1==1:
x=x+1
y=1
z.append(x)
while y==1:
a = 0
b = 0
if z(a)==x:
print(x)
y = 2
elif x%z(a)!= 0:
a = a+1
elif b == 2:
y = 2
else:
b = b+1
So, I made a code to find all the prime numbers until python crashes. However, it relies on z(a) for it to work. The idea is that as "a" changes, it moves on in the list.
"z(a)" is where the error lies, so does anyone know a way to fix this?
z is a list. You can approach values inside it by indexes using the z[a] operator (and not z(a) which assumes a function call with a as parameter).
I've taken the liberty of using boolean variables, += operators and unpacking values:
z = []
x = 0
while True:
x += 1
y = True
z.append(x)
while y:
a, b = 0, 0
if z[a] == x:
print(x)
y = False
elif x % z[a]: a += 1
elif b == 2: y = False
else: b += 1
I believe that's what you want to achieve (infinitely incrementing prime generator):
def prime (n): return not any([n % i == 0 for i in range(2, n)])
x = 0
while True:
if prime(x): print(x)
x += 1

Recursive addition/subtraction in python with negative integers

I am trying to make a program that adds and subtracts two arguments using recursion. So far my program is working for positive integers but I am completely lost about how I can make this work for negative integers. I would really appreciate your help.
Here is my code so far:
def add(x,y):
"""add takes x and y and adds them together"""
if y == 0:
return x
else:
return add1(add(x, sub1(y)))
def sub(x,y):
"""sub takes x and y and subtracts them"""
if y == 0:
return x
else:
return sub1(sub(x, sub1(y)))
def add1(x):
return x+1
def sub1(x):
return x-1
I'd go with this
def add(x,y):
if y > 0:
return add(x, y-1) + 1
elif y < 0:
return add(x, y+1) - 1
else:
return x
Subtract would be the same idea, but flip the signs
def sub(x,y):
if y > 0:
return sub(x, y-1) - 1
elif y < 0:
return sub(x, y+1) + 1
else:
return x
Testing
>>> add(3,5)
8
>>> add(3,0)
3
>>> add(3,-5)
-2
>>> subtract(8,3)
5
>>> subtract(3,8)
-5
>>> subtract(3,0)
3
In each case you try to get y closer to 0 since y==0 is the base case for your recursion.
If y is positive, you do so by repeatedly subtracting 1.
If y is negative, you repeatedly add 1.
In every case, do "the right thing" to x:
Addition
if x or y is 0, return the other value
if y is positive, add 1 to x, subtract 1 from y: 6 + 2 = 7 + 1 = 8 + 0 = 8
if y is negative, subtract 1 from x, add 1 to y: 6 + (-2) = 5 + (-1) = 4 + 0 = 4
code:
def add(x, y):
if y == 0:
return x
elif x == 0:
return y
elif y > 0:
return add(add1(x), sub1(y))
else:
return add(sub1(x), add1(y))
Subtraction
if y is 0, return x
if y is positive, subtract 1 from x, subtract 1 from y: 6 - 2 = 5 - 1 = 4 - 0 = 4
if y is negative, add 1 to x, add 1 to y: 6 - (-2) = 7 - (-1) = 8 - 0 = 8
code:
def sub(x, y):
if y == 0:
return x
elif y > 0:
return sub(sub1(x), sub1(y))
else:
return sub(add1(x), add1(y))

Python: syntax assignment error in iteration

I am doing a simple script for self learning in python where the scripts in turn finds the 1000th prime number but I get a syntax error.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:(counter = counter + 1 and integer = integer + 1)
print (y)
when it comes to the ='s assignment right after the ELSE operator and I don't understand why it won't let me add one to both the counter and integer when this has worked in other iteration scenario
In python you can't make an assignment inside an expresion, to avoid misspellings between = and ==. So you must do that in two lines:
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)
try this
else:
counter = counter + 1
integer = integer + 1
In python, assignment to variable has no Boolean value. and mean Boolean operator not do this and this.
so you need to split the statements.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)

Categories

Resources