Skip to content

Instantly share code, notes, and snippets.

@andrelaszlo
Last active August 29, 2015 14:08
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 andrelaszlo/36cc603dded41c9b600f to your computer and use it in GitHub Desktop.
Save andrelaszlo/36cc603dded41c9b600f to your computer and use it in GitHub Desktop.
Python version of the dc command found here http://stackoverflow.com/a/453290/98057
### Implement some commands from dc
registers = {'r': None}
stack = []
def add():
stack.append(stack.pop() + stack.pop())
def z():
stack.append(len(stack))
def less(reg):
if stack.pop() < stack.pop():
registers[reg]()
def store(reg):
registers[reg] = stack.pop()
def p():
print stack[-1]
### Python version of the following dc command: dc -f infile -e '[+z1<r]srz1<rp'
import fileinput
# The equivalent to -f: read a file and push every line to the stack
for line in fileinput.input():
stack.append(int(line.strip()))
def cmd():
add()
z()
stack.append(1)
less('r')
stack.append(cmd)
store('r')
z()
stack.append(1)
less('r')
p()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment