Geely written test - programming code questions

Q1:

Suppose there is a table with n rows and m columns, which can be divided into n✖️m blocks.

  • There is a diagonal line connecting from the upper left corner to the lower right corner. All the blocks in the table that touch the diagonal line are white, that is, the diagonal line will pass through the small blocks in the table.
  • Input the size of the table and output the number of white blocks.

A rectangle with n rows and m columns can be regarded as divided into n✖️m square blocks of size one. Count the number of passing blocks on the output diagonal.

For example: enter 3, 4. should output 6

  • If eithern or m is1, then the diagonal will only pass through one block, that is, n * m.
  • Otherwise, the diagonal line will pass through the first row of the rectangle, the last row< /span>. Therefore, the number of passing blocks on the diagonal can be calculated as: The remaining blocks will only be counted once. four lines will be counted repeatedly, the blocks on these last column and first column,
    • Number of diagonal blocks = n + m − 1 − g c d ( n , m ) + 1 n + m - 1 - gcd(n, m) + 1 n+m1gcd(n,m)+1
    • Among them,gcd(n, m)DisplayThe greatest common number of the sum of n.
import math

def count_diagonal_blocks(n, m):
    if n == 1 or m == 1:
        return n * m
    else:
        return n + m - math.gcd(n, m)

# 输入表格的大小
n = 3
m = 4

# 计算对角线上经过的块的数量
diagonal_blocks = count_diagonal_blocks(n, m)
print("对角线上经过的块的数量:", diagonal_blocks)


Q2:

Enter a list of strings representing a maze.

  • Among them, j represents the wall, 空格 represents entry, and k represents the starting position of the person.
  • People can only move left or right or up or down.
  • Note that the maze does not have to be square.
  • Please output true to indicate that people can come out, otherwise output false.

For example, enter ["j jj", "j kj", "jjjj"]. output true

If there is only one element k, it will also output true
If the outer circle is all wall j, it will also output false

def can_escape_maze(maze):
    if not maze:
        return False

    def dfs(x, y):
        if x < 0 or x >= len(maze) or y < 0 or y >= len(maze[0]) or maze[x][y] == 'j':
            return False

        if maze[x][y] == ' ':
            return True

        maze[x] = maze[x][:y] + 'j' + maze[x][y + 1:]

        if (dfs(x + 1, y) or
            dfs(x - 1, y) or
            dfs(x, y + 1) or
            dfs(x, y - 1)):
            return True

        return False

    # 检查外围一圈是否都是墙
    for i in range(len(maze)):
        if i == 0 or i == len(maze) - 1:
            for j in range(len(maze[i])):
                if maze[i][j] != 'j':
                    return False
        else:
            if maze[i][0] != 'j' or maze[i][-1] != 'j':
                return False

    # 找到起始位置
    for i in range(1, len(maze) - 1):
        for j in range(1, len(maze[i]) - 1):
            if maze[i][j] == 'k':
                return dfs(i, j)

    return False

# 输入迷宫
maze = [
    "jjjjjjjj",
    "j j jj j",
    "j jkj  j",
    "j j j jj",
    "j j j  j",
    "j      jj",
    "jjjjjjjj"
]

# 检查是否可以走出迷宫
result = can_escape_maze(maze)
print(result)


Q3:

Input an n✖️n-sized array, which needs to be output in clockwise order from from outside to inside.

  • Example, Import [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] [[1 ,2,3] ,[4,5,6],[7,8,9]] [[1,2,3],[4,5,6],[7,8,9]] 输出应该为 [ 1 , 2 , 3 , 6 , 9 , 8 , 7 , 4 , 5 ] [1,2,3,6,9,8,7,4,5] [1,2,3,6,9,8,7,4,5]

Traverse the outer elements of the matrix in sequence in a clockwise direction from outside to inside, and then Traverse inward layer by layer

def spiral_order(matrix):
    if not matrix:
        return []

    result = []
    while matrix:
        # 从左到右
        result += matrix[0]

        # 从上到下
        if len(matrix) > 1:
            for row in matrix[1:-1]:
                if row:
                    result.append(row[-1])

        # 从右到左
        if len(matrix) > 1:
            result += matrix[-1][::-1]

        # 从下到上
        if len(matrix) > 2:
            for row in matrix[-2:0:-1]:
                if row:
                    result.append(row[0])

        # 剔除已经遍历的外围元素
        matrix = matrix[1:-1]
        for i in range(len(matrix)):
            if matrix[i]:
                matrix[i] = matrix[i][1:-1]

    return result

# 输入矩阵
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# 获取顺时针排列的结果
result = spiral_order(matrix)
print(result)

Guess you like

Origin blog.csdn.net/weixin_43338969/article/details/134090094