thewitchking

Untitled

Apr 13th, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1.  import json
  2. from collections import defaultdict
  3. from datetime import datetime
  4. import requests
  5.  
  6. # -----------------------------
  7. # Load JSON
  8. # -----------------------------
  9. def load_data(url=None, raw_json=None):
  10.     if url:
  11.         return requests.get(url).json()
  12.     elif raw_json:
  13.         return json.loads(raw_json)
  14.     else:
  15.         raise ValueError("Provide URL or raw JSON")
  16.  
  17.  
  18. # -----------------------------
  19. # 1) Highest protein meal on 1 Nov 2022
  20. # -----------------------------
  21. def highest_protein_meal(data):
  22.     target_date = "2022-11-01"
  23.     max_protein = -1
  24.     meal_name = None
  25.  
  26.     for item in data:
  27.         if item["date_consumed"] == target_date:
  28.             protein = float(item["protein"])
  29.             if protein > max_protein:
  30.                 max_protein = protein
  31.                 meal_name = item["name"]
  32.  
  33.     return meal_name or "No Result"
  34.  
  35.  
  36. # -----------------------------
  37. # 2) Users exceeding 30k calories per month
  38. # -----------------------------
  39. def users_exceeding_calories(data):
  40.     monthly_user_calories = defaultdict(lambda: defaultdict(float))
  41.  
  42.     for item in data:
  43.         date_obj = datetime.strptime(item["date_consumed"], "%Y-%m-%d")
  44.         month = date_obj.strftime("%Y-%m")
  45.         user_id = item["user_id"]
  46.  
  47.         monthly_user_calories[month][user_id] += float(item["calories"])
  48.  
  49.     result = {}
  50.  
  51.     for month, users in monthly_user_calories.items():
  52.         count = sum(1 for cal in users.values() if cal > 30000)
  53.         result[month] = count
  54.  
  55.     return result
  56.  
  57.  
  58. # -----------------------------
  59. # 3) 3 Cheapest foods and their consumers
  60. # -----------------------------
  61. def cheapest_foods(data):
  62.     food_min_price = {}
  63.  
  64.     for item in data:
  65.         name = item["name"]
  66.         price = float(item["price"])
  67.         user_id = item["user_id"]
  68.  
  69.         if name not in food_min_price or price < food_min_price[name][0]:
  70.             food_min_price[name] = (price, user_id)
  71.  
  72.     sorted_foods = sorted(food_min_price.items(), key=lambda x: x[1][0])
  73.  
  74.     return sorted_foods[:3]
  75.  
  76.  
  77. # -----------------------------
  78. # Runner
  79. # -----------------------------
  80. def run_all(data):
  81.     print("1) No Result (just reading file)")
  82.     print("2) No Result (just parsing JSON)")
  83.  
  84.     # 3
  85.     print("3)", highest_protein_meal(data))
  86.  
  87.     # 4
  88.     print("4)")
  89.     monthly = users_exceeding_calories(data)
  90.     for month in sorted(monthly):
  91.         print(f"{month}: {monthly[month]}")
  92.  
  93.     # 5
  94.     print("5)")
  95.     for name, (price, user_id) in cheapest_foods(data):
  96.         print(f"{name}, {price:.2f}, {user_id}")
  97.  
  98.  
  99. # -----------------------------
  100. # MAIN
  101. # -----------------------------
  102. if __name__ == "__main__":
  103.     URL = "https://topt.al/r6cvQM"
  104.  
  105.     try:
  106.         data = load_data(url=URL)
  107.     except:
  108.         print("Paste JSON manually if URL fails")
  109.         data = []
  110.  
  111.     run_all(data)
Advertisement
Add Comment
Please, Sign In to add comment