Hanoi Tower
最近学习到了一个很有趣的problem. 汉诺塔。 我认真的想了好几天,终于在今天找IT给我set up machine等着没事干的时候大概想通了。哈哈哈。 解决这个问题的方法让我想到了binary tree traverse 里面的in order的遍历,汉诺塔的solution之一的 recursive 的方式跟二叉树in order就是异曲同工,而且in human mind, 汉诺塔should make more sense to understand compared to binary tree. 在网上看到了 一个印度小男孩对汉诺塔的分析 ,果然是tree. Basically 解决汉诺塔的问题就是一个搬家公司应该怎么搬家的问题,from mission completed to start, which is from bottom to top. Let's define Hanoi move function as: HanoiMove(int leftToSit, int from - 0 , int to - 1, int rest - 2) and we have n pieces in total waiting for the move In theory, to approach the last step nth of move, we just need finish move rest of Hanoi pieces (n-1 in total) from start to rest pile,(简单说来就是,清空路障,直捣虎穴。)and then it should be able to move the last piece from start to To pile. so we just do 1) HanoiMove(n-1, from, res, to) ---->>> left as we planned in theory, we move the nth piece move(from, to) after this move, we have...
declare a list
ReplyDeletelist = []
declare a dictionary
dic = {}
Regular Expressions in Python
ReplyDeleteAn example regex is r"^(From|To|Cc).*?python-list@python.org"
- the caret ^ matches text at the beginning of a line.
- the part with (From|To|Cc) means that the line has to start with one of the words that are separated by the pipe |
- The .*? means to un-greedily match any number of characters, except the newline \n character. The un-greedy part means to match as few repetitions as possible. The . character means any non-newline character, the * means to repeat 0 or more times, and the ? character makes it un-greedy.