Collections.OrderedDict() Hacker Rank Solution – Queslers

Problem: Collections.OrderedDict() Hacker Rank Solution

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_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 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 :

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:
APPLE JUICE: Quantity bought: 2, Price: 10
Net Price: 20
CANDY: Quantity bought: 4, Price: 5
Net Price:20

Collections.OrderedDict() Hacker Rank Solution in python 2

from collections import OrderedDict
nTrans = int(raw_input())
inventories = OrderedDict()
for n in range(nTrans):
    tmp = raw_input().split()
    item = ' '.join(tmp[0:len(tmp)-1])
    price = int(tmp[-1])
    if item in inventories:
        inventories[item] += price
    else:
        inventories[item] = price
items = list(inventories.items())
items.sort()
for item in items:
    print item[0], item[1]

Collections.OrderedDict() Hacker Rank Solution in python 3

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
    item, space, quantity = input().rpartition(' ')
    d[item] = d.get(item, 0) + int(quantity)
for item, quantity in d.items():
    print(item, quantity)

Collections.OrderedDict() Hacker Rank Solution in pypy

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import OrderedDict
items = int(raw_input())
items_price = OrderedDict()
for _ in range(items):
    desc = raw_input().split()
    if not items_price.get(" ".join(desc[0:-1]),None):
        items_price[" ".join(desc[0:-1])] =0
    items_price[" ".join(desc[0:-1])] += int(desc[-1])

for i,j in items_price.items():
    print i,j

Collections.OrderedDict() Hacker Rank Solution in pypy 3

from collections import OrderedDict

d = OrderedDict()
for _ in range(int(input())):
    info = input().split(' ')
    price = int(info[-1])
    info.pop()
    item = ' '.join(info)
    price = int(price)
    if not item in d:
        d[item] = price
    else:
        d[item] += price

for pair in d:
    print(pair + ' ' + str(d[pair]))


Collections.OrderedDict() Hacker Rank Solution Review:

In our experience, we suggest you solve this Collections.OrderedDict() Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Collections.OrderedDict() problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Collections.OrderedDict() Hacker Rank Solution

Conclusion:

I hope this Collections.OrderedDict() Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution

Leave a Reply

Your email address will not be published.