From 92de94183be8803bce70c15c89a077042b45cc7c Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Tue, 12 Sep 2023 22:01:31 +0200 Subject: [PATCH] application of fprettify, 2/2 For easier reading of git's reflogs and diff views, some lines were manually edited/truncated. The test and reference arrays in check.f90 were protected by "fprettify fences" to retain some visual guide; the additional white space does not affect the result of fpm test, nor the diff test launched by the Makefile. For the present project, with editors with syntax highlighting, I'm not sure if an horizontal alignment of variable declarations (and not only around `::`, as discussed on fprettify[1]) is of significant advantage here. [1] https://github.com/pseewald/fprettify/issues/157 Signed-off-by: Norwid Behrnd --- app/main.f90 | 12 +++---- src/sudoku.f90 | 62 +++++++++++++++++++----------------- test/check.f90 | 86 +++++++++++++++++++++++++++----------------------- 3 files changed, 86 insertions(+), 74 deletions(-) diff --git a/app/main.f90 b/app/main.f90 index 06724d1..03d8816 100644 --- a/app/main.f90 +++ b/app/main.f90 @@ -16,7 +16,7 @@ ! If not, see . !------------------------------------------------------------------------------ ! Contributed by Vincent Magnin, 2006-11-27; Norwid Behrnd, 2023 -! Last modifications: 2023-09-05 +! Last modifications: 2023-09-12 !------------------------------------------------------------------------------ program main @@ -25,11 +25,11 @@ program main implicit none ! Variables locales : - integer, dimension(9, 9) :: grid ! (line, column) - real(kind=dp) :: Start, End ! monitor the duration of computation + integer, dimension(9, 9) :: grid + real(kind=dp) :: Start, End ! monitor the duration of computation integer :: choice - integer :: n_empty ! number of cells to clear - character(50) :: file ! file name (including extension .txt) + integer :: n_empty ! number of cells to clear + character(50) :: file ! file name (including extension .txt) select case (command_argument_count()) case (0) ! the typical invocation with `fpm run` @@ -40,7 +40,7 @@ program main grid = 0 print *, "sudoku.f90, version 0.8.1, copyright (C) 2006 Vincent MAGNIN" - ! provide a user menue + ! provide a user menu do print * print *, "*************************** MENU *****************************************" diff --git a/src/sudoku.f90 b/src/sudoku.f90 index c161789..7234ce9 100644 --- a/src/sudoku.f90 +++ b/src/sudoku.f90 @@ -16,7 +16,7 @@ ! If not, see . !------------------------------------------------------------------------------ ! Contributed by Vincent Magnin, 2006-11-27; Norwid Behrnd, 2023 -! Last modifications: 2023-09-05 +! Last modifications: 2023-09-12 !------------------------------------------------------------------------------ module sudoku @@ -29,14 +29,14 @@ subroutine Solve_grid(grid) ! input/output parameters: integer, dimension(9, 9), intent(inout) :: grid ! local variables: - integer, dimension(9, 9) :: grid_0 ! - real(kind=dp) :: random ! random number + integer, dimension(9, 9) :: grid_0 ! empty grid + real(kind=dp) :: random ! random number integer :: row, column, line_0, row_0, i, j - integer :: counter_empty_cells ! counter of empty/non allocated cells - integer, dimension(1:81, 1:3) :: empty_cells ! list of empty cells - !logical, dimension(0:9) :: possible ! Possibility of each number - integer, dimension(1:9) :: possible_digit ! list of (still) possible numbers - integer :: counter_possible_digits ! counter of possible numbers + integer :: counter_empty_cells ! counter of empty/non allocated cells + integer, dimension(1:81, 1:3) :: empty_cells ! list of empty cells + ! logical, dimension(0:9) :: possible ! Possibility of each number + integer, dimension(1:9) :: possible_digit ! list of (still) possible numbers + integer :: counter_possible_digits ! counter of possible numbers possible_digit = 0 @@ -53,13 +53,14 @@ subroutine Solve_grid(grid) counter_empty_cells = counter_empty_cells + 1 empty_cells(counter_empty_cells, 1) = row empty_cells(counter_empty_cells, 2) = column - !call list_possible_digits(grid,row,column,empty_cells(counter_empty_cells,3),possible_digit) + ! call list_possible_digits(grid, row, column, & + ! empty_cells(counter_empty_cells,3), possible_digit) end if end do end do ! sort the empty cells: - !call Draw(empty_cells,1,counter_empty_cells) + ! call Draw(empty_cells,1,counter_empty_cells) ! iterate over all empty cells: i = 1 @@ -70,7 +71,8 @@ subroutine Solve_grid(grid) do j = i, counter_empty_cells line_0 = empty_cells(j, 1) row_0 = empty_cells(j, 2) - call list_possible_digits(grid, line_0, row_0, empty_cells(j, 3), possible_digit) + call list_possible_digits(grid, line_0, row_0, & + empty_cells(j, 3), possible_digit) end do ! retrieve the empty cells (which depends on the number of still ! possible numbers) @@ -80,7 +82,8 @@ subroutine Solve_grid(grid) line_0 = empty_cells(i, 1) row_0 = empty_cells(i, 2) - call list_possible_digits(grid, line_0, row_0, counter_possible_digits, possible_digit) + call list_possible_digits(grid, line_0, row_0, & + counter_possible_digits, possible_digit) ! if there are multiple possibilities, choose one (by chance) and ! continue with the next empty cell: @@ -103,16 +106,17 @@ subroutine Solve_grid(grid) end subroutine Solve_grid ! procedure to create a list of allowed numbers in the present empty cell: - subroutine list_possible_digits(grid, line_0, row_0, counter_possible_digits, possible_digit) + subroutine list_possible_digits(grid, line_0, row_0, & + counter_possible_digits, possible_digit) ! input parameters: integer, dimension(9, 9), intent(in) :: grid integer :: line_0, row_0 ! output parameters: - integer, dimension(1:9), intent(out) :: possible_digit ! list of possible numbers - integer, intent(out) :: counter_possible_digits ! counter of possible numbers + integer, dimension(1:9), intent(out) :: possible_digit + integer, intent(out) :: counter_possible_digits ! locale variables: integer :: row, column, cr, lr, j - logical, dimension(0:9) :: possible ! Possibility of each number + logical, dimension(0:9) :: possible ! Plausibility of each digit possible = .true. do j = 1, 9 @@ -136,7 +140,7 @@ subroutine list_possible_digits(grid, line_0, row_0, counter_possible_digits, po possible_digit(counter_possible_digits) = j end if end do - end subroutine + end subroutine list_possible_digits !**************************************************************** ! Starting from position p, sort the (still) empty cells by @@ -148,11 +152,10 @@ subroutine Draw(empty_cells, p, n) integer, intent(in) :: n ! number of empty lists integer, intent(in) :: p ! sort, start by position p (p inclusive) ! output parameters: - integer, dimension(1:81, 1:3), intent(inout) :: empty_cells ! list of empty cells + integer, dimension(1:81, 1:3), intent(inout) :: empty_cells ! local variables: - integer :: i ! loop counters - integer :: j - integer, dimension(1:3) :: column ! save + integer :: i, j ! loop counters + integer, dimension(1:3) :: column logical :: completely_solved completely_solved = .false. @@ -224,8 +227,9 @@ logical function ValidDigit(grid, row, column) i = (row - 1) / 3 j = (column - 1) / 3 - ValidDigit = ValidColumOrRow(grid(row, 1:9)) .and. ValidColumOrRow(grid(1:9, column)) & - & .and. ValidZone(grid(i * 3 + 1:i * 3 + 3, j * 3 + 1:j * 3 + 3)) + ValidDigit = ValidColumOrRow(grid(row, 1:9)) .and. & + ValidColumOrRow(grid(1:9, column)) .and. & + ValidZone(grid(i * 3 + 1:i * 3 + 3, j * 3 + 1:j * 3 + 3)) end function ValidDigit ! Note: at present it is unknown if there are Sudoku grids with less than @@ -303,7 +307,7 @@ subroutine Save_grid(grid, filename) open (newunit=fileunit, file=filename, STATUS="REPLACE") do row = 1, 9 - write (fileunit, '(3i2, " |", 3i2, " |", 3i2)') (grid(row, column), column=1, 9) + write (fileunit, '(3I2, " |", 3I2, " |", 3I2)') (grid(row, column), column=1, 9) if ((row == 3) .or. (row == 6)) then write (fileunit, *) "------+-------+------" end if @@ -330,7 +334,7 @@ subroutine Read_grid(grid, filename) open (newunit=fileunit, file=filename) do row = 1, 9 - READ (fileunit, '(3i2, a2, 3i2, a2, 3i2)') & + READ (fileunit, '(3I2, A2, 3I2, A2, 3I2)') & grid(row, 1:3), pipe1, grid(row, 4:6), pipe2, grid(row, 7:9) ! skip the lines of dashes @@ -347,7 +351,7 @@ subroutine Display_grid(grid) integer :: row, column ! line numbers and column numbers do row = 1, 9 - print '(3i2, " |", 3i2, " |", 3i2)', (grid(row, column), column=1, 9) + print '(3I2, " |", 3I2, " |", 3I2)', (grid(row, column), column=1, 9) if ((row == 3) .or. (row == 6)) then print *, "------+-------+------" end if @@ -370,8 +374,8 @@ logical function ValidColumOrRow(col) ! input parameter: integer, dimension(1:9) :: col ! local variables: - integer, dimension(0:9) :: counter ! count the occurrence of each number - integer :: row ! loop counter + integer, dimension(0:9) :: counter ! count the occurrence of each number + integer :: row ValidColumOrRow = .true. counter = 0 @@ -480,7 +484,7 @@ end function Time subroutine solver(grid, file) ! ****************************************************************** - ! proempty a solution for a partially filled grid proemptyd as a file + ! provide a solution for a partially filled grid entered as a file ! ! Concept study for a direct invocation of the executable by the CLI ! as, for example, by diff --git a/test/check.f90 b/test/check.f90 index da1626c..261768c 100644 --- a/test/check.f90 +++ b/test/check.f90 @@ -1,6 +1,6 @@ ! file: check.f90 ! date: [2023-08-24 Thu] -! edit: [2023-09-05 Tue] +! edit: [2023-09-12 Tue] ! This file contains tests to be launched by `fpm test`. @@ -22,17 +22,19 @@ subroutine assert_readtest01() array_equality = .true. - reference_grid(:, 1) = [5, 3, 0, 0, 7, 0, 0, 0, 0] - reference_grid(:, 2) = [6, 0, 0, 1, 9, 5, 0, 0, 0] - reference_grid(:, 3) = [0, 9, 8, 0, 0, 0, 0, 6, 0] + !&< + reference_grid(:, 1) = [5, 3, 0, 0, 7, 0, 0, 0, 0] + reference_grid(:, 2) = [6, 0, 0, 1, 9, 5, 0, 0, 0] + reference_grid(:, 3) = [0, 9, 8, 0, 0, 0, 0, 6, 0] - reference_grid(:, 4) = [8, 0, 0, 0, 6, 0, 0, 0, 3] - reference_grid(:, 5) = [4, 0, 0, 8, 0, 3, 0, 0, 1] - reference_grid(:, 6) = [7, 0, 0, 0, 2, 0, 0, 0, 6] + reference_grid(:, 4) = [8, 0, 0, 0, 6, 0, 0, 0, 3] + reference_grid(:, 5) = [4, 0, 0, 8, 0, 3, 0, 0, 1] + reference_grid(:, 6) = [7, 0, 0, 0, 2, 0, 0, 0, 6] - reference_grid(:, 7) = [0, 6, 0, 0, 0, 0, 2, 8, 0] - reference_grid(:, 8) = [0, 0, 0, 4, 1, 9, 0, 0, 5] - reference_grid(:, 9) = [0, 0, 0, 0, 8, 0, 0, 7, 9] + reference_grid(:, 7) = [0, 6, 0, 0, 0, 0, 2, 8, 0] + reference_grid(:, 8) = [0, 0, 0, 4, 1, 9, 0, 0, 5] + reference_grid(:, 9) = [0, 0, 0, 0, 8, 0, 0, 7, 9] + !&> call Read_grid(grid_from_file, "./test/test_in_01.txt") grid_from_file = transpose(grid_from_file) @@ -65,17 +67,19 @@ subroutine assert_readtest02() array_equality = .true. - reference_grid(:, 1) = [5, 3, 0, 0, 7, 0, 0, 0, 0] - reference_grid(:, 2) = [6, 0, 0, 1, 9, 5, 0, 0, 0] - reference_grid(:, 3) = [0, 9, 8, 0, 0, 0, 0, 6, 0] + !&< + reference_grid(:, 1) = [5, 3, 0, 0, 7, 0, 0, 0, 0] + reference_grid(:, 2) = [6, 0, 0, 1, 9, 5, 0, 0, 0] + reference_grid(:, 3) = [0, 9, 8, 0, 0, 0, 0, 6, 0] - reference_grid(:, 4) = [8, 0, 0, 0, 6, 0, 0, 0, 3] - reference_grid(:, 5) = [4, 0, 0, 8, 0, 3, 0, 0, 1] - reference_grid(:, 6) = [7, 0, 0, 0, 2, 0, 0, 0, 6] + reference_grid(:, 4) = [8, 0, 0, 0, 6, 0, 0, 0, 3] + reference_grid(:, 5) = [4, 0, 0, 8, 0, 3, 0, 0, 1] + reference_grid(:, 6) = [7, 0, 0, 0, 2, 0, 0, 0, 6] - reference_grid(:, 7) = [0, 6, 0, 0, 0, 0, 2, 8, 0] - reference_grid(:, 8) = [0, 0, 0, 4, 1, 9, 0, 0, 5] - reference_grid(:, 9) = [0, 0, 0, 0, 8, 0, 0, 7, 9] + reference_grid(:, 7) = [0, 6, 0, 0, 0, 0, 2, 8, 0] + reference_grid(:, 8) = [0, 0, 0, 4, 1, 9, 0, 0, 5] + reference_grid(:, 9) = [0, 0, 0, 0, 8, 0, 0, 7, 9] + !&> call Read_grid(grid_from_file, "./test/test_in_02.txt") grid_from_file = transpose(grid_from_file) @@ -110,32 +114,36 @@ subroutine assert_wikipedia_solution() array_equality = .true. ! Wikipedia's incomplete Sudoku grid - grid_a(:, 1) = [5, 3, 0, 0, 7, 0, 0, 0, 0] - grid_a(:, 2) = [6, 0, 0, 1, 9, 5, 0, 0, 0] - grid_a(:, 3) = [0, 9, 8, 0, 0, 0, 0, 6, 0] + !&< + grid_a(:, 1) = [5, 3, 0, 0, 7, 0, 0, 0, 0] + grid_a(:, 2) = [6, 0, 0, 1, 9, 5, 0, 0, 0] + grid_a(:, 3) = [0, 9, 8, 0, 0, 0, 0, 6, 0] - grid_a(:, 4) = [8, 0, 0, 0, 6, 0, 0, 0, 3] - grid_a(:, 5) = [4, 0, 0, 8, 0, 3, 0, 0, 1] - grid_a(:, 6) = [7, 0, 0, 0, 2, 0, 0, 0, 6] + grid_a(:, 4) = [8, 0, 0, 0, 6, 0, 0, 0, 3] + grid_a(:, 5) = [4, 0, 0, 8, 0, 3, 0, 0, 1] + grid_a(:, 6) = [7, 0, 0, 0, 2, 0, 0, 0, 6] - grid_a(:, 7) = [0, 6, 0, 0, 0, 0, 2, 8, 0] - grid_a(:, 8) = [0, 0, 0, 4, 1, 9, 0, 0, 5] - grid_a(:, 9) = [0, 0, 0, 0, 8, 0, 0, 7, 9] + grid_a(:, 7) = [0, 6, 0, 0, 0, 0, 2, 8, 0] + grid_a(:, 8) = [0, 0, 0, 4, 1, 9, 0, 0, 5] + grid_a(:, 9) = [0, 0, 0, 0, 8, 0, 0, 7, 9] + !&> call Solve_grid(grid_a) ! this fills (hence modifies) grid_a ! Wikipedia's complete Sudoku grid - grid_b(:, 1) = [5, 3, 4, 6, 7, 8, 9, 1, 2] - grid_b(:, 2) = [6, 7, 2, 1, 9, 5, 3, 4, 8] - grid_b(:, 3) = [1, 9, 8, 3, 4, 2, 5, 6, 7] - - grid_b(:, 4) = [8, 5, 9, 7, 6, 1, 4, 2, 3] - grid_b(:, 5) = [4, 2, 6, 8, 5, 3, 7, 9, 1] - grid_b(:, 6) = [7, 1, 3, 9, 2, 4, 8, 5, 6] - - grid_b(:, 7) = [9, 6, 1, 5, 3, 7, 2, 8, 4] - grid_b(:, 8) = [2, 8, 7, 4, 1, 9, 6, 3, 5] - grid_b(:, 9) = [3, 4, 5, 2, 8, 6, 1, 7, 9] + !&< + grid_b(:, 1) = [5, 3, 4, 6, 7, 8, 9, 1, 2] + grid_b(:, 2) = [6, 7, 2, 1, 9, 5, 3, 4, 8] + grid_b(:, 3) = [1, 9, 8, 3, 4, 2, 5, 6, 7] + + grid_b(:, 4) = [8, 5, 9, 7, 6, 1, 4, 2, 3] + grid_b(:, 5) = [4, 2, 6, 8, 5, 3, 7, 9, 1] + grid_b(:, 6) = [7, 1, 3, 9, 2, 4, 8, 5, 6] + + grid_b(:, 7) = [9, 6, 1, 5, 3, 7, 2, 8, 4] + grid_b(:, 8) = [2, 8, 7, 4, 1, 9, 6, 3, 5] + grid_b(:, 9) = [3, 4, 5, 2, 8, 6, 1, 7, 9] + !&> ! comparison of computed solution with Wikipedia's reference solution outer: do i = 1, 9