1.1 --- a/lib/builtins.py Sun Feb 18 00:50:52 2007 +0100
1.2 +++ b/lib/builtins.py Sun Feb 18 01:39:11 2007 +0100
1.3 @@ -74,6 +74,9 @@
1.4 class float:
1.5 __atomic__ = 1
1.6
1.7 + def __init__(self, number_or_string=None):
1.8 + pass
1.9 +
1.10 def __iadd__(self, other):
1.11 if isinstance(other, int):
1.12 return float()
1.13 @@ -289,6 +292,9 @@
1.14 class int:
1.15 __atomic__ = 1
1.16
1.17 + def __init__(self, number_or_string=None):
1.18 + pass
1.19 +
1.20 def __iadd__(self, other):
1.21 if isinstance(other, int):
1.22 return int()
1.23 @@ -485,6 +491,9 @@
1.24 class long:
1.25 __atomic__ = 1
1.26
1.27 + def __init__(self, number_or_string=None):
1.28 + pass
1.29 +
1.30 def __iadd__(self, other):
1.31 if isinstance(other, int):
1.32 return long()
1.33 @@ -620,6 +629,12 @@
1.34 else:
1.35 raise TypeError
1.36
1.37 + def __mod__(self, other):
1.38 +
1.39 + "The format operator for strings."
1.40 +
1.41 + return str()
1.42 +
1.43 def __len__(self):
1.44 return int()
1.45
1.46 @@ -716,11 +731,15 @@
1.47 return bool()
1.48
1.49 class xrange:
1.50 - def __init__(self, start, end, step=1):
1.51 - self.start = start
1.52 - self.end = end
1.53 + def __init__(self, start_or_end, end=None, step=1):
1.54 + if end is None:
1.55 + self.start = 0
1.56 + self.end = start_or_end
1.57 + else:
1.58 + self.start = start_or_end
1.59 + self.end = end
1.60 self.step = step
1.61 - self.current = start
1.62 + self.current = self.start
1.63
1.64 def __iter__(self):
1.65 return self
2.1 --- a/tests/shootout/binary-trees.py Sun Feb 18 00:50:52 2007 +0100
2.2 +++ b/tests/shootout/binary-trees.py Sun Feb 18 01:39:11 2007 +0100
2.3 @@ -26,7 +26,7 @@
2.4 max_depth = max(min_depth + 2, 16) #int(argv[1]))
2.5 stretch_depth = max_depth + 1
2.6
2.7 - #print "stretch tree of depth %d\t check: %d" % (stretch_depth, check_tree(make_tree(0, stretch_depth)))
2.8 + print "stretch tree of depth %d\t check: %d" % (stretch_depth, check_tree(make_tree(0, stretch_depth)))
2.9
2.10 long_lived_tree = make_tree(0, max_depth)
2.11
2.12 @@ -37,9 +37,9 @@
2.13 for i in xrange(1, iterations + 1):
2.14 check += check_tree(make_tree(i, depth)) + check_tree(make_tree(-i, depth))
2.15
2.16 - #print "%d\t trees of depth %d\t check: %d" % (iterations * 2, depth, check)
2.17 + print "%d\t trees of depth %d\t check: %d" % (iterations * 2, depth, check)
2.18
2.19 - #print "long lived tree of depth %d\t check: %d" % (max_depth, check_tree(long_lived_tree))
2.20 + print "long lived tree of depth %d\t check: %d" % (max_depth, check_tree(long_lived_tree))
2.21
2.22 if __name__ == '__main__':
2.23 main()
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/shootout/chameneos.py Sun Feb 18 01:39:11 2007 +0100
3.3 @@ -0,0 +1,79 @@
3.4 +#-- The Computer Language Shootout
3.5 +#-- http://shootout.alioth.debian.org/
3.6 +#-- contributed by Tobias Polzin, translated from Mike Pall's Lua program
3.7 +#-- modified by Josh Goldfoot to use ifs for the complement routine
3.8 +
3.9 +import sys
3.10 +
3.11 +N = int(sys.argv[1])
3.12 +first = None
3.13 +second = None
3.14 +meetings = 0
3.15 +
3.16 +RED, BLUE, YELLOW = xrange(1,4)
3.17 +
3.18 +#-- Create a very social creature.
3.19 +def creature(me):
3.20 + global N, first, second, meetings
3.21 + met = 0
3.22 + while 1:
3.23 + #-- Meet another creature.
3.24 +
3.25 + #-- Wait until meeting place clears.
3.26 + while second:
3.27 + yield None
3.28 +
3.29 + other = first
3.30 + if other:
3.31 + #-- Hey, I found a new friend!
3.32 + second = me
3.33 + else:
3.34 + # -- Sniff, nobody here (yet).
3.35 + if N <= 0:
3.36 + #-- Uh oh, the mall is closed.
3.37 + meetings += met
3.38 + yield None
3.39 +
3.40 + # The mall was closed, so everyone is faded.
3.41 + raise StopIteration
3.42 + N -= 1
3.43 + first = me
3.44 + while not second:
3.45 + yield None #-- Wait for another creature.
3.46 + other = second
3.47 +
3.48 + first = None
3.49 + second = None
3.50 + yield None
3.51 +
3.52 + # perform meeting
3.53 + met += 1
3.54 + if me != other:
3.55 + if me == BLUE:
3.56 + me = other == RED and YELLOW or RED
3.57 + elif me == RED:
3.58 + me = other == BLUE and YELLOW or BLUE
3.59 + elif me == YELLOW:
3.60 + me = other == BLUE and RED or BLUE
3.61 +
3.62 +#-- Trivial round-robin scheduler.
3.63 +def schedule(threads):
3.64 + global meetings
3.65 + try:
3.66 + while 1:
3.67 + for thread in threads:
3.68 + thread.next()
3.69 + except StopIteration:
3.70 + return meetings
3.71 +
3.72 +def main():
3.73 + #-- A bunch of colorful creatures.
3.74 + threads = [
3.75 + creature(BLUE),
3.76 + creature(RED),
3.77 + creature(YELLOW),
3.78 + creature(BLUE) ]
3.79 +
3.80 + print schedule(threads)
3.81 +
3.82 +main()
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/shootout/fannkuch.py Sun Feb 18 01:39:11 2007 +0100
4.3 @@ -0,0 +1,49 @@
4.4 +# The Computer Language Shootout
4.5 +# http://shootout.alioth.debian.org/
4.6 +#
4.7 +# Contributed by Sokolov Yura
4.8 +
4.9 +from sys import argv
4.10 +def fannkuch(n):
4.11 + count = range(1,n+1)
4.12 + maxFlipsCount, m, r, check = 0, n-1, n, 0
4.13 +
4.14 + perm1 = range(n)
4.15 + perm = range(n)
4.16 + perm1_ins = perm1.insert
4.17 + perm1_pop = perm1.pop
4.18 + while True:
4.19 + if check < 30:
4.20 + print "".join(`i+1` for i in perm1)
4.21 + check += 1;
4.22 +
4.23 + while r != 1:
4.24 + count[r-1] = r
4.25 + r -= 1
4.26 +
4.27 + if perm1[0] != 0 and perm1[m] != m:
4.28 + perm[:]=perm1
4.29 + flipsCount = 0
4.30 + k = perm[0]
4.31 + while k:
4.32 + perm[:k+1] = perm[k::-1]
4.33 + flipsCount += 1
4.34 + k = perm[0]
4.35 +
4.36 + if flipsCount > maxFlipsCount:
4.37 + maxFlipsCount = flipsCount
4.38 + maxPerm = list(perm1)
4.39 +
4.40 + while True:
4.41 + if r == n: return maxFlipsCount
4.42 + perm1_ins(r,perm1_pop(0))
4.43 + count[r] -= 1
4.44 + if count[r] > 0: break
4.45 + r += 1
4.46 +
4.47 +def main():
4.48 + n = int(argv and argv[1] or 1)
4.49 + print "Pfannkuchen(%d) = %d\n"%(n,fannkuch(n)),
4.50 +
4.51 +if __name__=="__main__":
4.52 + main()
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/tests/shootout/fasta.py Sun Feb 18 01:39:11 2007 +0100
5.3 @@ -0,0 +1,94 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +# The Great Computer Language Shootout
5.7 +# http://shootout.alioth.debian.org/
5.8 +#
5.9 +# modified by Ian Osgood
5.10 +
5.11 +import sys
5.12 +import itertools
5.13 +import bisect
5.14 +
5.15 +def gen_random(max_, last=[42], ia=3877, ic=29573, im=139968):
5.16 + last[0] = (last[0] * ia + ic) % im
5.17 + return (max_ * last[0]) / im
5.18 +
5.19 +def make_cumulative(table):
5.20 + l = []
5.21 + prob = 0.0
5.22 + for char, p in table:
5.23 + prob += p
5.24 + l.append((prob, char))
5.25 + return l
5.26 +
5.27 +alu = (
5.28 + "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"
5.29 + "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"
5.30 + "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"
5.31 + "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"
5.32 + "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"
5.33 + "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"
5.34 + "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"
5.35 +)
5.36 +
5.37 +iub = [
5.38 + ("a", 0.27),
5.39 + ("c", 0.12),
5.40 + ("g", 0.12),
5.41 + ("t", 0.27),
5.42 +
5.43 + ("B", 0.02),
5.44 + ("D", 0.02),
5.45 + ("H", 0.02),
5.46 + ("K", 0.02),
5.47 + ("M", 0.02),
5.48 + ("N", 0.02),
5.49 + ("R", 0.02),
5.50 + ("S", 0.02),
5.51 + ("V", 0.02),
5.52 + ("W", 0.02),
5.53 + ("Y", 0.02),
5.54 +]
5.55 +
5.56 +homosapiens = [
5.57 + ("a", 0.3029549426680),
5.58 + ("c", 0.1979883004921),
5.59 + ("g", 0.1975473066391),
5.60 + ("t", 0.3015094502008),
5.61 +]
5.62 +
5.63 +
5.64 +def make_repeat_fasta(id_, desc, src, n):
5.65 + print ">%s %s" % (id_, desc)
5.66 + width = 60
5.67 + l = len(src)
5.68 + s = src * (n // l)
5.69 + s += src[:n % l]
5.70 + #assert len(s) == n
5.71 + print "\n".join([s[i:i+width] for i in xrange(0, n, width)])
5.72 +
5.73 +def make_random_fasta(id_, desc, table, n):
5.74 + print ">%s %s" % (id_, desc)
5.75 + width = 60
5.76 + chunk = 1 * width
5.77 + _gr = gen_random
5.78 + _table = make_cumulative(table)
5.79 + probs = [t[0] for t in _table]
5.80 + chars = [t[1] for t in _table]
5.81 + _bisect = bisect.bisect
5.82 + r = range(width)
5.83 + for j in xrange(n // width):
5.84 + print "".join([chars[_bisect(probs, _gr(1.0))] for i in r])
5.85 + if n % width:
5.86 + print "".join([chars[_bisect(probs, _gr(1.0))] for i in xrange(n % width)])
5.87 +
5.88 +def main():
5.89 + try:
5.90 + n = int(sys.argv[1])
5.91 + except:
5.92 + print "Usage: %s <N>" % sys.argv[0]
5.93 + make_repeat_fasta('ONE', 'Homo sapiens alu', alu, n*2)
5.94 + make_random_fasta('TWO', 'IUB ambiguity codes', iub, n*3)
5.95 + make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, n*5)
5.96 +
5.97 +main()
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/shootout/nbody.py Sun Feb 18 01:39:11 2007 +0100
6.3 @@ -0,0 +1,131 @@
6.4 +#!/usr/bin/python -OO
6.5 +# The Computer Language Shootout Benchmarks
6.6 +# http://shootout.alioth.debian.org/
6.7 +#
6.8 +# contributed by Kevin Carson
6.9 +
6.10 +import sys
6.11 +
6.12 +pi = 3.14159265358979323
6.13 +solar_mass = 4 * pi * pi
6.14 +days_per_year = 365.24
6.15 +
6.16 +class body :
6.17 + pass
6.18 +
6.19 +def advance(bodies, dt) :
6.20 + for i in xrange(len(bodies)) :
6.21 + b = bodies[i]
6.22 +
6.23 + for j in xrange(i + 1, len(bodies)) :
6.24 + b2 = bodies[j]
6.25 +
6.26 + dx = b.x - b2.x
6.27 + dy = b.y - b2.y
6.28 + dz = b.z - b2.z
6.29 + distance = (dx**2 + dy**2 + dz**2)**0.5
6.30 +
6.31 + b_mass_x_mag = dt * b.mass / distance**3
6.32 + b2_mass_x_mag = dt * b2.mass / distance**3
6.33 +
6.34 + b.vx -= dx * b2_mass_x_mag
6.35 + b.vy -= dy * b2_mass_x_mag
6.36 + b.vz -= dz * b2_mass_x_mag
6.37 + b2.vx += dx * b_mass_x_mag
6.38 + b2.vy += dy * b_mass_x_mag
6.39 + b2.vz += dz * b_mass_x_mag
6.40 +
6.41 + for b in bodies :
6.42 + b.x += dt * b.vx
6.43 + b.y += dt * b.vy
6.44 + b.z += dt * b.vz
6.45 +
6.46 +def energy(bodies) :
6.47 + e = 0.0
6.48 + for i in xrange(len(bodies)) :
6.49 + b = bodies[i]
6.50 + e += 0.5 * b.mass * (b.vx**2 + b.vy**2 + b.vz**2)
6.51 +
6.52 + for j in xrange(i + 1, len(bodies)) :
6.53 + b2 = bodies[j]
6.54 +
6.55 + dx = b.x - b2.x
6.56 + dy = b.y - b2.y
6.57 + dz = b.z - b2.z
6.58 + distance = (dx**2 + dy**2 + dz**2)**0.5
6.59 +
6.60 + e -= (b.mass * b2.mass) / distance
6.61 +
6.62 + return e
6.63 +
6.64 +def offset_momentum(bodies) :
6.65 + global sun
6.66 + px = py = pz = 0.0
6.67 +
6.68 + for b in bodies :
6.69 + px += b.vx * b.mass
6.70 + py += b.vy * b.mass
6.71 + pz += b.vz * b.mass
6.72 +
6.73 + sun.vx = - px / solar_mass
6.74 + sun.vy = - py / solar_mass
6.75 + sun.vz = - pz / solar_mass
6.76 +
6.77 +sun = body()
6.78 +sun.x = sun.y = sun.z = sun.vx = sun.vy = sun.vz = 0.0
6.79 +sun.mass = solar_mass
6.80 +
6.81 +jupiter = body()
6.82 +jupiter.x = 4.84143144246472090e+00
6.83 +jupiter.y = -1.16032004402742839e+00
6.84 +jupiter.z = -1.03622044471123109e-01
6.85 +jupiter.vx = 1.66007664274403694e-03 * days_per_year
6.86 +jupiter.vy = 7.69901118419740425e-03 * days_per_year
6.87 +jupiter.vz = -6.90460016972063023e-05 * days_per_year
6.88 +jupiter.mass = 9.54791938424326609e-04 * solar_mass
6.89 +
6.90 +saturn = body()
6.91 +saturn.x = 8.34336671824457987e+00
6.92 +saturn.y = 4.12479856412430479e+00
6.93 +saturn.z = -4.03523417114321381e-01
6.94 +saturn.vx = -2.76742510726862411e-03 * days_per_year
6.95 +saturn.vy = 4.99852801234917238e-03 * days_per_year
6.96 +saturn.vz = 2.30417297573763929e-05 * days_per_year
6.97 +saturn.mass = 2.85885980666130812e-04 * solar_mass
6.98 +
6.99 +uranus = body()
6.100 +uranus.x = 1.28943695621391310e+01
6.101 +uranus.y = -1.51111514016986312e+01
6.102 +uranus.z = -2.23307578892655734e-01
6.103 +uranus.vx = 2.96460137564761618e-03 * days_per_year
6.104 +uranus.vy = 2.37847173959480950e-03 * days_per_year
6.105 +uranus.vz = -2.96589568540237556e-05 * days_per_year
6.106 +uranus.mass = 4.36624404335156298e-05 * solar_mass
6.107 +
6.108 +neptune = body()
6.109 +neptune.x = 1.53796971148509165e+01
6.110 +neptune.y = -2.59193146099879641e+01
6.111 +neptune.z = 1.79258772950371181e-01
6.112 +neptune.vx = 2.68067772490389322e-03 * days_per_year
6.113 +neptune.vy = 1.62824170038242295e-03 * days_per_year
6.114 +neptune.vz = -9.51592254519715870e-05 * days_per_year
6.115 +neptune.mass = 5.15138902046611451e-05 * solar_mass
6.116 +
6.117 +def main() :
6.118 + try :
6.119 + n = int(sys.argv[1])
6.120 + except :
6.121 + print "Usage: %s <N>" % sys.argv[0]
6.122 +
6.123 + bodies = [sun, jupiter, saturn, uranus, neptune]
6.124 +
6.125 + offset_momentum(bodies)
6.126 +
6.127 + print "%.9f" % energy(bodies)
6.128 +
6.129 + for i in xrange(n) :
6.130 + advance(bodies, 0.01)
6.131 +
6.132 + print "%.9f" % energy(bodies)
6.133 +
6.134 +main()
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/tests/shootout/recursive.py Sun Feb 18 01:39:11 2007 +0100
7.3 @@ -0,0 +1,35 @@
7.4 +# The Computer Language Shootout
7.5 +# http://shootout.alioth.debian.org/
7.6 +# based on bearophile's psyco program
7.7 +# slightly modified by Isaac Gouy
7.8 +
7.9 +def Ack(x, y):
7.10 + if x == 0: return y+1
7.11 + if y == 0: return Ack(x-1, 1)
7.12 + return Ack(x-1, Ack(x, y-1))
7.13 +
7.14 +def Fib(n):
7.15 + if n < 2: return 1
7.16 + return Fib(n-2) + Fib(n-1)
7.17 +
7.18 +def FibFP(n):
7.19 + if n < 2.0: return 1.0
7.20 + return FibFP(n-2.0) + FibFP(n-1.0)
7.21 +
7.22 +def Tak(x, y, z):
7.23 + if y < x: return Tak( Tak(x-1,y,z), Tak(y-1,z,x), Tak(z-1,x,y) )
7.24 + return z
7.25 +
7.26 +def TakFP(x, y, z):
7.27 + if y < x: return TakFP( TakFP(x-1.0,y,z), TakFP(y-1.0,z,x), TakFP(z-1.0,x,y) )
7.28 + return z
7.29 +
7.30 +from sys import argv, setrecursionlimit
7.31 +setrecursionlimit(20000)
7.32 +
7.33 +n = int(argv[1]) - 1
7.34 +print "Ack(3,%d):" % (n+1), Ack(3, n+1)
7.35 +print "Fib(%.1f): %.1f" % (28.0+n, FibFP(28.0+n))
7.36 +print "Tak(%d,%d,%d): %d" % (3*n, 2*n, n, Tak(3*n, 2*n, n))
7.37 +print "Fib(3):", Fib(3)
7.38 +print "Tak(3.0,2.0,1.0):", TakFP(3.0, 2.0, 1.0)