Last active
August 29, 2015 14:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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