Created
December 8, 2014 15:04
-
-
Save trhura/ceee939bfe292d1fb042 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 multiply(a, b): | |
# Assuming a and b are not negative | |
if b == 0: return 0 | |
if b == 1: return a | |
return a + multiply(a, b-1) | |
def power(b, e): | |
# Assuming e is not negative | |
if e == 0: return 1 | |
if e == 1: return b | |
return multiply(b, power(b, e-1)) | |
def random_test (func, builtin_func): | |
import random, sys, resource | |
# increment the stack depth | |
sys.setrecursionlimit(1000000) | |
# ulimit tweak | |
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) | |
for a in range (0, 7): | |
b = random.randint(0, 7) | |
assert func(a, b) == builtin_func(a, b) | |
import operator | |
random_test(multiply, operator.mul) | |
random_test(power, operator.pow) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment