Skip to content

Instantly share code, notes, and snippets.

@eungju
Created December 20, 2009 23:08
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 eungju/260676 to your computer and use it in GitHub Desktop.
Save eungju/260676 to your computer and use it in GitHub Desktop.
첫째날
10:00 소개
10:10 실습
10:30 휴식
0h 40m
#!/bin/env python
# -*- coding: utf-8 -*-
import re
class Computer:
def __init__(self):
self.rules = [
(r"(\d{2}):(\d{2})", self.reset),
(r"(.*)\s+(\d+)m", self.activity),
(r"ELAPSED\?$", self.elapsed),
(r".*", self.bypass)]
def hours_and_minutes(self, minutes):
return (minutes / 60, minutes % 60)
def reset(self, m):
self.start = int(m.group(1)) * 60 + int(m.group(2))
self.now = self.start
def activity(self, m):
description = m.group(1)
duration = int(m.group(2))
print "%02d:%02d" % self.hours_and_minutes(self.now), description
self.now += duration
def elapsed(self, m):
elapsed = self.now - self.start
print "%dh %dm" % self.hours_and_minutes(elapsed)
def bypass(self, m):
print m.group(0)
def evaluate(self, data):
for line in data.strip().split("\n"):
instruction = line.strip();
for (p, r) in self.rules:
m = re.match(p, instruction)
if m:
r(m)
break
PLAN = """
첫째날
10:00
소개 10m
실습 20m
휴식 10m
ELAPSED?
"""
Computer().evaluate(PLAN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment