Advertisement
shinemic

menu_count

Nov 4th, 2019
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. def read_menus(food_cat, *menus):
  2.     """Return a string that summarized amount of items from the same category
  3.    in the menus.
  4.    >>> read_menus("food_cat.txt", "menu1.txt", "menu2.txt")
  5.    'There are 7 burgers, 4 salads and 5 desserts'
  6.    """
  7.  
  8.     categories = {}
  9.     counts = {}
  10.  
  11.     for dish, cat in map(lambda x: x.split(': '), open(food_cat).read().strip().splitlines()):
  12.         categories[dish] = cat
  13.  
  14.     for m in menus:
  15.         for dish, _ in map(lambda x: x.split(': '), open(m).read().strip().splitlines()):
  16.             counts[categories[dish]] = 1 + counts.setdefault(categories[dish], 0)
  17.  
  18.     return 'There are {Burger} burgers, {Salad} salads and {Desert} desserts'.format_map(counts)
  19.  
  20.  
  21. if __name__ == '__main__':
  22.     print(read_menus('./food_cat.txt', './menu1.txt', './menu2.txt'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement