Intro to
Programming
(with Python)

Fall 2016
course
navigation

Nov 22

Questions about anything?
Any project topics you'd care to talk about?
Go over homework : which recursion exercise did you do?
We did the palindrome problem in class in a fair amount of detail. The code is attached.

game search

One of the classic recursive algorithms is searching for the best move in a two person perfect information game, like tic-tac-toe or chess.
First I'll discuss the "min-max" search tree.
Then I'll show an example: the subtraction game.
Depending on interest, we may start work on a tic-tac-toe game following the same pattern, continuing it next week.

memoization

We may discuss this idea, which makes some recursive procedures much, much faster.
This can also make the AI part of games like tic-tac-toe respond right away, by pre-solving and remembering each best move.
memo = {} # remember values of fib(x) def fib(x): if not memo.has_key(x): print " calculating fib({})".format(x) if x < 2: memo[x] = 1 else: memo[x] = fib(x-1) + fib(x-2) return memo[x]
Without the memo dict, calculating (say) fib(100) takes longer than you'd want to wait, since it does the same calculation many times along the way.
http://cs.marlboro.edu/ courses/ fall2016/python/ notes/ Nov_22
last modified Tuesday November 22 2016 11:42 am EST

attachments [paper clip]

     name last modified size
   palindrome.py Nov 22 2016 11:42 am 1.49kB