python prints odd cells such as 9 cells and 25 cells, and adds them horizontally and vertically to make them equal.

Keywords: Python

I believe that everyone has played the game of nine squares. The rule is to fill in the numbers 1-9 in nine squares, so that the horizontal and vertical angles are added and equal. It may be easy to fill in only nine ones, but we need to find the rules when we fill in 25, 49, 81 and other odd ones. How to use code to find rules?

Before you look at the code, let's take a look at this formula:

The first place is in the right center of the upward line. It is slanted to the right and top in turn. When the box is up and out, it is written down. When the box is right and out, it is placed on the left. The row weight is returned to the lower grid to fill in. The top right row is a sample.

Explanation: 1. Put it in the center of the first line, and fill in the value in the up format. If it exceeds the top border, move it vertically down to the last line. If it exceeds the right border, move it horizontally to the left. If there is a value in the place to be moved, put it under the previous number. If both the top border and the right border exceed , just like arranging weights, put this under the previous number. The process is shown in the figure below

The code is as follows:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    num = int(input('Please enter an odd number:'))
    # Define a list with a length of num
    high = [[0] * num ]
    # Define a list of num*num, and then assign values to it
    for i in range(num-1):
       high += [[0]*num]
    n = 1
    # First, make sure the middle value of the first row is 1
    high[0][num//2] = n
    x = 0
    y = num//2
    # Assign values to the high list in sequence starting with 2
    for j in range(1,num*num):
       # x is the row, y is the column, j is the value of x, y coordinate
       j = j + 1
       x = x - 1 
       y = y + 1
       # Determine which possibilities are met
       if y > (num - 1) and x < 0 :
          x = x + 2
          y = y - 1
          high[x][y]=j
       elif x < 0 :
                                     //I also collated more Python learning materials
    
                                           //QQ group  688244617
   
                                     //There are also small partners in the group to communicate and study with you.
           x = num - 1 
          high[x][y]=j
       elif y > num-1 :
          y = 0
          high[x][y]=j
       else:
          if high[x][y] == 0 :
             high[x][y]=j
          elif high[x][y] != 0 :
             x = x + 2
             y = y - 1
             high[x][y]=j
    # Print out the values of the high list in turn
    for a in range(num):
       for b in range(num):
          # rjust means output 01, 02, 03 and so on. See my last blog introduction.
          print( str(high[a][b]).rjust(2,'0'),end=' ') 
       print()

Train of thought:


It's mainly to understand the top part of the formula. If you understand the formula, you can understand the code. If you don't understand, please leave a message.

Posted by rocksolidsr on Tue, 22 Oct 2019 08:32:05 -0700