Skip to content

Instantly share code, notes, and snippets.

@Shawn1224
Last active July 26, 2019 10:32
Show Gist options
  • Save Shawn1224/7145abe8a9419d80d7c07b1e52319f05 to your computer and use it in GitHub Desktop.
Save Shawn1224/7145abe8a9419d80d7c07b1e52319f05 to your computer and use it in GitHub Desktop.
Practice > Tutorials > 30 Days of Code > Day 8: Dictionaries and Maps

Solution of Day 8: Dictionaries and Maps

πŸ‘ŽπŸ’© What did i fuck up?

As the task describe, you may confidently wrote down some code like below what I done at first:

N = int(input())
d = dict()

for _ in range(0, N):
    name, number = input().split()
    d[name] = number

for _ in range(0, N):
    name = input()
    if name in d:
        print("{}={}".format(name, d[name]))
    else:
        print("Not found")

You press Run Rode button, it works. And then you press Submit Code and have a cup of tea 🍡...Oops Runtime Error :( occurred in test case 1 which the first input line is 100000.

πŸ™ŒπŸ° Aha! Gotcha

But once you take the full testcase and run it in your local machine, you can get the error detail:

ValueError             Traceback (most recent call last)
<ipython-input-1-bec769e0bec0> in <module>()
      3
      4 for _ in range(0, N):
----> 5     name, number = input().split()
      6     d[name] = number
      7

ValueError: not enough values to unpack (expected 2, got 1)

Once take a peek at the input testcase, you will find out that the n subsequent lines after the first line actually does not match number 100000

πŸ‘ŒOK, just need to do a little change to work. Here's an example

import sys
_ = int(input())
d = dict()

for line in sys.stdin:
    line_splited = str(line).split(" ")
    if len(line_splited) == 2:
        d[line_splited[0]] = line_splited[1].replace('\n', '')
    else:
        name = line_splited[0].replace('\n', '')
        if name in d:
            print("{}={}".format(name, d[name]))
        else:
            print("Not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment