Skip to content

Instantly share code, notes, and snippets.

@schas002
Last active July 18, 2016 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schas002/5e8f3bd767d21c925c95a6222edc29a0 to your computer and use it in GitHub Desktop.
Save schas002/5e8f3bd767d21c925c95a6222edc29a0 to your computer and use it in GitHub Desktop.
A golfing "framework" for Python.
stack = []
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
G = 16
H = 17
I = 18
J = 19
K = 20
L = []
M = ""
N = "\n"
O = ""
P = 3.141592653589793
Q = ""
R = ""
S = " "
T = 0
U = 0
V = 0
W = -1
X = 1
Y = 2
Z = 3
def a():
"""Add. Pop y, x: push x + y."""
y = stack.pop()
x = stack.pop()
stack.append(x + y)
def b():
"""Bring the bottom of stack to the top of stack."""
stack.append(stack.pop(0))
def c():
"""Copy the top of stack on top of stack."""
stack.append(stack[-1])
def d():
"""Divide. Pop y, x: push x / y, floored."""
y = stack.pop()
x = stack.pop()
stack.append(x // y)
def f():
"""Flip the top of stack and second-to-top of stack so that the former
becomes the latter and vice versa."""
stack[-2], stack[-1] = stack[-1], stack[-2]
def g():
"""Greater than. Pop y, x: push 1 (truthy) if y > x, else 0."""
y = stack.pop()
x = stack.pop()
stack.append(int(y > x))
def i():
"""Input a character and push its ASCII value on top of stack."""
byte = ord(sys.stdin.read(1) or '\0')
stack.append(byte)
def k():
"""Kill the stack, removing every value."""
del stack[:]
def m():
"""Multiply. Pop y, x: push x * y."""
y = stack.pop()
x = stack.pop()
stack.append(x * y)
def o():
"""Output the top of stack as a numeral."""
print str(stack.pop())
def p():
"""Print the top of stack as an ASCII character."""
print chr(stack.pop())
def q():
"""Nop."""
pass
def r():
"""Remainder. Pop y, x: push x % y."""
y = stack.pop()
x = stack.pop()
stack.append(x % y)
def s():
"""Subtract. Pop y, x: push y - x."""
y = stack.pop()
x = stack.pop()
stack.append(y - x)
def u():
"""Unbring the top of stack to the bottom of stack."""
stack.insert(0, stack.pop())
def w():
"""Push 100."""
stack.append(100)
def x():
"""Discard the top of stack."""
stack.pop()
def z():
"""Pop the top of stack, and push 1 if it is falsy and 0 otherwise."""
stack[-1] = int(not stack[-1])
def h(item):
"""Push an item to the stack."""
stack.append(item)
def e(items):
"""Execute the one-char functions corresponding to the items."""
itemlist = list(items)
for i in range(len(itemlist)):
itemlist[i] = itemlist[i] + "()"
for j in itemlist:
exec j
def j():
"""Input a number and push it on top of stack."""
stack.append(int(raw_input()))
def l():
"""Divide. Pop y, x: push x / y."""
y = stack.pop()
x = stack.pop()
stack.append(x / y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment