# HG changeset patch # User paulb@localhost.localdomain # Date 1171759151 -3600 # Node ID 76b0d12a915cff600599f250ec7e3b2843d584ec # Parent d2dee1202635d0a6f7d04b67d13729059b784722 Added various shootout tests (binary-trees, nbody and recursive work). Added support for certain built-in object/function behaviour. diff -r d2dee1202635 -r 76b0d12a915c lib/builtins.py --- a/lib/builtins.py Sun Feb 18 00:50:52 2007 +0100 +++ b/lib/builtins.py Sun Feb 18 01:39:11 2007 +0100 @@ -74,6 +74,9 @@ class float: __atomic__ = 1 + def __init__(self, number_or_string=None): + pass + def __iadd__(self, other): if isinstance(other, int): return float() @@ -289,6 +292,9 @@ class int: __atomic__ = 1 + def __init__(self, number_or_string=None): + pass + def __iadd__(self, other): if isinstance(other, int): return int() @@ -485,6 +491,9 @@ class long: __atomic__ = 1 + def __init__(self, number_or_string=None): + pass + def __iadd__(self, other): if isinstance(other, int): return long() @@ -620,6 +629,12 @@ else: raise TypeError + def __mod__(self, other): + + "The format operator for strings." + + return str() + def __len__(self): return int() @@ -716,11 +731,15 @@ return bool() class xrange: - def __init__(self, start, end, step=1): - self.start = start - self.end = end + def __init__(self, start_or_end, end=None, step=1): + if end is None: + self.start = 0 + self.end = start_or_end + else: + self.start = start_or_end + self.end = end self.step = step - self.current = start + self.current = self.start def __iter__(self): return self diff -r d2dee1202635 -r 76b0d12a915c tests/shootout/binary-trees.py --- a/tests/shootout/binary-trees.py Sun Feb 18 00:50:52 2007 +0100 +++ b/tests/shootout/binary-trees.py Sun Feb 18 01:39:11 2007 +0100 @@ -26,7 +26,7 @@ max_depth = max(min_depth + 2, 16) #int(argv[1])) stretch_depth = max_depth + 1 - #print "stretch tree of depth %d\t check: %d" % (stretch_depth, check_tree(make_tree(0, stretch_depth))) + print "stretch tree of depth %d\t check: %d" % (stretch_depth, check_tree(make_tree(0, stretch_depth))) long_lived_tree = make_tree(0, max_depth) @@ -37,9 +37,9 @@ for i in xrange(1, iterations + 1): check += check_tree(make_tree(i, depth)) + check_tree(make_tree(-i, depth)) - #print "%d\t trees of depth %d\t check: %d" % (iterations * 2, depth, check) + print "%d\t trees of depth %d\t check: %d" % (iterations * 2, depth, check) - #print "long lived tree of depth %d\t check: %d" % (max_depth, check_tree(long_lived_tree)) + print "long lived tree of depth %d\t check: %d" % (max_depth, check_tree(long_lived_tree)) if __name__ == '__main__': main() diff -r d2dee1202635 -r 76b0d12a915c tests/shootout/chameneos.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/shootout/chameneos.py Sun Feb 18 01:39:11 2007 +0100 @@ -0,0 +1,79 @@ +#-- The Computer Language Shootout +#-- http://shootout.alioth.debian.org/ +#-- contributed by Tobias Polzin, translated from Mike Pall's Lua program +#-- modified by Josh Goldfoot to use ifs for the complement routine + +import sys + +N = int(sys.argv[1]) +first = None +second = None +meetings = 0 + +RED, BLUE, YELLOW = xrange(1,4) + +#-- Create a very social creature. +def creature(me): + global N, first, second, meetings + met = 0 + while 1: + #-- Meet another creature. + + #-- Wait until meeting place clears. + while second: + yield None + + other = first + if other: + #-- Hey, I found a new friend! + second = me + else: + # -- Sniff, nobody here (yet). + if N <= 0: + #-- Uh oh, the mall is closed. + meetings += met + yield None + + # The mall was closed, so everyone is faded. + raise StopIteration + N -= 1 + first = me + while not second: + yield None #-- Wait for another creature. + other = second + + first = None + second = None + yield None + + # perform meeting + met += 1 + if me != other: + if me == BLUE: + me = other == RED and YELLOW or RED + elif me == RED: + me = other == BLUE and YELLOW or BLUE + elif me == YELLOW: + me = other == BLUE and RED or BLUE + +#-- Trivial round-robin scheduler. +def schedule(threads): + global meetings + try: + while 1: + for thread in threads: + thread.next() + except StopIteration: + return meetings + +def main(): + #-- A bunch of colorful creatures. + threads = [ + creature(BLUE), + creature(RED), + creature(YELLOW), + creature(BLUE) ] + + print schedule(threads) + +main() diff -r d2dee1202635 -r 76b0d12a915c tests/shootout/fannkuch.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/shootout/fannkuch.py Sun Feb 18 01:39:11 2007 +0100 @@ -0,0 +1,49 @@ +# The Computer Language Shootout +# http://shootout.alioth.debian.org/ +# +# Contributed by Sokolov Yura + +from sys import argv +def fannkuch(n): + count = range(1,n+1) + maxFlipsCount, m, r, check = 0, n-1, n, 0 + + perm1 = range(n) + perm = range(n) + perm1_ins = perm1.insert + perm1_pop = perm1.pop + while True: + if check < 30: + print "".join(`i+1` for i in perm1) + check += 1; + + while r != 1: + count[r-1] = r + r -= 1 + + if perm1[0] != 0 and perm1[m] != m: + perm[:]=perm1 + flipsCount = 0 + k = perm[0] + while k: + perm[:k+1] = perm[k::-1] + flipsCount += 1 + k = perm[0] + + if flipsCount > maxFlipsCount: + maxFlipsCount = flipsCount + maxPerm = list(perm1) + + while True: + if r == n: return maxFlipsCount + perm1_ins(r,perm1_pop(0)) + count[r] -= 1 + if count[r] > 0: break + r += 1 + +def main(): + n = int(argv and argv[1] or 1) + print "Pfannkuchen(%d) = %d\n"%(n,fannkuch(n)), + +if __name__=="__main__": + main() diff -r d2dee1202635 -r 76b0d12a915c tests/shootout/fasta.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/shootout/fasta.py Sun Feb 18 01:39:11 2007 +0100 @@ -0,0 +1,94 @@ +#!/usr/bin/env python + +# The Great Computer Language Shootout +# http://shootout.alioth.debian.org/ +# +# modified by Ian Osgood + +import sys +import itertools +import bisect + +def gen_random(max_, last=[42], ia=3877, ic=29573, im=139968): + last[0] = (last[0] * ia + ic) % im + return (max_ * last[0]) / im + +def make_cumulative(table): + l = [] + prob = 0.0 + for char, p in table: + prob += p + l.append((prob, char)) + return l + +alu = ( + "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" + "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" + "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" + "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" + "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" + "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" + "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA" +) + +iub = [ + ("a", 0.27), + ("c", 0.12), + ("g", 0.12), + ("t", 0.27), + + ("B", 0.02), + ("D", 0.02), + ("H", 0.02), + ("K", 0.02), + ("M", 0.02), + ("N", 0.02), + ("R", 0.02), + ("S", 0.02), + ("V", 0.02), + ("W", 0.02), + ("Y", 0.02), +] + +homosapiens = [ + ("a", 0.3029549426680), + ("c", 0.1979883004921), + ("g", 0.1975473066391), + ("t", 0.3015094502008), +] + + +def make_repeat_fasta(id_, desc, src, n): + print ">%s %s" % (id_, desc) + width = 60 + l = len(src) + s = src * (n // l) + s += src[:n % l] + #assert len(s) == n + print "\n".join([s[i:i+width] for i in xrange(0, n, width)]) + +def make_random_fasta(id_, desc, table, n): + print ">%s %s" % (id_, desc) + width = 60 + chunk = 1 * width + _gr = gen_random + _table = make_cumulative(table) + probs = [t[0] for t in _table] + chars = [t[1] for t in _table] + _bisect = bisect.bisect + r = range(width) + for j in xrange(n // width): + print "".join([chars[_bisect(probs, _gr(1.0))] for i in r]) + if n % width: + print "".join([chars[_bisect(probs, _gr(1.0))] for i in xrange(n % width)]) + +def main(): + try: + n = int(sys.argv[1]) + except: + print "Usage: %s " % sys.argv[0] + make_repeat_fasta('ONE', 'Homo sapiens alu', alu, n*2) + make_random_fasta('TWO', 'IUB ambiguity codes', iub, n*3) + make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, n*5) + +main() diff -r d2dee1202635 -r 76b0d12a915c tests/shootout/nbody.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/shootout/nbody.py Sun Feb 18 01:39:11 2007 +0100 @@ -0,0 +1,131 @@ +#!/usr/bin/python -OO +# The Computer Language Shootout Benchmarks +# http://shootout.alioth.debian.org/ +# +# contributed by Kevin Carson + +import sys + +pi = 3.14159265358979323 +solar_mass = 4 * pi * pi +days_per_year = 365.24 + +class body : + pass + +def advance(bodies, dt) : + for i in xrange(len(bodies)) : + b = bodies[i] + + for j in xrange(i + 1, len(bodies)) : + b2 = bodies[j] + + dx = b.x - b2.x + dy = b.y - b2.y + dz = b.z - b2.z + distance = (dx**2 + dy**2 + dz**2)**0.5 + + b_mass_x_mag = dt * b.mass / distance**3 + b2_mass_x_mag = dt * b2.mass / distance**3 + + b.vx -= dx * b2_mass_x_mag + b.vy -= dy * b2_mass_x_mag + b.vz -= dz * b2_mass_x_mag + b2.vx += dx * b_mass_x_mag + b2.vy += dy * b_mass_x_mag + b2.vz += dz * b_mass_x_mag + + for b in bodies : + b.x += dt * b.vx + b.y += dt * b.vy + b.z += dt * b.vz + +def energy(bodies) : + e = 0.0 + for i in xrange(len(bodies)) : + b = bodies[i] + e += 0.5 * b.mass * (b.vx**2 + b.vy**2 + b.vz**2) + + for j in xrange(i + 1, len(bodies)) : + b2 = bodies[j] + + dx = b.x - b2.x + dy = b.y - b2.y + dz = b.z - b2.z + distance = (dx**2 + dy**2 + dz**2)**0.5 + + e -= (b.mass * b2.mass) / distance + + return e + +def offset_momentum(bodies) : + global sun + px = py = pz = 0.0 + + for b in bodies : + px += b.vx * b.mass + py += b.vy * b.mass + pz += b.vz * b.mass + + sun.vx = - px / solar_mass + sun.vy = - py / solar_mass + sun.vz = - pz / solar_mass + +sun = body() +sun.x = sun.y = sun.z = sun.vx = sun.vy = sun.vz = 0.0 +sun.mass = solar_mass + +jupiter = body() +jupiter.x = 4.84143144246472090e+00 +jupiter.y = -1.16032004402742839e+00 +jupiter.z = -1.03622044471123109e-01 +jupiter.vx = 1.66007664274403694e-03 * days_per_year +jupiter.vy = 7.69901118419740425e-03 * days_per_year +jupiter.vz = -6.90460016972063023e-05 * days_per_year +jupiter.mass = 9.54791938424326609e-04 * solar_mass + +saturn = body() +saturn.x = 8.34336671824457987e+00 +saturn.y = 4.12479856412430479e+00 +saturn.z = -4.03523417114321381e-01 +saturn.vx = -2.76742510726862411e-03 * days_per_year +saturn.vy = 4.99852801234917238e-03 * days_per_year +saturn.vz = 2.30417297573763929e-05 * days_per_year +saturn.mass = 2.85885980666130812e-04 * solar_mass + +uranus = body() +uranus.x = 1.28943695621391310e+01 +uranus.y = -1.51111514016986312e+01 +uranus.z = -2.23307578892655734e-01 +uranus.vx = 2.96460137564761618e-03 * days_per_year +uranus.vy = 2.37847173959480950e-03 * days_per_year +uranus.vz = -2.96589568540237556e-05 * days_per_year +uranus.mass = 4.36624404335156298e-05 * solar_mass + +neptune = body() +neptune.x = 1.53796971148509165e+01 +neptune.y = -2.59193146099879641e+01 +neptune.z = 1.79258772950371181e-01 +neptune.vx = 2.68067772490389322e-03 * days_per_year +neptune.vy = 1.62824170038242295e-03 * days_per_year +neptune.vz = -9.51592254519715870e-05 * days_per_year +neptune.mass = 5.15138902046611451e-05 * solar_mass + +def main() : + try : + n = int(sys.argv[1]) + except : + print "Usage: %s " % sys.argv[0] + + bodies = [sun, jupiter, saturn, uranus, neptune] + + offset_momentum(bodies) + + print "%.9f" % energy(bodies) + + for i in xrange(n) : + advance(bodies, 0.01) + + print "%.9f" % energy(bodies) + +main() diff -r d2dee1202635 -r 76b0d12a915c tests/shootout/recursive.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/shootout/recursive.py Sun Feb 18 01:39:11 2007 +0100 @@ -0,0 +1,35 @@ +# The Computer Language Shootout +# http://shootout.alioth.debian.org/ +# based on bearophile's psyco program +# slightly modified by Isaac Gouy + +def Ack(x, y): + if x == 0: return y+1 + if y == 0: return Ack(x-1, 1) + return Ack(x-1, Ack(x, y-1)) + +def Fib(n): + if n < 2: return 1 + return Fib(n-2) + Fib(n-1) + +def FibFP(n): + if n < 2.0: return 1.0 + return FibFP(n-2.0) + FibFP(n-1.0) + +def Tak(x, y, z): + if y < x: return Tak( Tak(x-1,y,z), Tak(y-1,z,x), Tak(z-1,x,y) ) + return z + +def TakFP(x, y, z): + if y < x: return TakFP( TakFP(x-1.0,y,z), TakFP(y-1.0,z,x), TakFP(z-1.0,x,y) ) + return z + +from sys import argv, setrecursionlimit +setrecursionlimit(20000) + +n = int(argv[1]) - 1 +print "Ack(3,%d):" % (n+1), Ack(3, n+1) +print "Fib(%.1f): %.1f" % (28.0+n, FibFP(28.0+n)) +print "Tak(%d,%d,%d): %d" % (3*n, 2*n, n, Tak(3*n, 2*n, n)) +print "Fib(3):", Fib(3) +print "Tak(3.0,2.0,1.0):", TakFP(3.0, 2.0, 1.0)