Advertisement
Guest User

lesson1.py

a guest
Dec 12th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. import time
  2.  
  3.  
  4. def read_file(filename):  # defines the function to read files
  5.     with open(filename, "r") as f:
  6.         content = f.read()
  7.     return content
  8.  
  9.  
  10. def add_to_dict(filename):  # adds the file inside a dictionary (unordered)
  11.     vote_dictionary = {}
  12.     with open(filename) as f:
  13.         for line in f:
  14.             (key, val) = line.split()
  15.             vote_dictionary[(key)] = int(val)
  16.         return vote_dictionary
  17.  
  18.  
  19. def sort_dict(dict_to_sort):  # defines the function to sort the dictionary according to values
  20.     for key, value in sorted(dict_to_sort.items(), key=lambda kv: kv[1], reverse=False):
  21.         print("%s: %s" % (key, value))
  22.  
  23.  
  24. def add_points_to_dict(vote_dictionary, vote):
  25.     if vote[0] in vote_dictionary:
  26.         vote_dictionary[vote[0]] += int(vote[1])
  27.     else:
  28.         vote_dictionary[vote[0]] = int(vote[1])
  29.     return vote_dictionary
  30.  
  31.  
  32. def welcome_judge(number):
  33.     print("Hello Judge%s. Can we have your votes please?") % (number)
  34.  
  35.  
  36. def judge_read(content, judge_number, vote_dictionary):
  37.     """
  38.    Here type what does this actually do
  39.    :param content: Content of the file (list of lines)
  40.    :param judge_number:
  41.    :param vote_dictionary:
  42.    :return:
  43.    """
  44.     print "Hello Amsterdam! Here are my votes:"
  45.     vote_list = content.splitlines()
  46.     len_vote = len(vote_list)
  47.     for i, vote in enumerate(vote_list):
  48.         vote = vote.split()
  49.         vote_dictionary = add_points_to_dict(vote_dictionary, vote)
  50.         vote = vote[0] + ": " + vote[1]
  51.         if i == len_vote - 1:
  52.             print("and finally ...")
  53.             print(vote + "\n")
  54.         else:
  55.             print(vote)
  56.         return vote_dictionary
  57.  
  58. def judge_read_2(vote_dict):
  59.     """
  60.    This function takes the contents of a file and prints the votes in the expected format
  61.    :param vote_dict: vote dictionary as returned by add_to_dict
  62.    :return:
  63.    """
  64.     print "Hello Amsterdam! Here are my votes:"
  65.     number_of_entries = len(vote_dict)
  66.     counter = 0
  67.     for country, number_of_votes in sorted(vote_dict.items(), key=lambda kv: kv[1], reverse=False):
  68.         print country, number_of_votes
  69.         counter += 1
  70.         if counter == number_of_entries - 1:
  71.             print "And finally..."
  72.  
  73. def print_statement(vote_dictionary):
  74.     for key, value in sorted(vote_dictionary.items(), key=lambda kv: kv[1], reverse=False):
  75.         # time.sleep(1)
  76.         print("%s: %s" % (key, value))
  77.  
  78.  
  79. welcome_judge(1)
  80. dct1 = add_to_dict("Judge1.txt")
  81.  
  82. # judge_read(dct1, 1, vote_dictionary)
  83.  
  84. print_statement(dct1)
  85.  
  86. # judge_read("Judge1.txt", 1, vote_dictionary)
  87.  
  88. welcome_judge(2)
  89. dct2 = add_to_dict("Judge2.txt")
  90. print_statement(dct2)
  91.  
  92. dct3 = add_to_dict("Judge3.txt")
  93. print_statement(dct3)
  94.  
  95. file_contents = read_file("Judge4.txt")
  96. dct4 = add_to_dict("Judge4.txt")
  97. judge_read(content=file_contents, judge_number=1, vote_dictionary=dct1)
  98. judge_read_2(vote_dict=dct4)
  99. # read_file("Judge1.txt")
  100. # sorting1 = sort_dict(lst1)
  101. # print_statement(vote_dictionary)
  102. # judge_read(lst1, "Judge1.txt", vote_dictionary)
  103. # why is content not defined
  104. # judge_read(vote_list, "Judge1.txt", vote_dictionary)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement