Skip to content

Instantly share code, notes, and snippets.

@masahitojp
Created August 9, 2011 05:51
Show Gist options
  • Save masahitojp/1133482 to your computer and use it in GitHub Desktop.
Save masahitojp/1133482 to your computer and use it in GitHub Desktop.
# あああ
import StringIO
class HQ9Plus(object):
def __init__(self, src):
self._src = src
self._count = 0
def run(self):
_output = StringIO.StringIO()
for str in self._src:
if str == "H":
_output.write(self._hello())
elif str == "Q":
_output.write(self._print_source())
elif str == "9":
_output.write(self._print_99_bottles_of_beer())
elif str == "+":
_output.write(self._increment())
else:
_output.write("some error occured")
rtn = _output.getvalue()
_output.close()
return rtn
def _hello(self):
return "Hello, world\n"
def _print_source(self):
return self._src + "\n"
def _print_99_bottles_of_beer(self):
_output = StringIO.StringIO()
for i in reversed(range(1,100)):
if i == 0:
before = "no more bottles"
after = "99 bottles"
elif i == 1 :
before = "1 bottle"
after = "no more bottles"
else:
before = "%d bottles" % (i)
after = "%d bottles" % (i - 1)
if i == 0:
action = "Go to the store and buy some more"
else :
action = "Take one down and pass it around"
_output.write("%s of beer on the wall. %s of beer.\n" % (before, before))
_output.write("%s, %s of beer on the wall.\n" % (action, after))
_output.write("\n")
rtn = _output.getvalue()
_output.close()
return rtn
def _increment(self):
self._count += 1
if __name__ == '__main__':
print HQ9Plus("HQ9+Q").run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment