We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Python
  3. Collections
  4. Collections.OrderedDict()
  5. Discussions

Collections.OrderedDict()

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • jon257
    2 months ago+ 0 comments

    I know ordered dictionaries are the norm in modern versions of py3, but added the import just for the spirit of things.

    from collections import OrderedDict as od
    
    if __name__ == '__main__':
        n = int(input())
        dic = od()
        for i in range(n):
            product, price = input().rsplit(' ', 1)
            dic[product] = int(price) + dic.get(product, 0)
        print(*(f"{k} {v}" for k, v in dic.items()), sep='\n')
    
    3|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy