Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- def read_file(filename): # defines the function to read files
- with open(filename, "r") as f:
- content = f.read()
- return content
- def add_to_dict(filename): # adds the file inside a dictionary (unordered)
- vote_dictionary = {}
- with open(filename) as f:
- for line in f:
- (key, val) = line.split()
- vote_dictionary[(key)] = int(val)
- return vote_dictionary
- def sort_dict(dict_to_sort): # defines the function to sort the dictionary according to values
- for key, value in sorted(dict_to_sort.items(), key=lambda kv: kv[1], reverse=False):
- print("%s: %s" % (key, value))
- def add_points_to_dict(vote_dictionary, vote):
- if vote[0] in vote_dictionary:
- vote_dictionary[vote[0]] += int(vote[1])
- else:
- vote_dictionary[vote[0]] = int(vote[1])
- return vote_dictionary
- def welcome_judge(number):
- print("Hello Judge%s. Can we have your votes please?") % (number)
- def judge_read(content, judge_number, vote_dictionary):
- """
- Here type what does this actually do
- :param content: Content of the file (list of lines)
- :param judge_number:
- :param vote_dictionary:
- :return:
- """
- print "Hello Amsterdam! Here are my votes:"
- vote_list = content.splitlines()
- len_vote = len(vote_list)
- for i, vote in enumerate(vote_list):
- vote = vote.split()
- vote_dictionary = add_points_to_dict(vote_dictionary, vote)
- vote = vote[0] + ": " + vote[1]
- if i == len_vote - 1:
- print("and finally ...")
- print(vote + "\n")
- else:
- print(vote)
- return vote_dictionary
- def judge_read_2(vote_dict):
- """
- This function takes the contents of a file and prints the votes in the expected format
- :param vote_dict: vote dictionary as returned by add_to_dict
- :return:
- """
- print "Hello Amsterdam! Here are my votes:"
- number_of_entries = len(vote_dict)
- counter = 0
- for country, number_of_votes in sorted(vote_dict.items(), key=lambda kv: kv[1], reverse=False):
- print country, number_of_votes
- counter += 1
- if counter == number_of_entries - 1:
- print "And finally..."
- def print_statement(vote_dictionary):
- for key, value in sorted(vote_dictionary.items(), key=lambda kv: kv[1], reverse=False):
- # time.sleep(1)
- print("%s: %s" % (key, value))
- welcome_judge(1)
- dct1 = add_to_dict("Judge1.txt")
- # judge_read(dct1, 1, vote_dictionary)
- print_statement(dct1)
- # judge_read("Judge1.txt", 1, vote_dictionary)
- welcome_judge(2)
- dct2 = add_to_dict("Judge2.txt")
- print_statement(dct2)
- dct3 = add_to_dict("Judge3.txt")
- print_statement(dct3)
- file_contents = read_file("Judge4.txt")
- dct4 = add_to_dict("Judge4.txt")
- judge_read(content=file_contents, judge_number=1, vote_dictionary=dct1)
- judge_read_2(vote_dict=dct4)
- # read_file("Judge1.txt")
- # sorting1 = sort_dict(lst1)
- # print_statement(vote_dictionary)
- # judge_read(lst1, "Judge1.txt", vote_dictionary)
- # why is content not defined
- # judge_read(vote_list, "Judge1.txt", vote_dictionary)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement