Day 7 - Python

Problem Statement:

You are tasked with implementing a program for a shop that manages orders for cakes, pizzas, and other foods. The shop operates on a first-come-first-serve basis but gives priority to certain types of food.

Your task is to implement the Shop class with the following specifications:

The Shop class should have two methods:

take_order(name, food_type): This method takes two parameters, name (a string) and food_type (a string). It adds the order to the shop's queue based on the priority of the food type. If the food type is "cake", it has the highest priority (1), "pizza" comes next with priority 2, and any other food type has priority 3.

serve_order(): This method serves the next order in the queue. If there are multiple orders, it serves them in the order of their priority. It prints a message in the format "Serving {food_type} for {name}".

You need to implement the main() function that reads input from the standard input and creates a Shop object to process the orders.

The input will consist of a single line containing a sequence of space-separated strings. Each pair of strings represents an order, where the first string is the name of the customer and the second string is the type of food they ordered.

Your task is to write the code that takes these orders, processes them, and serves them in the order of their priority.

Constraints

The length of the customer's name and food type strings will be at most 100 characters.

The number of orders will be at most 1000.

Example

Input:

A cake B pizza C burger

Output:

Serving cake for A

Serving pizza for B

Serving burger for C

Explanation

A ordered a cake (priority 1), so his order is served first.

B ordered a pizza (priority 2), so her order is served next.

C ordered a burger (priority 3), so his order is served last.

Test Case:

Input:

Ziva cake Dhoni pizza Raina cookies Rutu candies

Output:

Serving cake for Ziva

Serving pizza for Dhoni

Serving cookies for Raina

Serving candies for Rutu

SOLUTION:

from queue import PriorityQueue

class Shop:
    def __init__(self):
        self.queue = PriorityQueue()

    def take_order(self, name, food_type):
        if food_type.lower() == "cake":
            priority = 1
        elif food_type.lower() == "pizza":
            priority = 2
        else:
            priority = 3
        self.queue.put((priority, name, food_type))

    def serve_order(self):
        if not self.queue.empty():
            priority, name, food_type = self.queue.get()
            print(f"Serving {food_type} for {name}")

def main():
    shop = Shop()
    orders = input().split()

    for i in range(0, len(orders), 2):
        name = orders[i]
        food_type = orders[i + 1]
        shop.take_order(name, food_type)

    while not shop.queue.empty():
        shop.serve_order()

main()

Insights:
  • The program utilizes Python's PriorityQueue from the queue module to manage orders based on priority. This data structure ensures that orders are served according to their assigned priorities.
  • The program follows an object-oriented approach by defining a Shop class. This allows for better organization and encapsulation of the shop's functionalities.
  • Orders are prioritized based on the type of food ordered. Cakes have the highest priority (1), followed by pizzas (2), and all other food types (3).
  • The take_order method is responsible for adding orders to the priority queue. It accepts the customer's name and the type of food ordered as parameters.
  • The serve_order method retrieves and serves the next order in the priority queue. It prints a message indicating the type of food being served and the customer's name.
  • Orders are read from the standard input as a sequence of space-separated strings. Each pair of strings represents a customer's name and the type of food they ordered. The main function reads the input, splits it into pairs of name and food type, and passes them to the take_order method to add them to the shop's queue.
  • After all orders are taken, the program enters a loop to serve orders until the queue is empty. This ensures that all orders are processed in the correct order of priority.
Enjoy Coding as a prioritized task! Happy Weekend with good revision! :)

Comments