How can i write a python code to manipulate a Restaurant logs?

Question:

I have a Task to complete but I’m not sure how should i start.

Problem statement 1: Write python code
A restaurant keeps a log of (eater_id, foodmenu_id) for all the diners. The eater_id
is a unique number for every diner and foodmenu_id is unique for every food item
served on the menu. Write a program that reads this log file and returns the top 3
menu items consumed. If you find an eater_id with the same foodmenu_id more
than once then show an error.

I tried using my own data like a csv or json with basic things "customer_id" and "food_id". but I get confused on the read the top food consumed.
can anyone give me an idea where can I start and how to get this task done.
also I appreciate it anyone share a link or reference of similar script.

  • I have to create a log file which has customer id and food consumed.
  • I have to get top food consumed by the customer
  • a customer should not have repeated food_id in his top food consumed.

I feel like it’s easy to implement but how to start?
Thanks.

Asked By: Devein Clara

||

Answers:

Here is your homework done for you.

Here is the log file called log.txt. The left number is the eater_id and the right number is the foodmenu_id they consumed.

1,1
1,2
2,1
2,3
3,1
1,1
3,5
4,5
6,5
7,5

Here is the code.

# Create a dictionary to keep track of the food items consumed by each diner
diner_food_items = {}

# Read the log file line by line
with open("log.txt", "r") as f:
    for line in f:
        # Parse the line to get the eater_id and foodmenu_id
        eater_id, foodmenu_id = line.strip().split(",")

        # Check if the eater_id already exists in the dictionary
        if eater_id in diner_food_items:
            # If the foodmenu_id is already in the list of food items consumed
            # by the diner, show an error
            if foodmenu_id in diner_food_items[eater_id]:
                print(
                    f"Warning: diner {eater_id} has already consumed food item {foodmenu_id}, do not count."
                )
            else:
                # Add the food item to the list of food items consumed by the diner
                diner_food_items[eater_id].append(foodmenu_id)
        else:
            # Add the diner and their food item to the dictionary
            diner_food_items[eater_id] = [foodmenu_id]

# Create a dictionary to count the number of times each food item appears
# in the list of food items consumed by each diner
food_item_count = {}
for food_items in diner_food_items.values():
    for food_item in food_items:
        if food_item in food_item_count:
            food_item_count[food_item] += 1
        else:
            food_item_count[food_item] = 1

# Get the top 3 menu items consumed
top_3_menu_items = sorted(food_item_count, key=food_item_count.get, reverse=True)[:3]
print(f"Top 3 menu items consumed: {top_3_menu_items}")

When you run the code, this is what you should see.

Warning: diner 1 has already consumed food item 1, do not count
Top 3 menu items consumed: ['5', '1', '2']
Answered By: Raya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.