Untitled

 avatar
unknown
plain_text
a year ago
3.6 kB
8
Indexable
from collections import defaultdict

class Item:
    def __init__(self,itemName,itemPrice):
        self.name = itemName
        self.price = itemPrice

class Restaurant:
    def __init__(self,id,coordinates):
        self.id = id
        self.coordinates = coordinates
        self.itemsMap = defaultdict(Item)
    
    def addItemWithPrice(self,item):
        self.itemsMap[item.name] = item

    def getItemPrice(self,itemId):
        return self.itemsMap[itemId].price

def getEuclideanDistance(coordinat1,coordinate2):
    x1,y1 = coordinat1[0],coordinat1[1]
    x2,y2 = coordinate2[0],coordinate2[1]

    return ((x1-x2)**2 + (y1-y2)**2)

def calculateTime(coordinate1,coordinate2,speed):
    distance = getEuclideanDistance(coordinate1,coordinate2)
    return distance/speed


class RestaurantManager:
    def __init__(self):
        self.itemRestaurantMapping = defaultdict(set)
        self.restaurantList = defaultdict(Restaurant)
        
    def getAllRestaurants(self):
        return self.restaurantList.values()
    
    def addRestaurants(self,restaurants):
        for restaurant in restaurants:
            self.restaurantList[restaurant.id] = restaurant

    
    def addItemsForRestaurant(self,restaurantId,itemsList):
        for item in itemsList:

            self.itemRestaurantMapping[item.name].add(restaurantId)
            self.restaurantList[restaurantId].addItemWithPrice(item)

    def getMinimumCostForItem(self,itemName):

        restaurantsServingItem = self.itemRestaurantMapping[itemName]

        minPrice = float("inf")
        for restaurant in restaurantsServingItem:
            itemPriceInRestaurant = self.restaurantList[restaurant].getItemPrice(itemName)
            minPrice = min(minPrice,itemPriceInRestaurant)
        
        return minPrice
    
    def getMinimumTimeForItem(self,itemName,userCoordinate,speed):

        restaurantsServingItem = self.itemRestaurantMapping[itemName]

        minTime = float("inf")

        for restaurant in restaurantsServingItem:
            minTime = min(minTime,calculateTime(self.restaurantList[restaurant].coordinates,userCoordinate,speed))


        return minTime

if __name__ == "__main__":


    '''
       Create 6 restaurants
       add items for restaurant 1 and restaurant 2

            1. Get the minimum cost for burger item => Restaurant 2 has burger at price 3, so 3 should be printed

            2. Get the minimum time for burget item => For burger, restaurant 2 is close for the user in the example
    '''

    restaurant1 = Restaurant(1,[4.5,6.8])
    restaurant2 = Restaurant(2,[3.11,8.89])
    restaurant3 = Restaurant(3,[34.5,6.2])
    restaurant4 = Restaurant(4,[21.4,7.23])
    restaurant5 = Restaurant(5,[11.5,6.8])
    restaurant6 = Restaurant(6,[14.25,11.3])

    restaurantManager = RestaurantManager()
    restaurantManager.addRestaurants([restaurant1,restaurant2,restaurant3,restaurant4,restaurant4,restaurant5,restaurant6])
    
    restaurant_1_items = [Item("Burger",5),Item("Pizza",10),Item("Momos",6.5)]
    resatuarant_2_items = [Item("Burger",3),Item("Pizza",2),Item("Momos",2.5)]

    restaurantManager.addItemsForRestaurant(1,restaurant_1_items)
    restaurantManager.addItemsForRestaurant(2,resatuarant_2_items)
    


    minPriceForItemBurger = restaurantManager.getMinimumCostForItem("Burger")
    print(minPriceForItemBurger)

    minTimeForItemBurger = restaurantManager.getMinimumTimeForItem("Burger",[3.15,8.91],2.3)
    print(minTimeForItemBurger)

    
Editor is loading...
Leave a Comment