Collections.OrderedDict() HackerRank Solution In Python

Problem

collections.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_dictionaryOrderedDict([('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 Foramt

The first line contains the number of items, N.
The next N lines contains 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

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

Sample Output

BANANA FRIES 12POTATO CHIPS 60APPLE JUICE 20CANDY 20

Explanation

BANANA FRIES: Quantity bought: 1, Price: 12 Net Price12
POTATO CHIPS: Quantity bought: 2, Price: 30
Net Price60
APPLE JUICE: Quantity bought: 2, Price: 10
Net Price20
CANDY: Quantity bought: 4, Price: 5
Net Price20

Solution – Collections.OrderedDict() In Python | HackerRank

from collections import OrderedDictd = OrderedDict()for _ in range(int(input())):    item = input().rpartition(' ')    if not item[0] in d:        d[item[0]] = int(item[2])    else:        d[item[0]] += int(item[2])for i in d.items():    print(i[0], i[1])

NOTE: The problem solved above, Collections.OrderedDict(), was generated by HackerRank and the solution was brought by the admin of  CodingSolutions for educational purpose. Got any issues with the code? Ask your questions in the comment box and I shall attend to it.

Comments