pythonでHQ9+

2010年一発目。

今年もテキトーにやります。

タイトルのHQ9+

まずはインタプリタを書く
[hq9+.py]


#!/usr/bin/env python
import sys
if __name__ == "__main__":
acc = 0
line = sys.stdin.readline()
o = line[:-1]
l = len(line)
for i in range(l):
buf = ord(line[0:1])
line = line[1:]
if buf == ord("H"):
print "Hello World! ",
elif buf == ord("Q"):
print o + "",
elif buf == ord("9"):
print "99 bottles of beer on the wall, 99 bottles of beer. ",
elif buf == ord("+"):
acc += 1

続いて実行コード
[test.hq9+]


HQ9++HHQ9++++

で実行してみる


$ ./hq9+.py < test.hq9+
Hello World! HQ9++HHQ9++++ 99 bottles of beer on the wall, 99 bottles of beer. Hello World! Hello World! HQ9++HHQ9++++ 99 bottles of beer on the wall, 99 bottles of beer.

インタプリタは18行でいけた