# morefun.py # Walker M. White (wmw2) # October 8, 2015 """Functions from second recursion lecture. These functions show the why the choice of division matters.""" import sys # Allow us to go really deep sys.setrecursionlimit(999999999) # COMMAFY FUNCTIONS def commafy_str(s): """Returns: s, with commas every 3 digits. Example: commafy('5341267') = '5,341,267' Parameter s: string representing an integer Precondition: s a string with only digits, not starting with 0""" # You can't always check everything with an assert # However, check what you can assert type(s) == str, str(s) + ' is not a string' if len(s) <= 3: return s; # String has more than 3 digits return commafy_str(s[0:-3]) + ',' +s[-3:] def commafy(n): """Returns: n as a string, with commas every 3 digits. Example: commafy('5341267') = '5,341,267' Parameter n: number to convert Precondition: n is an int with n >= 0""" assert type(n) == int, str(n)+' is not an int' # get in the habit assert n >= 0, str(n)+' is negative' # get in the habit return commafy_str(str(n)) #if n < 1000: # return str(n) # n >= 1000 #return commafy(n/1000) + ',' + to3(n % 1000) def to3(p): """Returns: p with at least 3 chars Adds leading 0's if necessary Parameter n: number to pad Precondition: p is an int""" assert type(p) == int, str(p)+' is not an int' # get in the habit if p < 10: return '00' + str(p) elif p < 100: return '0' + str(p) return str(p) # EXPONENTIAL FUNCTIONS # Number of frames used in exp recursive calls count_frames = 0 def exp_slow(b, c): """Returns: b^c. Property: b^c = b * b^(c-1) Parameter b: the number to raise to a power Precondition: b is a number Parameter c: the exponent Precondition: c is an int >= 0""" # get in the habit of checking what you can assert type(b) == float or type(b) == int, str(b)+' is not a number' assert type(c) == int, str(c)+' is not an int' assert c >= 0, str(c)+' is negative' if c == 0: return 1 # Allows us to write to global variable. EVIL! Do not use! global count_frames # Used to count the number of frames count_frames = count_frames+1; # c > 0 return b * exp_slow(b, c-1) def exp_alt(b, c): """Returns: b^c. Property: b^c = b^(c/2) * b^(c-c/2) Parameter b: the number to raise to a power Precondition: b is a number Parameter c: the exponent Precondition: c is an int >= 0""" assert type(b) == float or type(b) == int, str(b)+' is not a number' assert type(c) == int, str(c)+' is not an int' assert c >= 0, str(c)+' is negative' if c == 0: return 1 # Allows us to write to global variable. EVIL! Do not use! global count_frames # Used to count the number of frames count_frames = count_frames+1; # c > 0 return exp_alt(b, c/2)*exp_alt(b,c-c/2) def exp_fast(b, c): """Returns: b^c. Property. b^c = b * b^(c-1) Property. b^c = (b*b)^(c/2) for even c Parameter b: the number to raise to a power Precondition: b is a number Parameter c: the exponent Precondition: c is an int >= 0""" assert type(b) == float or type(b) == int, str(b)+' is not a number' assert type(c) == int, str(c)+' is not an int' assert c >= 0, str(c)+' is negative' if c == 0: return 1 # Allows us to write to global variable. EVIL! Do not use! global count_frames # Used to count the number of frames count_frames = count_frames+1; # c > 0 if c % 2 == 0: return exp_fast(b*b, c/2) return b * exp_fast(b, c-1)