Created by John Robison
A computational model of a system that has distinct states. The system can transition between these states based on external events.
Current State | Input | Next State | Output |
---|---|---|---|
Locked | coin | Unlocked | Unlock turnstile so customer can push through |
push | Locked | None | |
Unlocked | coin | Unlocked | None |
push | Locked | When customer has pushed through, lock turnstile |
A grid of cells which can have a state of on or off (also referred to as "alive" or "dead").
The transitions between these states are determined by the number of living cells that exist around the current cell.
# Takes a partially filled-in grid and attempts to assign values to
# all unassigned locations in such a way to meet the requirements
# for Sudoku solution (non-duplication across rows, columns, and boxes)
def solve_sudoku(arr):
# 'l' is a list variable that keeps the record of row and col in find_empty_location Function
l=[0,0]
# If there is no unassigned location, we are done
if(not find_empty_location(arr,l)):
return True
# Assigning list values to row and col that we got from the above Function
row=l[0]
col=l[1]
# consider digits 1 to 9
for num in range(1,10):
# if looks promising
if(check_location_is_safe(arr,row,col,num)):
# make tentative assignment
arr[row][col]=num
# return, if sucess, ya!
if(solve_sudoku(arr)):
return True
# failure, unmake & try again
arr[row][col] = 0
# this triggers backtracking
return False
A type of dynamic programming where past values are saved in a table for faster computation later in a program's execution.
def memoize(F):
# A hash table for storing previous states
hashed_states = {}
def memoized_F(*args):
# if we haven't memoized this state, calculate
# it. Then return it
if args not in hashed_states:
hashed_states[args] = F(*args)
return hashed_states[args]
return memoized_F
@memoize
def fibonacci(N):
if N is 0 or N is 1:
return N
else:
return fibonacci(N-1) + fibonacci(N-2)