Created
March 1, 2017 12:50
-
-
Save dsprenkels/f982bef924dd5a49c5070bf18dcf2500 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import re | |
products = [ | |
(r'(kratten )?bier', 'bier'), | |
(r'flessen rode wijn|rood|rode wijn', 'rood'), | |
(r'(flessen witte wijn )?zoet|zoete witte wijn', 'zoet'), | |
(r'(flessen witte wijn )?droog|droge witte wijn', 'droog'), | |
(r'(flessen )?rosé|rose', 'rose' ), | |
(r'(flessen )?cola', 'cola'), | |
(r'((flessen )?cola )?light', 'light'), | |
(r'(flessen )?fanta|sinas', 'fanta'), | |
(r'(flessen )?sprite', 'sprite'), | |
(r'(flessen )?spa( rood)?', 'spa'), | |
(r'((pakken )?appel)?sap', 'sap'), | |
(r'(pakken )?(sinaasappelsap|jus)', 'jus'), | |
(r'(flessen )?cassis', 'cassis'), | |
(r'(flessen )?ice tea', 'ice tea'), | |
(r'pakken ice tea|ijsthee|ice tea pak', 'ijsthee'), | |
(r'(zakken borrel)?mix', 'mix'), | |
(r'(zakken cocktail)?nootjes|noot', 'noot'), | |
] | |
def get_matching_value(dictionary, key): | |
for item in dictionary: | |
if re.match(r'^%s$' % item[0], key): | |
return item[1] | |
if __name__ == '__main__': | |
product_amounts = dict((x, 0) for x in list(zip(*products))[1]) | |
while 1: | |
try: | |
input_string = input() | |
m = re.match('^(- )?(?P<amount>\d+)\s(?P<product>%s)$' % "|".join(list(zip(*products))[0]), input_string) | |
if not m: | |
print("Did not understand '%s'." % input_string) | |
continue | |
product = get_matching_value(products, m.group('product')) | |
product_amounts[product] += int(m.group('amount')) | |
except EOFError: | |
break | |
print('I counted:'.upper()) | |
for product in list(zip(*products))[1]: | |
if not product_amounts[product] == 0: | |
print("%s: %d" % (product, product_amounts[product])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment