Skip to content

Instantly share code, notes, and snippets.

@kotatsugame
Last active March 3, 2022 06:40
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 kotatsugame/8a5a5019ce449889ba6a8e3db04ac24e to your computer and use it in GitHub Desktop.
Save kotatsugame/8a5a5019ce449889ba6a8e3db04ac24e to your computer and use it in GitHub Desktop.
HQ9+ implementation in Python3
#!/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