PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] Why does this code not read the sudoku numbers from csv file and reconstruct it?

 August 29, 2022     csv, python, recursion, sudoku     No comments   

Issue

This print_board is the code to reconstruct the the sudoku. it works perfectly fine when I use the sudoku numbers from the board, but it doesn't work from the sudoku.csv file. What have I done wrong?

""" Sudoku.csv = 
7;8;0;4;0;0;1;2;0
6;0;0;0;7;5;0;0;9
0;0;0;6;0;1;0;7;8
0;0;7;0;4;0;2;6;0
0;0;1;0;5;0;9;3;0
9;0;4;0;6;0;0;0;5
0;7;0;3;0;0;0;1;2
1;2;0;0;0;7;4;0;0
0;4;9;2;0;6;0;0;7 
"""

 #these are the sudoku numbers on the sudoku.csv file 


import csv
with open('sudoko.csv') as f:
    #next(f) 
    board = csv.reader(f,delimiter=';')
    for row in board:
        print(row)

#this is the code to read the sudoku.csv file.

def print_board(bo):
    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")

        for j in range(len(bo[0])):
            if j % 3 == 0 and j != 0:
                print(" | ", end="")

            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j]) + " ", end="")              
print(print_board(row))

Solution

There are multiple things that don't work.

  1. around the print_board function there should be no print function.
  2. don't pass a row to print_board, but the whole board
  3. rather use numpy to read csv. Then you get the board as an array

Here is a working example.

(Since I'm not sure how exactly your CSV looks like, you may have to change the delimiter argument)

import numpy as np

# change delimiter depending on your csv file
board = np.loadtxt('sudoko.csv', delimiter=";")

def print_board(bo):

    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")
        for j in range(len(bo[0])):
        
            if j % 3 == 0 and j != 0:
                print(" | ", end="")
            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j]) + " ", end="")


print_board(board)

So this is the output:

7.0 8.0 0.0  | 4.0 0.0 0.0  | 1.0 2.0 0.0
6.0 0.0 0.0  | 0.0 7.0 5.0  | 0.0 0.0 9.0
0.0 0.0 0.0  | 6.0 0.0 1.0  | 0.0 7.0 8.0
- - - - - - - - - - - - - 
0.0 0.0 7.0  | 0.0 4.0 0.0  | 2.0 6.0 0.0
0.0 0.0 1.0  | 0.0 5.0 0.0  | 9.0 3.0 0.0
9.0 0.0 4.0  | 0.0 6.0 0.0  | 0.0 0.0 5.0
- - - - - - - - - - - - - 
0.0 7.0 0.0  | 3.0 0.0 0.0  | 0.0 1.0 2.0
1.0 2.0 0.0  | 0.0 0.0 7.0  | 4.0 0.0 0.0
0.0 4.0 9.0  | 2.0 0.0 6.0  | 0.0 0.0 7.0


Answered By - Mario
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing