Code:

import sys
import math
import operator
ALPHABET = ''.join('%c'%c for c in range(256))
def out(x, y = sys.stdout): # foreign1_2
w = y.write
if (not hasattr(x, '__len__')) or (type(x) is str):
w(str(x))
else:
if type(x[0]) is str:
w(''.join(x))
elif not hasattr(x[0], '__len__'):
w(' '.join(str(X) for X in x))
else:
for X in x:
out(X,y)
w('\n')
def integers(y):
if 0 == y:
return []
result = range(abs(y))
if y < 0:
result = reversed(result)
return list(result)
assert integers(8) == [0, 1, 2, 3, 4, 5, 6, 7]
assert integers(-2) == [1, 0]
def steps(y):
'to do: implement complex steps'
STEP = (0 <= y) - (y < 0)
return list(range(-y, y + STEP, STEP))
assert [0] == steps(0)
assert [-3, -2, -1, 0, 1, 2, 3] == steps(3)
assert [3, 2, 1, 0, -1, -2, -3] == steps(-3)
def tally(y):
return len(y)
def table(u):
def table(x, y, u = u):
return [[u(X,Y) for Y in y] for X in x]
return table
def polynomial(x, y):
result = 0
for a in reversed(x):
result *= y
result += a
return result
def factorial(y):
return math.factorial(y)
def combinations(x, y):
return math.factorial(y) / (math.factorial(x) * math.factorial(y - x))
def base(x, y):
if not hasattr(x, '__len__'):
x = [x]*len(y)
w = [1]
for f in reversed(x[1:]):
w[:0] = [w[0] * f]
return sum(map(operator.mul, w, y))
def antibase(x, y):
'''
doctest. Run with bash command $ python -m doctest this_file.py
Find base 10 digits of small numbers.
>>> antibase([10]*8, 23456)
[0, 0, 0, 2, 3, 4, 5, 6]
>>>
convert $1.38 to coins.
A quarter is 2.5 times more valuable than a dime.
A dime has twice the value of a nickel.
A nickel is worth 5 pennies.
A penny is worth 1.
The output [5.0, 1.0, 0.0, 3.0, 0.0] represents
the number of quarters, dimes, nickels, pennies, and fractional pennies.
>>> US_COINS = antibase([9999, 2.5, 2, 5, 1], 138)
>>> print(US_COINS)
[5.0, 1.0, 0.0, 3.0, 0.0]
>>>
'''
n = y
base = x
cumprod = [1]
for b in reversed(base):
cumprod[:0] = [cumprod[0]*b]
result = []
for f in cumprod[:-1]:
(a,n,) = divmod(n,f,)
result.append(a)
result.append(n)
return result[1:]
def antibase2(y):
if (y < 0) or (int(y) != y):
raise ValueError('y must be non-negative integer')
result = []
while y:
result[:0] = [y & 1]
y >>= 1
return result if result else [0]
def From(x, y):
if hasattr(x,'__len__'):
result = []
for X in x:
result += [From(X,y)]
return result
return y[x]
def mfork(f, g, h):
def fork(y, f = f, g = g, h = h):
return g(f(y), h(y))
return fork
def dfork(f, g, h):
def fork(x, y, f = f, g = g, h = h):
return g(f(x, y), h(x, y))
return fork
def mhook(f, g):
def hook(y, f = f, g = g):
return f(y, g(y))
return hook
def dhook(f, g):
def hook(x, y, f = f, g = g):
return f(x, g(y))
return hook
def reflexive(u):
def reflexive(y, u = u):
return u(y, y)
return reflexive
def passive(u):
def passive(x, y, u = u):
return u(y, x)
return passive
def mat(u, v):
def at(y, u = u, v = v):
return u(v(y))
return at
def dat(u, v):
def at(x, y, u = u, v = v):
return u(v(x, y))
return at
def mcompose(u, v):
def compose(y, u = u, v = v):
return u(v(y))
return compose
def dcompose(u, v):
def compose(x, y, u = u, v = v):
return u(v(x), v(y))
return compose
def insert(u):
def insert(y, u = u):
result = y[:]
for i in reversed(range(len(y)-1)):
result = result[:i] + [u(result[i], result[i+1:][0])]
return result[0]
return insert
def mbond(m, v):
def bond(y, m = m, v = v):
return v(m, y)
return bond
def nbond(u, n):
def bond(y, n = n, u = u):
return u(y, n)
return bond
base2 = mbond(2, base)
def dotproduct(u, v):
def dotproduct(x, y, u = u, v = v):
result = [[0] * len(y[0]) for i in x]
for (i, X,) in enumerate(x):
for (j, Y,) in enumerate(zip(*y)):
result[i][j] = u(list(map(v, X, Y)))
return result
return dotproduct
mp = dotproduct(insert(operator.add), operator.mul)
assert [[11, 6], [17, 6], [20, 6]] == mp([[1,2,3],[3,2,1],[4,2,0]],[[4,1],[2,1],[1,1]])
quadratic_table = mfork( # y >= (1 + 0*x + (x**2)/4)
mat(integers, operator.neg),
table(dhook(
operator.ge,
mbond([1, 0, 0.25], polynomial))),
steps)
average = mfork(insert(operator.add), operator.truediv, tally)
# example: A is a list of even length
# average(A[len(A)//2:len(A)//2+2])

Tweet This+ 1 thisPost To Linkedin