Location>code7788 >text

Hannukah and recursion

Popularity:889 ℃/2024-08-30 08:48:09

catalogs
  • Requirements context, constraints, simplification
  • Steps to simulate plate movement
  • Recursive implementation of Code
  • analyze
  • Exercise 1


Requirements context, constraints, simplification

A Hannukah Tower is a toy that consists of columns and plates, and it has some play restrictions, mainly stipulating that there is a limit to the movement of the plates.
Trying to get to the essence of recursion, Hannukah is a good vehicle.
How?
As the plate moves.

# The number of trays:
# Usually, we assume that there are n plates. The plates are initially stacked on a column in descending order (source column).
# Numbers are used here to denote plates, and the size of the number denotes the size of the plate.

# Number of columns:
# There are three columns in total: the source column (the starting column), the target column (the final column), and the auxiliary column (the middle column).
# The a,b,c list is used to simulate the three columns.
# Specify that the list can only be retrieved from the end

# Move rules:
# Only one disk can be moved at a time.
# Only one plate can be placed on a column at any one time. #
# A larger plate cannot be placed on a smaller plate. # A larger plate cannot be placed on a smaller plate. # A larger plate cannot be placed on a smaller plate.

# Target:
# Move all trays from the source column to the target column with the above rules.

Summary:

  1. Problems and goals are understood;
  2. The real problem is drawn out, simplified, and expressed in a symbolic way;

Steps to simulate plate movement

Let's start with a normal demonstration of a move:

n = 1
a = [1]
b = []
c = []

a = []
b = []
c = [1]

n = 2
a = [2,1]
b = []
c = []

(1)
a = [2]
b = [1]
c = []

(2)
a = []
b = [1]
c = [2]

(3)
a = []
b = []
c = [2,1]

**

n = 3
a = [3,2,1]
b = []
c = [1]

(1)
a = [3,2]
b = []
c = [1]

(2)
a = [3]
b = [2]
c = [1]

(3)
a = [3]
b = [2,1]
c = []

(4)
a = []
b = [2,1]
c = [3]

(5)
a = [1]
b = [2]
c = [3]

(6)
a = [1]
b = []
c = [3,2]

(7)
a = []
b = []
c = [3,2,1]


The above positive thinking, n is about large, the more steps, more and more chaotic, can not grasp the focus.
One must be able to think in reverse:

n = 3
a = [3]
b = [2,1]
c = []

a = []
b = [2,1]
c = [3]

**

n = 4
a = [4]
b = [3,2,1]
c = []

a = []
b = [3,2,1]
c = [4]

**
n = 5
a = [5]
b = [4,3,2,1]
c = []

a = []
b = [4,3,2,1]
c = [5]
......

Simple capture of some fixed features:

  • Whatever n is, it is a to c.

  • If n>1, put all n-1 disks in the auxiliary column, and n goes directly to c. (This is also divided into a subproblem)

Recursive implementation of Code

def hanoi(n: int, a: list, b: list, c: list).
    print("sub State:", a, b, c)
    if n == 1.
        # Base case
        (())
        print("State 1:", a, b, c)
    else.
        # Recursive case
        hanoi(n - 1, a, c, b)

        (())
        print("State 2:", a, b, c)

        hanoi(n - 1, b, a, c)


a = [3, 2, 1]
b = []
c = []

print("Initial state:", a, b, c)
hanoi(len(a), a, b, c)
print("Final state:", a, b, c)

Core:
In a recursive call, the roles of the columns (source, target, auxiliary) are constantly exchanged. This swapping of roles ensures that the movement of the disk is done correctly at each step.

out:

Initial state: [3, 2, 1] [] []
sub State: [3, 2, 1] [] []
sub State: [3, 2, 1] [] []
sub State: [3, 2, 1] [] []
State 1: [3, 2] [] [1]
State 2: [3] [1] [2]
sub State: [1] [3] [2]
State 1: [] [3] [2, 1]
State 2: [] [2, 1] [3]
sub State: [2, 1] [] [3]
sub State: [2, 1] [3] []
State 1: [2] [3] [1]
State 2: [] [1] [3, 2]
sub State: [1] [] [3, 2]
State 1: [] [] [3, 2, 1]
Final state: [] [] [3, 2, 1]

analyze

Visualization was used to observe:
/

Some of the lessons learned from watching it:

  • Functions can be nested functions, multiple nested functions together to form a whole function;
    image

  • Recursion passes down the variables in the(after a verb of motion indicates movement away from the speaker) classifier for a chapter in old Chinese fictional novelsThe change between them can have a very bombastic effect;

  • Recursive layer sinking is typically controlled using an int variable.

Exercise 1

You have a data structure that holds employee information, which contains the employee's unique id, importance, and the id of the immediate subordinate.

Given an array employees, where:

  • employees[i].id is the ID of the ith employee.
  • employees[i].importance is the importance of the ith employee.
  • employees[i].subordinates is a list of the IDs of the direct subordinates of the ith employee.

Given an integer id representing the ID of an employee, return the sum of the importance of this employee and all his subordinates.

image

# Definition for Employee.
class Employee:
    def __init__(self, id: int, importance: int, subordinates: List[int]):
         = id
         = importance
         = subordinates