Is it good practice to use classes whenever you can? [closed]

  python

I making this sudoku program where you can play it and it would also solve it for you. So I far I have finished the part where it will randomly generate boards so you will be able to play it. I only used functions. I don’t think I need to use classes because all my functions just do specific things, but I am not sure. Should I put my code in classes and clean it up? I just want to know what the good practices are and how I can make my code better.

Here is my code

import random
import numpy as np


# populates a row in random spaces
def populate():
    row = [0] * 9
    num_in_box = random.randint(3, 6)
    while True:
        spot = random.randint(0, 8)
        r = random.randint(1, 9)
        if r not in row:
            row[spot] = r
        if row.count(0) == (9 - num_in_box):
            break
    return row


# populates a grid in random spaces - has duplicates in column, row and box
mapped = list()
for x in range(9): mapped.append(populate())


# checks every number in column and row and returns numbers in list
def col_row_nums(array, row, col):
    check_col = [j for j in array[:, col] if j != 0]
    check_row = [i for i in array[row] if i != 0]
    if array[row][col] in check_col:
        check_col.remove(array[row][col])
    return check_col + check_row


# checks every number box and returns numbers in list
def box_nums(array, box_row):
    reshaped_box = np.reshape(array, (27, 3))
    list_boxes_in_rows = list()
    for a in range(3):
        for b in range(3):
            for c in range(3):
                p2 = list(np.reshape((reshaped_box[c::3]), (3, 9)))
                for d in range(3): list_boxes_in_rows.append(p2[a])
    array_boxes_in_rows = np.array(list_boxes_in_rows)
    return [k for k in array_boxes_in_rows[box_row] if k != 0]


# Basically goes through each number and removes any duplicates so each column, row and box all have only one set of numbers 1 - 9
def finalize_board():
    box_rows_num = -1
    for x in range(9):
        for y in range(9):
            box_rows_num += 1
            arr = np.array(mapped)
            possible_nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
            col_row_duplicates = list()
            box_duplicates = list()
            used_nums = set(col_row_nums(arr, x, y)) | set(box_nums(arr, box_rows_num))

            for w in used_nums:
                col_row_count = col_row_nums(arr, x, y).count(w)
                box_count = box_nums(arr, box_rows_num).count(w)
                if col_row_count > 1: col_row_duplicates.append(w)
                if box_count > 1: box_duplicates.append(w)
            if mapped[x][y] in col_row_duplicates or mapped[x][y] in box_duplicates:
                remaining_nums = list(set(possible_nums) - set(used_nums))
                if len(remaining_nums) > 0: mapped[x][y] = random.choice(remaining_nums)
    return np.array(mapped)


print(finalize_board())

Here are some sample boards after running the program. The 0’s are empty spots.

[[9 1 0 3 0 4 0 5 0]
 [4 7 8 0 0 0 9 0 0]
 [9 3 2 0 7 5 0 6 0]
 [0 0 4 9 0 0 8 0 0]
 [7 0 9 5 0 1 0 2 6]
 [6 5 1 0 2 3 0 0 4]
 [1 0 0 6 0 0 0 8 3]
 [5 2 0 4 3 0 6 0 0]
 [0 0 3 0 5 8 7 0 0]]
[[4 0 9 0 5 0 1 0 0]
 [0 0 0 8 0 3 9 5 0]
 [5 0 8 4 0 0 0 2 0]
 [0 4 1 9 7 0 0 0 0]
 [0 0 2 0 0 1 0 8 7]
 [0 7 0 6 8 0 4 0 9]
 [7 8 0 1 3 0 2 0 5]
 [0 5 6 0 0 0 0 7 0]
 [1 0 3 5 0 4 0 0 0]]
[[0 2 0 0 9 7 0 8 0]
 [0 0 7 1 0 3 5 0 9]
 [9 3 0 5 6 2 0 0 4]
 [0 0 5 0 1 0 0 9 0]
 [0 4 0 7 8 9 2 0 6]
 [7 0 0 2 4 0 0 3 0]
 [0 0 0 3 0 0 1 4 8]
 [0 0 0 0 0 4 0 6 3]
 [3 1 2 0 0 8 9 7 0]]

Source: Python Questions

LEAVE A COMMENT