Last active
March 3, 2022 06:40
-
-
Save kotatsugame/8a5a5019ce449889ba6a8e3db04ac24e to your computer and use it in GitHub Desktop.
HQ9+ implementation in Python3
This file contains 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
#!/bin/python3 | |
import sys | |
if len(sys.argv)<=1: | |
print('Usage: python3 HQ9p.py codefile',file=sys.stderr) | |
exit(1) | |
with open(sys.argv[1]) as codefile: | |
code=codefile.readline().rstrip('\n') | |
accumulator=0 | |
for op in code: | |
if op=='H': | |
print('Hello, World!',flush=True) | |
elif op=='Q': | |
print(code,end='',flush=True) | |
elif op=='9': | |
for i in range(99,1,-1): | |
print(str(i) | |
+' bottles of beer on the wall, ' | |
+str(i) | |
+' bottles of beer.\n' | |
+'Take one down, pass it around, ' | |
+str(i-1) | |
+(' bottle' if i-1==1 else ' bottles') | |
+' of beer on the wall.\n',flush=True) | |
print('1 bottle of beer on the wall, ' | |
+'1 bottle of beer.\n' | |
+'Take one down, pass it around, ' | |
+'No bottles of beer on the wall.',flush=True) | |
elif op=='+': | |
accumulator+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment