Created
August 9, 2014 08:02
-
-
Save yuheiomori/ce86942ffc1ea9912395 to your computer and use it in GitHub Desktop.
Split The Number (CodeEval) in Python 3.x
This file contains hidden or 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
# coding=utf-8 | |
import sys | |
import re | |
CALC_PATTERN = re.compile("([a-z]+)([+-])[a-z]+") | |
def main(): | |
with open(sys.argv[1], "r") as f: | |
for line in f: | |
digit, pattern = line.rstrip().split() | |
lp, o = CALC_PATTERN.match(pattern).groups(()) | |
l = int(digit[:len(lp)]) | |
r = int(digit[len(lp):]) | |
if o == '+': | |
print(l + r) | |
elif o == '-': | |
print(l - r) | |
else: | |
raise ValueError() | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment