Trying create a matrix for sudoku with random numbers in Python - python

I need to create a sudoku template, with it all filled. I need to do with random numbers, so I check if the number already exists in the row and in the column. The problem is that when a number is repeated in the column, it does not try another one, getting an infinite loop. Can Someone help me? Remembering that I am required to do with random numbers.
import random
matrix = [[None for i in range(9)] for j in range(9)]
def criarSdk():
for l in range(9):
for c in range(9):
if matrix[l][c] is None:
tmp3 = False
print("linha l={}, coluna c={}".format(l,c))
while tmp3 is False:
ale = random.randint(1, 9)
tmp1 = veriLine(matrix,ale, l)
tmp2 = veriCol(matrix,ale, c)
tmp3 = tmp1 and tmp2
if tmp3 is True:
matrix[l][c] = ale
def veriLine(vetor, value, line):
tmp = True
for c in range(9):
if value == vetor[line][c]:
tmp = False
return tmp
def veriCol(vetor, value, col):
tmp = True
for l in range(9):
if value == vetor[l][col]:
tmp = False
return tmp
criarSdk()
for i in range(9):
print(matrix[i])

Your problem is not with the code, but with your algorithm.
If you fill up a sudoku like this, you can end up in a situation where no digit can be filled in for some space.
Consider this:
1 2 3 4 5 6 7 8 9
2 1 5 3 4 7 8 6 x
You can imagine the first line getting filled with random values (which happen to be in order) and then the values on the second line also getting filled in with random values, but what value to put in x? You can't put in 9, because it would conflict on the column. But you can't put in anything else either, since everything else is already on the line.
In this case, your code will loop endlessly, trying random numbers, but none of them will ever work.
You need to look into backtracking and come up with a better solution. Note that your requirement for randomly picking a number doesn't have to mean you keep calling random.randint() for a new option, you could also randomly pick from a set of numbers you haven't tried yet.

Related

Find The Parity Outlier using dictionary {Python}

during the Kata on Codewars called 'Find The Parity Outlier' I faced a problem, and have been trying to solve it using dictionary. I pass almost all tests except 4.
Instruction for the Kata is:
You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.
The function is:
def find_outlier(integers):
d = dict()
count = 0
count1 = 0
for i in range(len(integers)):
if integers[i] % 2 != 0 :
d['odd'] = integers[i]
else:
d['even'] = integers[i]
for j in range(len(integers)):
if integers[j] % 2 == 0:
count += 1
else:
count1 += 1
if count > count1:
return d['odd']
return d['even']
Test Results:
2 should equal 1
36 should equal 17
36 should equal -123456789
0 should equal 1
So the question is? Why is it so? Can you help me to sort the problem out? Thanks a lot!
I'm not sure what exactly you're referring to with that list of test results. In general though, your method with the dictionary seems like it might be overcomplicating things a bit as well. You shouldn't need to use a dict, and you shouldn't need two for loops either. Here's an alternative solution to this problem using only list comprehension.
def find_outlier(arr):
# input should be an array-like of integers (and length >= 3) with either only one odd element OR only one even element
odd_mask = [n%2 != 0 for n in arr] # boolean array with True in the location(s) where the elements are odd
even_mask = [n%2 == 0 for n in arr] # boolean array with True in the location(s) where the elements are even
N_odd = sum(odd_mask) # number of odd elements in the input
N_even = sum(even_mask) # number of even elements in the input
if N_even == 1: # if even is the 'outlier'...
return arr[even_mask.index(True)] # return the element of the input array at the index we determined we had an even
elif N_odd == 1: # if odd is the 'outlier'...
return arr[odd_mask.index(True)] # return the element of the input array at the index we determined we had an odd
else: # something has gone wrong or the input did not adhere to the standards set by the problem
return None
And even this is technically not as efficient as it could be. Let me know if you try this and whether it solves whatever issue you were experiencing with expected results.
In your code the final part should not be in the else block, nor even in the for loop:
if count > count1:
return d['odd']
return d['even']
Like this is may give a wrong result. For instance, if the first number in the input is odd, and is the only odd one, then this code will return d['even'] which is obviously wrong.
Place these lines after the loop (with correct indentation) and it should work.
However, this problem can be solved without dictionary or extra lists. Have a go at it.
def find_outlier(integers):
parity = integers[-2] % 2
if integers[-1] % 2 != parity:
if integers[0] % 2 != parity:
return integers[-2]
else:
return integers[-1]
for i in integers:
if i % 2 != parity:
return i

My python recursive function won't return and exceeds maximum recursive depth

I simply do not understand why this is not returning the value and stopping the recursion. I have tried everything but it seems to just keep on going no matter what I do. I am trying to get the program to get the loop to compare the first two values of the list if they are the same return that it was the first value. If they were not, add the first and second values of each list and compare, etc etc until it reaches the end of the list. If the sum of the values in each list never equal each other at any point then return 0.
It is supposed to take three inputs:
A single integer defining the length of the next two inputs
First set of input data
Second set of input data
Ex input
3
1 3 3
2 2 2
It should output a single number. In the case of the example data, it should output 2 because the sum of the lists equalled at the second value.
N = int(input())
s1 = input().split()
s2 = input().split()
count = 0
def func1(x,y):
if x == y:
return(count)
elif (N - 1) == count:
return(0)
else:
count + 1
return(func1(x + int(s1[count]), y + int(s2[count])))
days = func1(int(s1[0]),int(s2[0]))
print(days)
I am sorry in advance if I really messed up the formatting or made some dumb mistake, I am pretty new to programming and I have never posted on here before. Thanks in advance :)
The problem is that you never actually update the variable count. However, just writing:
count += 1
is not going to work either without declaring the variable global:
def func1(x, y):
global count
....
That said, global variables increase code complexity and break re-enterability, i.e. the same function can no longer be called twice, not to mention about concurrency. A much cleaner way is to make count a function argument, it will look like this (the code not tested and is here for illustration only):
N = int(input())
s1 = [int(c) for c in input().split()]
s2 = [int(c) for c in input().split()]
def func1(x, y, count=0):
if x == y:
return count
elif count == N - 1:
return 0
else:
return(func1(x + s1[count], y + s2[count]), count + 1)
days = func1(int(s1[0]),int(s2[0]))
print(days)
To answer "How would you go about solving this problem then" – If I understood the problem correctly, the aim is to find the index where the "running total" of the two lists is the same. If so,
def func1(s1, s2):
total_a = 0
total_b = 0
for i, (a, b) in enumerate(zip(s1, s2)):
total_a += a
total_b += b
if total_a == total_b:
return i
return 0
print(func1([1, 3, 3], [2, 2, 2]))
does the trick. (I've elided the input bits here – this function just works with two lists of integers.)

how to check for square condition in sudoku

I am a beginner and wrote a small script for playing sudoku
I was able to check for the row and column conditions but i need little help to check for square condition i.e there should be only one unique digit in a 3x3 square
Also i would be grateful if anyone would help me to reduce the size of the conditions and can please explain me how to create a random sudoku board generating random numbers as i have taken a static board in
tf()
Also i want to make GUI version for it so please recommend me a way to do it.
Thanks in advance
following is the code link is available here https://drive.google.com/file/d/0B0mzB3WBm-8-VzJua1BLakExajQ/view?usp=sharing
board=[[5,7,3,6,9,4,2,1,8],[9,2,4,7,8,1,3,5,'X'],['X',1,6,3,5,'X',7,9,4],[3,8,5,4,2,7,9,6,1],[1,9,2,5,3,6,8,4,7],[4,6,7,9,1,8,5,2,3],[7,3,1,'X',4,5,6,8,'X'],
['X',5,8,1,7,9,4,3,2],[2,4,9,8,6,3,'X',7,5]]#simple sudoku board for testing
def initiator():#print the board and start redirect to the funtion
for i in range(0,9):
print str(board[i])+"\n"
rc_checker()
def find_xs():#find whether there are any remaining spaces to fill
count = 0
for i in range(0,9):
for j in range(0,9):
if board[i][j] == 'X':
count+=1
if count>0:
return 1
else:
for i in range(0,9):
print str(board[i])+"\n"
print "Thanks for playing"
def rc_checker():#checks whether inputted row and column are valid
if find_xs()==1:
for i in range(0,9):
print str(board[i])+"\n"
print "Give the row and column"
r = int(input())
c = int(input())
if r<10 and c<10:
r=r-1
c-=1
validator(r,c)
def validator(r,c):#validate whether the field is empty and redirects accordingly
if board[r][c]=='X':
print "Enter the value"
val = int(input())
if val>0 and val <10:
tf(r,c,val)
rc_checker()
else:
print "The feild is not empty please try again"
rc_checker()
def tf(r,c,val):#checking if the inputted value is repeated in corresponding row or column
i = 0
r1 = 0
c1 = 0
if val!=board[r][i] and val!=board[r][i+1] and val!=board[r][i+2] and val!=board[r][i+3] and val!=board[r][i+4] and val!=board[r][i+5] and val!=board[r][i+6] and val!=board[r][i+7] and val!=board[r][i+8] and val!=board[r1][c] and val!=board[r1+1][c] and val!=board[r1+2][c] and val!=board[r1+3][c] and val!=board[r1+4][c] and val!=board[r1+5][c] and val!=board[r1+6][c] and val!=board[r1+7][c] and val!=board[r1+8][c]:
print "Value entered is correct"
board[r][c]=val#value is set
else:
print "Invalid value Retry"
board[r][c]='X'
def main():#welcome message
print "Welcome to sudoku game"
print "Fill all 'X' with valid input"
initiator()
main()
Questions you asked:
1. How to check for squares?
2. How to reduce the size of the conditions?
3. How to generate random numbers?
4. How to make a GUI version?
Answers.
1. and 2.
First, one simple way to check these kind of conditions is:
val = 6
my_list = [2, 4, 3, 1, 'X', 7, 8, 9, 5] # this might be your row for example
if val in my_list:
print 'Invalid value Retry'
else:
print 'Value entered is correct'
Depending on the case you have to construct my_list differently:
get the list for the row
r = 3
my_list = board[r]
get the list for the column
c = 4
my_list = [row[4] for row in board]
get the list for the square
# where the squares are numbered as follows:
# 0 1 2
# 3 4 5
# 6 7 8
s = 3 # second row, first column
r = int(s/3) # r = 1
c = s%3 # c = 0
my_list = [element for row in board[r*3:r*3+3]
for element in row[c*3:c*3+3]]
Here I used 2 important features:
list comprehension
list slicing
Make sure to understand them as they are really useful.
3.
You may use the function randint
import random
random.randint(0,8) # random integer in range [0, 8], including both end points.
4.
There are many ways to do it and I don't have enough experience to tell you about. Personally I liked a lot this tutorial. It might be useful, if you want to program a simple game with GUI. Before starting a project with GUI, my advice is to learn classes.

Sudoku Solver in Python

So, I'm trying to create a function that will take in a list of lists, each consisting of 9 integers numbered 1 through 9, and return a Boolean if it is a valid solution to for a Sudoku. Right now, I've managed to solve part of the problem with checking for rows and columns, but I am stuck on the implementation of checking the three by three boxes. Here's my code so far.
alist = [1,2,3,4,5,6,7,8,9]
def checkSudoku(grid):
a = grid
b = grid
c = grid
d = False
e = False
f = False
newlist = []
for i in a:
i.sort()
if i == alist:
d = True
else:
d = False
break
for j in range(9):
for k in b:
newlist.append(k)
newlist.sort()
if i == alist:
e = True
newlist = []
else:
e = False
break
if d == True and e == True:
return True
else:
return False
Basically, My was to test all three factors necessary for it to be true, then return True if all three are True, else return false. Any help?
Not sure if this is your problem, but there is a pretty obvious problem with this code:
a = grid
b = grid
c = grid
It looks like you think this creates 3 copies of the grid, but it doesn't. It creates three different references to the same object. This means that your i.sort() on a will affect the logic on b later on.
What you should do is actually copy the object. That it's a nested list makes this a little bit tricky, but an easy way to do this is with the library function deepcopy:
a = copy.deepcopy(grid)
b = copy.deepcopy(grid)
c = copy.deepcopy(grid)
One major problem with how your code works is that you're using list.sort, meaning that grid itself changes. Consider using sorted, which works with all iterables and returns a copy instead:
for row in grid:
if sorted(row) == alist:
# now you know that the row contains all ints 1-9.
This also means you don't need to try to manually duplicate grid. If you need help duplicating a list (especially a multidimensional list) check out this question.
As for checking each 3x3 box: iterate through each of the 9 "top left" corners first like so:
for x in (0,3,6):
for y in (0,3,6):
subgrid = grid[y][x:x+3] + grid[y+1][x:x+3] + grid[y+2][x:x+3]
if sorted(subgrid) == alist:
# do your thing
For help with list slicing check this out.

find triangular numbers between any given two numbers

My code is showing bellow
import math,sys
#create a list with numbers
def create_list():
num_list=[]
for num in range(int(input("insert start point: ")),int(input("Insert end point: "))):
num_list.append(num)
return num_list
#function to find triangular numbers
def get_triangles(numlist):
triangles = []
for i in numlist:
if (check_triangle(i)):
triangles.append(i)
return triangles
#function to check number is triangular or not
def check_triangle(n):
return math.sqrt((8*n)+1).is_integer()
#function main to run the process
def main():
numlist = create_list()
print(get_triangles(numlist))
Even though it seems like the task is completed it was not. I tried it with the range of 0 - 100000000(1*10^8) numbers . it is cause to stuck my laptop any method that can complete this task ?
DO NOT PRINT A LIST THAT LARGE. Instead write it to a file, that way you can open the file afterward. The program can't efficiently write that much information into the console. I find that printing stuff to the console makes a programs a ton less efficient.
Additionally, I read some of the comments on your code and they state it isn't efficient and I would have to concur.
Here is piece of code I wrote up. It takes a bit of interpretation, but I was in a hurry. Just reply if you need help understanding it.
def getTriangles(input1,input2): #input1 is the min value and input2 is the max value
li = [] #the list where all of the numbers will go
i = 0 #an integer that acts how much another layer of the triangle would have
j = 0 #the most current number that it is on
while True: #I whipped up this algorithm in a couple minutes, so there is probably a more efficient way than just looping through all of them, but it is faster than the current one being used
i += 1 #i has to increment to act as the increase of a side
if j > input2: #if the value that could be added is greater than the max number, than just end the function and return the list
return li
if j >= input1: #if the number qualifies the minimum number requirements, then the program will add it to the list, otherwise it will ignore it and continue on with the function
li.append(j)
j += i #this simulates adding in another layer of the triangle to the bottom
This would be a way to use it:
print(getTriangles(1,45))
I trust you can look up how to write content to a file.
It appears that you are just trying to generate all triangle numbers within a range. If this is so, computing them directly is substantially quicker than checking via square root.
Note that you can generate triangular numbers by simply adding consecutive numbers.
T_0 = 0
T_1 = T_0 + 1 = 1
T_2 = T_1 + 2 = 3
T_3 = T_2 + 3 = 6
...
If you want to keep it simple, you can create a function to keep generating these numbers from n = 0, keeping them when they enter the desired range, and continue until it exceeds the upper bound.
def generate_triangular_numbers(a, b):
"""Generates the triangular numbers in [a, b] (inclusive)"""
n, t = 0, 0
triangles = []
while t < a:
n += 1
t += n
while t <= b:
triangles.append(t)
n += 1
t += n
return triangles

Categories