Dictionaries (Maps)

  • Associate key with value
  • Key & value can be any data type
  • Easily searchable by key
  • Store and retrieve data quickly

The program will store a dictionary that associates lines of input with random integers. When a line of input is read, the program will check to see if that line has already been used as a key in the dictionary. If so, it will return the value associated with that key.

If the dictionary does not contain an entry for that key, the program should generate a new random integer and store it in the dictionary, using the input line as the key.


import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line):

import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line): if line in DictExample.dictionary:

import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line): if line in DictExample.dictionary: return DictExample.dictionary[line]


import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line): if line in DictExample.dictionary: return DictExample.dictionary[line] else:




import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line): if line in DictExample.dictionary: return DictExample.dictionary[line] else: new_number = random.randint(-1000000, 1000000)



import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line): if line in DictExample.dictionary: return DictExample.dictionary[line] else: new_number = random.randint(-1000000, 1000000) DictExample.dictionary[line] = new_number


import sys
import random
class DictExample:
dictionary = {}
@staticmethod def get_output(line): if line in DictExample.dictionary: return DictExample.dictionary[line] else: new_number = random.randint(-1000000, 1000000) DictExample.dictionary[line] = new_number return new_number