List out of range whilst using recursion - python

Currently making Sudoku in my CompSci class, I'm using recursion to check my rows and make sure that there are no duplicates (I'll do columns and 3x3's later) but I'm running into an issue of my index going out of range. Now I know that means that it's going outside of the list but for some reason I can't figure out how it is.
Here is my recursion method for covering all of the rows. I'm importing a default 0 as the row initially and therefore is goes from 0 all the way to 8 and when it reaches 8, it'll return(essentially a break).
def winCheck(self, row):
if row == 8:
return ''
for num in range (1,10): #for EACH values 1-9
count = 0
for j in range(9):
if (self.board[row][j] == str(num)):
count += 1
if (count!=1): #MUST have a count of 1
print("wrong!")
return False
elif count == 1:
print("good!")
row= row + 1
game.winCheck(row)
return True
Here is my list for my playing board and the original board (those can't be modified)
for i in range(9):
self.board[i] = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ]
self.origBoard[i] = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ]
# [[' ', '1', '9', '3', '7', '4', '6', '5', '2'],
# ['5', '7', '6', '1', '8', '2', '9', '4', '3'],
# ['3', '4', '2', '5', '9', '6', '7', '1', '8'],
# ['9', '2', '1', '7', '5', '3', '8', '6', '4'],
# ['6', '3', '8', '4', '1', '9', '5', '2', '7'],
# ['4', '5', '7', '6', '2', '8', '1', '3', '9'],
# ['1', '8', '5', '2', '3', '7', '4', '9', '6'],
# ['7', '6', '3', '9', '4', '1', '2', '8', '5'],
# ['2', '9', '4', '8', '6', '5', '3', '7', ' ']]
#raw data set for testing win conditions

game.winCheck(row) --> return game.winCheck(row)

Related

Python, If statement returning true for e==' ' sometimes even when e doesn't equal ' '

I wrote a function to separate a string of time and date into two strings, that string always comes with space between them so I thought I would append and pop the elements one by one until I reach the space.
This is the code:
def seprate_data_time(inlist):
timelist= list(inlist)
datelist=[]
#split Date and Time based on the space ' '
print('>>>>>>>>')
for e in timelist:
print(timelist)
if e==' ':
break
datelist.append(timelist[0])
timelist.pop(0)
return [''.join(datelist),''.join(timelist)]
which does work for like 10 loops then stops working like this:
#This is an example of the correct ones
'1', '/', '3', '1', '/', '2', '0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['/', '3', '1', '/', '2', '0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['3', '1', '/', '2', '0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['1', '/', '2', '0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['/', '2', '0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['2', '0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['0', '2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['2', '3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
['3', ' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
[' ', '6', ':', '5', '8', ':', '4', '1', ' ', 'P', 'M']
>>>>>>>>
#This is an example of the issue is stopping even before reaching the ' '
['2', '/', '1', '/', '2', '0', '2', '3', ' ', '1', '0', ':', '3', '0', ':', '2', '6', ' ', 'A', 'M']
['/', '1', '/', '2', '0', '2', '3', ' ', '1', '0', ':', '3', '0', ':', '2', '6', ' ', 'A', 'M']
['1', '/', '2', '0', '2', '3', ' ', '1', '0', ':', '3', '0', ':', '2', '6', ' ', 'A', 'M']
['/', '2', '0', '2', '3', ' ', '1', '0', ':', '3', '0', ':', '2', '6', ' ', 'A', 'M']
['2', '0', '2', '3', ' ', '1', '0', ':', '3', '0', ':', '2', '6', ' ', 'A', 'M']
>>>>>>>>
I tried a few things like switching the ' ' to 'S' but no luck.
Also tried putting the popping and appending into else also didn't work

How do I join string together in a list

gene=[]
x=1
path = '/content/drive/MyDrive/abc.txt'
file = open(path,'r').readlines()
for i in file[x]:
i.split('\t')[0]
gene.append(i)
x+=1
print(gene)
my output is
['A', 'H', 'Y', '3', '9', '2', '7', '8', '\t', '1', '4', '5', '.', '5', '4', '4', '\t', '1', '3', '5', '.', '2', '4', '\t', '5', '6', '.', '5', '1', '3', '8']
but i want it to split at '/t', taking the first element in the list which is AHY392978 and append it into the list gene
['AHY39278', 'AHY39278']
any idea how to join the elements together?
add this code to your project:
string = ''
for(i in range len(gene)):
string += gene[i]
you can get your data from string

Replace numbers after a character using re.sub() on a list

I have a list of numbers stored in a list and I want to delete the all the numbers after .
The list is,
['08-52-05.173735', '09-01-22.68835', '09-10-34.145061',] and I want to delete everything after ..Below is the code I am using,
ignore_ms = [re.sub(r'(?<=\.).*$', ' ', y) for y in timestamp]
print (ignore_ms)
where timestamp is the list above. However, the result I get is,
['[', "'", '0', '8', '-', '5', '2', '-', '0', '5', '. ', '1', '7', '3', '7', '3', '5', "'", ',', ' ', "'", '0', '9', '-', '0', '1', '-', '2', '2', '. ', '6', '8', '8', '3', '5', "'", ',', ' ', "'", '0', '9', '-', '1', '0', '-', '3', '4', '. ', '1', '4', '5', '0', '6', '1', "'", ',', ' ',
Whereas the result I want is 08-52-05., 09-01-22., 09-10-34.
Any idea what is wrong with the code above?
Thanks.
Try using str.split:
l = ['08-52-05.173735', '09-01-22.68835', '09-10-34.145061']
print([i.split('.')[0]+'.' for i in l])
You could instead go with a simpler approach avoiding regular expressions with:
l = ['08-52-05.173735', '09-01-22.68835', '09-10-34.145061',]
[i[:i.rfind('.')] for i in l]
# ['08-52-05', '09-01-22', '09-10-34']

How to check columns and subgrids (Sudoku Checker in Python)?

I'm doing the traditional "Sudoku Puzzle Checker" activity, and I've only been able to figure out how to check the rows so far. As per instruction, I'm suppose to check (in 3 separate functions), the rows, columns, and subgrids (the 3x3 squares) of a solved sudoku puzzle (given in a .csv file). I understand how to do the columns (add 9 each time, 9 times starting numbers ranging from 0-8) and the subgrids (range of 3 numbers, add 6 to get to the next range of 3 numbers e.t.c) but I'm confused as how to implement these concepts into my code. Here's what I have right now:
def load_puzzle(filename):
file = open(filename, 'r')
puzzle = list(file)
#the puzzle currently looks like this:
['5,3,4,6,7,8,9,1,2\n', '6,7,2,1,9,5,3,4,8\n', '1,9,8,3,4,2,5,6,7\n', '8,5,9,7,6,1,4,2,3\n', '4,2,6,8,5,3,7,9,1\n', '7,1,3,9,2,4,8,5,6\n', '9,6,1,5,3,7,2,8,4\n', '2,8,7,4,1,9,6,3,5\n', '3,4,5,2,8,6,1,7,9\n']
print_puzzle(puzzle)
def print_puzzle(puzzle):
print(" ")
count = 0
while count <= 8:
line = list(puzzle[count])
line = [x for x in line if x != ',']
line.remove('\n')
line.insert(3, '|')
line.insert(7, '|')
if count == 3 or count == 6:
print("---------------------")
fresh = ' '.join(line)
count += 1
print('''
Checking puzzle...
''')
delimiter = ','
together = delimiter.join(puzzle)
new = list(together)
new[:] = [x for x in new if x != ',']
new[:] = [x for x in new if x != '\n']
check_rows(new)
#the puzzle now looks like this:
['5', '3', '4', '6', '7', '8', '9', '1', '2', '6', '7', '2', '1', '9', '5', '3', '4', '8', '1', '9', '8', '3', '4', '2', '5', '6', '7', '8', '5', '9', '7', '6', '1', '4', '2', '3', '4', '2', '6', '8', '5', '3', '7', '9', '1', '7', '1', '3', '9', '2', '4', '8', '5', '6', '9', '6', '1', '5', '3', '7', '2', '8', '4', '2', '8', '7', '4', '1', '9', '6', '3', '5', '3', '4', '5', '2', '8', '6', '1', '7', '9']
def check_numbers(numbers):
my_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
numbers.sort()
if numbers == my_list:
return True
else:
return False
def check_rows(n):
count = 0
start = 0
while count <= 8:
row = n[start:(start + 9)]
if check_numbers(row) is True:
count += 1
start += 9
elif check_numbers(row) is False:
break
if count == 9:
print("Rows okay...")
else:
print("Error in row", count)
check_columns(n)
def check_columns(n):
count = 0
start = 0
while count <= 8:
col = n[start::9]
if check_numbers(col) is True:
count += 1
start += 1
elif check_numbers(col) is False:
break
if count == 9:
print("Columns okay...")
else:
print("Error in row", count)
def check_subgrids(n):
filename = input("Enter file name of solved sudoku puzzle: ")
load_puzzle(filename)
I'm looking for a way to find the columns and subgrids similar to how I found the rows (i.e. row = n[start:(start + 9)]) if that's possible. I'm a beginner, so I'm also happy to learn about different tools/ways I could improve my code!
Edit: I've solved my problem with the check_columns function (code updated) but still would appreciate some guidance for the check_subgrid function!

Search a list and split

First of all sorry for the very basic question but I haven't been able to find an example with the end results I need. In the following example I have a list with numbers and sometimes the numbers are joined like index 2 and 5 in the following list.
list = ['1', '2', '3, 4', '5', '6', '7, 8', '9', '10']
I would like to search the list and split when it finds a comma so the end result is the following:
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
What would be the easiest way to accomplish this?
>>> [x for y in ['1', '2', '3, 4', '5', '6', '7, 8', '9', '10'] for x in y.split(', ')]
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
An alternative method is:
>>> my_list = ['1', '2', '3, 4', '5', '6', '7, 8', '9', '10']
>>> sum((item.split(', ') for item in my_list), [])
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
or
>>> ', '.join(my_list).split(', ')
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

Categories