Advertisement
roman_gemini

Food kitchen

Jun 18th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # Class that represents food object
  2. class Product(object):
  3.     def __init__(self, name, calories):
  4.         self.__name = name
  5.         self.__calories = calories
  6.  
  7.     def name(self):
  8.         return self.__name
  9.  
  10.     def calories(self):
  11.         return self.__calories
  12.  
  13.     def __str__(self):
  14.         return "(%s, %d calories)" % (self.name(), self.calories())
  15.  
  16.     def __repr__(self):
  17.         return self.__str__()
  18.  
  19.  
  20. # Maximum calories per eating
  21. threshold = 100
  22.  
  23. # Available meal on a kitchen
  24. products = [
  25.     Product("Beer", 31), Product("Chocolate", 45),
  26.     Product("Wine", 11), Product("Pizza", 55),
  27.     Product("Sushi", 54), Product("Tea", 10),
  28.     Product("Cake", 35), Product("Ice", 63),
  29.     Product("Orange", 5), Product("Snickers", 80),
  30.     Product("Meat", 89), Product("Apple", 42),
  31.     Product("Orange", 5), Product("Snickers", 80),
  32.     Product("Meat", 89), Product("Apple", 42),
  33.     Product("Cherry", 38), Product("Cheese", 29),
  34.     Product("Water", 1), Product("Bread", 42)
  35. ]
  36.  
  37.  
  38. # Copy list to other list excepting object
  39. def ignore(items: list, i) -> list:
  40.     return list(item for item in items if i != item)
  41.  
  42.  
  43. # Count calories in food list
  44. def calories(items: list) -> int:
  45.     return sum(map(lambda x: x.calories(), items))
  46.  
  47.  
  48. # Generates combinations of available food
  49. def combine(items: list, other: list, threshold: int):
  50.     result = list()
  51.     result.append(other)
  52.     for char in items:
  53.         rest = ignore(items, char)
  54.         new = other + list([char])
  55.         if calories(new) > threshold:
  56.             result.append(other)
  57.             return result
  58.         else:
  59.             result += combine(rest, new, threshold)
  60.     return result
  61.  
  62.  
  63. # Fetches portion of meal from kitchen
  64. def fetch(items: list) -> list:
  65.     combinations = combine(items, list(), 100)
  66.     combinations.sort(key=lambda x: calories(x), reverse=True)
  67.     combination = (lambda x: x[0] if x else None)(combinations)
  68.     for item in combination:
  69.         items.remove(item)
  70.     return combination
  71.  
  72.  
  73. # Business logic
  74. while True:
  75.     if len(products) == 0:
  76.         print("The kitchen is empty. Nothing more to eat. Bye!")
  77.         break
  78.  
  79.     print("Food on a kitchen: ", products)
  80.  
  81.     ans = input("Do you want some food? (y/n)").lower()
  82.  
  83.     if ans == "n":
  84.         print("Ok, bye!")
  85.         break
  86.  
  87.     eat = fetch(products)
  88.  
  89.     if eat is not None:
  90.         print("You ate: ", eat)
  91.         print("Total calories: ", calories(eat))
  92.  
  93.     print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement