Task03: Stack and Recursion

1. Definitions and stack operation

1.1 Definitions stack

Insertion (push) and deletion (popping) operations only linear form at one end (top of the stack). That is, after an advanced (First In Last Out) linear form.
An analog sequence table stack, the stackSingle chain analog stack, the stack

1.2 Stack operation

  • Stack operations: inserting the data element value stack.
  • Pop operations: removing the stack of data elements.
  • It is empty: determining whether the stack contains data elements.
  • To give a deep stack: Get the number of the stack contains the actual data elements.
  • Clear: Remove all data elements in the stack.
  • Get the top element.

1.3 stack implementation (python)

class Stack:
  """模拟栈"""
  def __init__(self):
    self.items = []
     
  def isEmpty(self):
    return len(self.items)==0
   
  def push(self, item):
    self.items.append(item)
   
  def pop(self):
    return self.items.pop()
   
  def peek(self):
    if not self.isEmpty():
      return self.items[len(self.items)-1]
     
  def size(self):
    return len(self.items)
s=Stack()
print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())

2. Recursion

If a function calls itself within itself, this function is a recursive function.

Tower of Hanoi problem

We can assume that a column other than the lowermost plate, the column has been successfully a top plate 63 moves to the b-pillar, then we again just to the bottom of a plate column c is moved to column.

When we made the biggest move a column tray column c, b column is the remaining 63 dishes, a column is empty. So the goal now becomes these 63 plates of b c column to move the column. This question and the original question exactly the same, just change the column to a column b, the size of the 64 turns 63. Thus the same method can be used, the above first plate 62 to move a column from the column b, c and then moved to the lowermost column plate.

In this interpolation, then b is a buffer column, move a column above the uppermost disc 61 in the disc 62 to column b, c and finally moved to a disk column.

We have found that the law, we have the a or b are each in a column buffer, and then moving the disk other than the first except the lowermost disc to the auxiliary column, then the lowermost disc shift to the c-pillar, this process repeats.

This process is repeated recursively mobile disc, for example, every time we want to solve the problem of n mobile disk, we must first solve the (n-1) The same issue carried a plate operation.

You can then write a function, move (n, a, b , c). It can be understood: move (the number of plates, the starting point, buffering, end).
1. On a plate of a case where only directly moved to C, the code is as follows :

if n == 1:
    print(a, '-->', c)

There is more than a case where the plate A 2. :

首先,需要把 n-1 个盘子搬到 b 柱子缓冲。打印出的效果是:a --> b。

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

再把最大的盘子搬到 c 柱子,也是最大尺寸的一个。打印出:a–>c。

move(1, a, b, c)

最后,把剩下 b 柱的 n-1 个盘子搬到 c 上,此时缓冲变成了起点,起点变成了缓冲。

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

利用 Python 实现汉诺塔问题

i = 0


def move(n, a, b, c):
    global i
    if (n == 1):
        i += 1
        print('移动第 {0} 次 {1} --> {2}'.format(i, a, c))
        return
    move(n - 1, a, c, b)
    move(1, a, b, c)
    move(n - 1, b, a, c)


move(3, "a", "b", "c")  

# 移动第 1 次 a --> c
# 移动第 2 次 a --> b
# 移动第 3 次 c --> b
# 移动第 4 次 a --> c
# 移动第 5 次 b --> a
# 移动第 6 次 b --> c
# 移动第 7 次 a --> c

3.练习-车辆重排序

假设一列货运列车共有n节车厢,每节车厢将停放在不同的车站。假定n个车站的编号分别为1n,货运列车按照第n站至第1站的次序经过这些车站。车厢的编号与它们的目的地相同。为了便于从列车上卸掉相应的车厢,必须重新排列车厢,使各车厢从前至后按编号1n的次序排列。当所有的车厢都按照这种次序排列时,在每个车站只需卸掉最后一节车厢即可。

我们在一个转轨站里完成车厢的重排工作,在转轨站中有一个入轨、一个出轨和k个缓冲铁轨(位于入轨和出轨之间)。图(a)给出一个转轨站,其中有k个(k=3)缓冲铁轨H1H2H3。开始时,n节车厢的货车从入轨处进入转轨站,转轨结束时各车厢从右到左按照编号1n的次序离开转轨站(通过出轨处)。在图(a)中,n=9,车厢从后至前的初始次序为5,8,1,7,4,2,9,6,3。图(b)给出了按所要求的次序重新排列后的结果。

Transition buffer rail having three stations

编写算法实现火车车厢的重排,模拟具有n节车厢的火车“入轨”和“出轨”过程。

def output(stacks, n):
    global minVal, minStack
    stacks[minStack].pop()
    print('移动车厢 %d 从缓冲铁轨 %d 到出轨。' % (minVal, minStack))
    minVal = n + 2
    minStack = -1
    for index, stack in enumerate(stacks):
        if((not stack.isempty()) and (stack.top() < minVal)):
            minVal = stack.top()
            minStack = index

def inputStack(i, stacks, n):
    global minVal, minStack
    beskStack = -1  # 最小车厢索引值所在的缓冲铁轨编号
    bestTop = n + 1  # 缓冲铁轨中的最小车厢编号
    for index, stack in enumerate(stacks):
        if not stack.isempty():  # 若缓冲铁轨不为空
            # 若缓冲铁轨的栈顶元素大于要放入缓冲铁轨的元素,并且其栈顶元素小于当前缓冲铁轨中的最小编号
            a = stack.top()
            # print('stack.top()的类型是', a)
            if (a > i and bestTop > a):
                bestTop = stack.top()
                beskStack = index
        else:  # 若缓冲铁轨为空
            if beskStack == -1:
                beskStack = index
                break
    if beskStack == -1:
        return False
    stacks[beskStack].push(i)
    print('移动车厢 %d 从入轨到缓冲铁轨 %d。' % (i, beskStack))
    if i < minVal:
        minVal = i
        minStack = beskStack
    return True

def rail_road(list, k):
    global minVal, minStack
    stacks = []
    for i in range(k):
        stack = stack1.ArrayStack()
        stacks.append(stack)
    nowNeed = 1
    n = len(list)
    minVal = n + 1
    minStack = -1
    for i in list:
        if i == nowNeed:
            print('移动车厢 %d 从入轨到出轨。' % i)
            nowNeed += 1
            # print("minVal", minVal)
            while (minVal == nowNeed):
                output(stacks, n)  # 在缓冲栈中查找是否有需求值
                nowNeed += 1
        else:
            if(inputStack(i, stacks, n) == False):
                return False
    return True

if __name__ == "__main__":
    list = [3, 6, 9, 2, 4, 7, 1, 8, 5]
    k = 3
    minVal = len(list) + 1
    minStack = -1
    result = rail_road(list, k)
    while(result == False):
        print('需要更多的缓冲轨道,请输入需要添加的缓冲轨道数量。')
        k = k + int(input())
        result = rail_road(list, k)

Released eight original articles · won praise 1 · views 191

Guess you like

Origin blog.csdn.net/Moby97/article/details/103931237