Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Created August 9, 2014 08:02
Show Gist options
  • Save yuheiomori/ce86942ffc1ea9912395 to your computer and use it in GitHub Desktop.
Save yuheiomori/ce86942ffc1ea9912395 to your computer and use it in GitHub Desktop.
Split The Number (CodeEval) in Python 3.x
# 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