Question: I need the following python code in java pleaseeeee: def puzzle_to_string(puzzle): ''' Takes a puzzle and prints it out in a nice beautiful artistic way
I need the following python code in java pleaseeeee:
def puzzle_to_string(puzzle): ''' Takes a puzzle and prints it out in a nice beautiful artistic way puzzle_to_string(puzzle : list[list[int]]) -> str Empty cells (0s) are denoted by a space ' ' ''' result = "" for r in range (0,9): if (r%3==0) and r != 0: # This finds where to separate the subgrids horizontally result +="------+-------+------ " for c in range (0,9): if (c%3==0) and c != 0: # This finds where to separate the subgrids vertically result += "| " if puzzle[r][c] == 0: result += " " + " " else: result += str(puzzle[r][c]) + " " result += " " return result
def check_rows(puzzle): ''' Checks the given puzzle for errors in rows (non-integers, repeated intergers and if the int is r>=1 and r<=9) and if there are any, adds the index of the row to a list. check_rows(puzzle : list[list[int]]) -> list[int] ''' errors = [] for r in range(0,9): numbers = [] # This keeps track of which numbers we've already come across in this row for c in range(0,9): if puzzle[r][c] != 0: if (str(puzzle[r][c]).isdigit() is True) and puzzle[r][c]>=1 and puzzle[r][c]<=9 and (puzzle[r][c] not in numbers): numbers.append(puzzle[r][c]) else: errors.append(r) break return errors
def check_columns(puzzle): '''Checks the given puzzle for errors in columns(non-integers, repeated intergers and if the int is c>=1 and c<=9) and if there are any, adds the index of the column to a list. check_columns(puzzle : list[list[int]]) -> list[int] ''' errors = [] for c in range(0,9): numbers = [] # This keeps track of which numbers we've already come across in this column for r in range(0,9): if puzzle[r][c] != 0: if (str(puzzle[r][c]).isdigit() is True) and puzzle[r][c]>=1 and puzzle[r][c]<=9 and (puzzle[r][c] not in numbers): numbers.append(puzzle[r][c]) else: errors.append(c) break return errors def check_subgrid(puzzle, idx): ''' A helper function for check_subgrids() that determines the validity of a singular subgrid, given the subgrid index check_subgrid(puzzle : list[list[int]], idx: int) -> bool ''' column = (idx%3)*3 # finds the starting column of a given subgrid row = idx - (idx%3) # finds the starting row of a given subgrid numbers = [] for r in range(row, row+3): for c in range(column, column+3): if puzzle[r][c] != 0: if (str(puzzle[r][c]).isdigit() is True) and puzzle[r][c]>=1 and puzzle[r][c]<=9 and (puzzle[r][c] not in numbers): numbers.append(puzzle[r][c]) else: return False return True
def check_subgrids(puzzle): ''' Checks validity of all subgrids in the puzzle by using the check_subgrid() check_subgrids(puzzle : list[list[int]]) -> list[int] ''' errors = [] for i in range(0,9): if check_subgrid(puzzle, i) is False: errors.append(i) return errors
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
