Hannot tower problem

  • Rules:

    1. You can only move one plate at a time
    2. In any one move, the state of the three towers must be smaller plate on top and larger plate on top
  • Methods:

    1. N =1: move A plate directly from A to C, A minus >C
    2. N = 2:

      1. I’m going to take the little plate from A to B, A minus >B
      2. You take the big plate from A to C, A minus >, C
      3. I’m going to take the little plate from B to C, B minus >, C
    3. n=3:

      1. Take the two plates from A, move them through C to B, and call the recursive implementation
      2. I’m going to move the largest plate left on A to C, A minus >, C
      3. Take two plates from B, with the help of A, and move them to C, and call recursion
    4. N = n:

      1. We take n minus 1 plates from A, with the help of C, and we move them to B, and we call recursion
      2. Take the largest plate on A, which is the only one, and move it to C, A minus >C
      3. We take n minus 1 plates from B, and with the help of A, we move them to C, and we call recursion
Def hano(n, a, b, c): "" recursive implementation of Hanno tower n: several towers a: first tower, beginning tower b: second tower, middle tower c: third tower, target tower" "if n == 1: print(a, "-->", c) return None ''' if n== 2: Print (a, "-- >", b) print (a, "-- >", c) print (b, "-- >", c) return None "' # n - 1 tower, from a tower with c tower, Print (a, "-->", c) # print(a, "-->", c) # print(a, "-->", c) # print(a, "-->", c) # print(a, "-->", c) # Hano (n, a, b, a, c) = 'a' b = 'b' c = 'c' n = 1
A --> C


n = 2
hano(n, a, b, c)
A --> B
A --> C
B --> C


n = 3
hano(n, a, b, c)
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C


n = 5
hano(n, a, b, c)
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
A --> B
C --> B
C --> A
B --> A
C --> B
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
B --> A
C --> B
C --> A
B --> A
B --> C
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C

The List (List)

  • Del: Delete command
Print print print print print print print print print print print print print print print print print print print print print
[1, 2, 4, 5, 6]


List a = [1,2,3,4,5,6] print(id(a)) del a[2] print(id(a)) print(a) print(a)
1787345100360 [1, 2, 4, 5, 6]
# del a print(a) # del a print(a)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-15-a09b634934b2> in <module> 1 # del ----> 2 del a 3 print(a) NameError: name 'a' is not defined

List together

# use plus connecting two lists a = b =,6,7,8,9 [5] [1, 2, 3, 4, 5] d = [' a ', 'b', 'c'] c = a + b + d print (c)
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 'a', 'b', 'c']


Print (b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b)
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Print (b in a) print(b in a) print(b in a) print(b in a) print(b in a)
False
True


Print (b not in a) print(b not in a) print(b not in a)
True

List traversal

  • for
  • while
For I in a = [1,2,3,4,5] for I in a: print(I)
One, two, three, four, five
For I in range(0,len(a)): print(a[I]) I += 1
One, two, three, four, five
b = ["I love you"]

for i in b:
    print(i)
I love you


Print (I) print(type(range(1,10)) print(type(range(1,10))
1
2
3
4
5
6
7
8
9
<class 'range'>


List a = [1,2,3,4,5,6] length = len(a) # indx = 0 while indx < length: print(a[indx]) indx += 1
One, two, three, four, five, six
# # list of double loop a nested list, or double list a = [[" one ", 1], [" two ", 2], [" three ", 3]] for k, v in a: print (" -- "k, v)
one -- 1
two -- 2
three -- 3


# # list of double loop variable a is nested lists, or double list a = [[" one ", 1, "enis"], [" two ", 2], [" three ", 3,4,5,6,8]] for k, v in a: print (" -- "k, v)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last)  <ipython-input-44-db702adbc386> in <module> 4 a = [["one", 1, "enis"], ["two", 2], ["three", Print (k, "--", v) ValueError: too many values to unpack (expected 2) print(k, "--", v) ValueError: too many values to unpack (expected 2)
# # list of double loop variable a is nested lists, or double list a = [[" one ", 1, "enis"], [" two ", 2, "zwei"], [" three ", 3 "drei"]] # point of this illustration: For k,v,w in a: print(k, "--", v, "--", w)
one -- 1 -- enis
two -- 2 -- zwei
three -- 3 -- drei

List content

  • Create a list in an easy way
Print (b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b) print(b)
['a', 'b', 'c']


List a = [1, 2, 3, 4, 5] List a = [1, 2, 3, 4, 5] List b = [1, 2, 3, 4, 5] B = [I *10 for I in a] print(b)
[10, 20, 30, 40, 50]


You can also filter the contents of an old List and put them into a new List. B = [m for m in a if m % 2 == 0] b = [m for m in a if m % 2 == 0] print(b)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34]


# list generation can be nested # there are two lists a Print (b) = [I for I in range(1,4)] print(b) = [I for I in range(100,400) if I % 100 == 0] print(b) = [I for I in range(1,4)] C = [m+n for m in a for n in b] print(c) # for m in a for n in b: Print (m+n, end=" ") print() # print() = [m+n for m in a for n in b if m+n < 250] print(c)
[1, 2, 3] [100, 200, 300] [101, 201, 301, 102, 202, 302, 103, 203] 101 201 301 102 202 302 103 203 102, 202, 103, 203]

Common functions about lists

Print (len(a) = [x for x in range(1,100)] print(len(a) = [x for x in range(1,100)] print(len(a) = [x for x in range(1,100)] The same print (Max) (a) b = [" man ", "film", "python"] print (Max) (b)
99
99
python


Print (List a = [1,2,3]) print(List (a)) print(List (a)) print(List (a))
[1, 2, 3]


s = "I love you"
print(list(s))
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']


Print (list(range(12,19))) print(list(range(12,19))
[12, 13, 14, 15, 16, 17, 18]