This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def HanoiTowers(start, via, target, n): | |
""" computes a list of discs steps to move a stack of n discs from | |
rod "start" to rod "target" employing intermidiate rod "via" """ | |
if n == 0: | |
return [] | |
else: | |
return HanoiTowers(start, target, via, n-1) \ | |
+ [str.format("disk {} from {} to {}", n, start, target)] \ | |
+ HanoiTowers(via, start, target, n-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def generate_name(): | |
""" generate a random first name with 3-6 letters, | |
space, and family name with 4-8 letters """ | |
first=random.sample(alphabet ,random.randint(3,6)) | |
family=random.sample(alphabet ,random.randint(4,8)) | |
name=str.join("", first) + " " + str.join("", family) | |
return str.title(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def elapsed(expression, number=1): | |
''' computes elapsed time for executing code | |
number of times (default is 1 time). expression should | |
be a string representing a Python expression. ''' | |
t1 = time.clock() | |
for i in range(number): | |
eval(expression) | |
t2 = time.clock() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def display_gcd(x,y): | |
""" greatest common divisor of two integers - Euclid's algorithm | |
Function prints all intermediate results along the way """ | |
assert isinstance(x,int) and isinstance(y,int) | |
# type checking: x and y both integers | |
x,y=abs(x),abs(y) # simultaneous assignment to x and y | |
# gcd invariant to abs. Both x,y now non-negativr | |
if x<y: | |
x,y=y,x # switch x and y if x < y. Now y <= x | |
print(x,y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge(lst1, lst2): | |
""" merging two ordered lists using | |
the two pointer algorithm """ | |
n1 = len(lst1) | |
n2 = len(lst2) | |
lst3 = [0 for i in range(n1 + n2)] # alocates a new list | |
i = j = k = 0 # simultaneous assignment | |
while (i < n1 and j < n2): | |
if (lst1[i] <= lst2[j]): | |
lst3[k] = lst1[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random # package for generating (pseudo) random elements | |
def quicksort(lst): | |
""" quick sort of lst """ | |
if len(lst) <= 1: | |
return lst | |
else: | |
pivot = random.choice(lst) # select a random element from list | |
smaller = [elem for elem in lst if elem < pivot] | |
equal = [elem for elem in lst if elem == pivot] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def generate_name(): | |
""" generate a random first name with 3-6 letters, | |
space, and family name with 4-8 letters """ | |
first=random.sample(alphabet ,random.randint(3,6)) | |
family=random.sample(alphabet ,random.randint(4,8)) | |
name=str.join("", first) + " " + str.join("", family) | |
return str.title(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n): | |
""" recursive Fibonacci, naive """ | |
if n <= 2: | |
return 1 | |
else: | |
return fibonacci(n-2) + fibonacci(n-1) | |
count = 0 | |
def count_fibonacci(n): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hanoi_towers(start, via, target, n): | |
# computes a list of discs steps to move a stack of n discs from | |
# rod "start" to rod "target" employing intermidiate rod "via" | |
if n == 0: | |
return [] | |
else: | |
return hanoi_towers(start, target, via, n-1) \ | |
+ [str.format("disk {} from {} to {}", n, start, target)] \ | |
+ hanoi_towers(via, start, target, n-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def square(x): | |
return x**2 | |
def linear(x): | |
return 2 * x + 1 | |
def pow10(x): | |
return x**10 | |
def integral(f, h=0.001): |
OlderNewer