Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Collections.OrderedDict() solution in python

YASH PAL, 31 July 2024

In this Collections.orderedDict() problem solution in python 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.

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.

HackerRank Collections.OrderedDict() solution in python

Problem solution in Python 2 programming.

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]

Problem solution in Python 3 programming.

# 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)

Problem solution in pypy programming.

# 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

Problem solution in pypy3 programming.

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]))

coding problems hackerrank solutions python

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes