HackerRank Python Solution - Collections Topic - OrderedDict

An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged.

Example Code:


>>> from collections import OrderedDict
>>> 
>>> ordinary_dictionary = {}
>>> ordinary_dictionary['a'] = 1
>>> ordinary_dictionary['b'] = 2
>>> ordinary_dictionary['c'] = 3
>>> ordinary_dictionary['d'] = 4
>>> ordinary_dictionary['e'] = 5
>>> 
>>> print ordinary_dictionary
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> 
>>> ordered_dictionary = OrderedDict()
>>> ordered_dictionary['a'] = 1
>>> ordered_dictionary['b'] = 2
>>> ordered_dictionary['c'] = 3
>>> ordered_dictionary['d'] = 4
>>> ordered_dictionary['e'] = 5
>>> 
>>> print ordered_dictionary
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
Task:
  • You are the manager of a supermarket.
  • You have a list of N items together with their prices that consumers bought on a particular day.
  • Your task is to print each item_name and net_price in order of its first occurrence.
    • item_name = Name of the item.
    • net_price = quantity of the item sold multiplied by the price of each item.
Input Format:
  • The first line contains the number of items, N.
  • The next N lines contain the item's name and price, separated by a space.
Constraints:
  • 0 < N <= 100
Output Format:

Print the item_name and net_price in order of its first occurrence.

Sample Input:

9
BANANA FRIES 12
POTATO CHIPS 30
APPLE JUICE 10
CANDY 5
APPLE JUICE 10
CANDY 5
CANDY 5
CANDY 5
POTATO CHIPS 30
Sample Output:

BANANA FRIES 12
POTATO CHIPS 60
APPLE JUICE 20
CANDY 20
Explanation:
  • BANANA FRIES: Quantity bought: 1, Price: 12
  • Net Price: 12
  • POTATO CHIPS: Quantity bought: 2, Price: 30
  • Net Price: 60
  • APPLE JUICE: Quantity bought: 2, Price: 10
  • Net Price: 20
  • CANDY: Quantity bought: 4, Price: 5
  • Net Price: 20
Solution:

from collections import OrderedDict

N = int(input())

itemDict = OrderedDict()

for _ in range(N):
    
    item = input().split()    
    if len(item) > 2:
        item_name = item[0] + ' ' + item[1]
        item_price = int(item[2])
    else:
        item_name = item[0]
        item_price = int(item[1])
        
    #update the itemDict ordered dictionary
    if (item_name in itemDict):
        curPrice = itemDict[item_name] + item_price
        itemDict[item_name] = curPrice
    else:
        itemDict[item_name] = item_price

print(*[ " ".join((item, str(price))) for item,price in itemDict.items()],sep='\n')
            
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...