From 83fb61e63b9fbad1d5082b893dcefbe1445116c6 Mon Sep 17 00:00:00 2001 From: Joe S <31870999+KenwoodFox@users.noreply.github.com> Date: Sun, 21 Feb 2021 00:51:59 -0500 Subject: [PATCH 1/3] Solution so far --- 7/7.9 LAB/file1.txt | 12 +++++++++ 7/7.9 LAB/main.py | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 7/7.9 LAB/file1.txt create mode 100644 7/7.9 LAB/main.py diff --git a/7/7.9 LAB/file1.txt b/7/7.9 LAB/file1.txt new file mode 100644 index 0000000..c65c870 --- /dev/null +++ b/7/7.9 LAB/file1.txt @@ -0,0 +1,12 @@ +20 +Gunsmoke +30 +The Simpsons +10 +Will & Grace +14 +Dallas +20 +Law & Order +12 +Murder, She Wrote diff --git a/7/7.9 LAB/main.py b/7/7.9 LAB/main.py new file mode 100644 index 0000000..a69aec1 --- /dev/null +++ b/7/7.9 LAB/main.py @@ -0,0 +1,61 @@ +class MutliKeyDict(dict): + """ + New Multi Key dict for this application, inherits 'dict' + """ + def __setitem__(self, key, value): # Overwrites the default __setitem__ + try: + self[key] + except KeyError: # If there is a key error (like we append the same season num twice) + super(MutliKeyDict, self).__setitem__(key, []) # Make that value a list insted, + self[key].append(value) # Append that new list and save + +def pharse_shows(file_location): + result = MutliKeyDict() + try: + with open(file_location) as f: + for line in f: + season = line.strip("\n") + title = next(f).strip("\n") + result[season] = title + except StopIteration: + print('Error pharsing last value, odd number of lines in file?') + return(result) + +def save_output_keys(showdict): + result = '' # Set the result to a blank string + showdict = dict(sorted(showdict.items())) # Sort the dict by key + + for entry in showdict: # For every dict entry in our show dict + result += '{0}: '.format(entry) # Append a new line to represent that entry season count + if isinstance(showdict[entry], list): # If the entry is a list of shows + for show in showdict[entry]: # for very entry in that list of shows + result += '{0}; '.format(show) # append that show, and a ";" + result = result[:-2] + '\n' # Chop the last ";", ugly but it works. Also add carriage return + else: # If the entry is not a list + result += '{0}'.format(showdict[entry]) + + with open('output_keys.txt', 'a') as f: + f.write(result) + +def save_output_titles(showdict): + result = [] # Set the result to a blank list + for entry in showdict: # For every dict entry in our show dict + if isinstance(showdict[entry], list): # If the entry is a list of shows + for show in showdict[entry]: # for every entry in that list of shows + result.append(show) # Append that show to our result list + else: # If the entry is not a list + result.append(showdict[entry]) # Append the result directly + + result.sort() # Sort the list + + resultstring = '' # Setup a new blank result string + for item in result: # Convert list to stirng, with \n + resultstring += str(item) + '\n' + + with open('output_titles.txt', 'a') as f: + f.write(resultstring) + +if __name__ == '__main__': + showdict = pharse_shows(input()) + save_output_keys(showdict) + save_output_titles(showdict) -- 2.43.0 From 030dd0b611052a5b2d56bd8615429456a222b6ae Mon Sep 17 00:00:00 2001 From: Joe S <31870999+KenwoodFox@users.noreply.github.com> Date: Mon, 22 Feb 2021 19:53:46 -0500 Subject: [PATCH 2/3] Make autograder happy or whatever Autograder has some issues, but these should fix them. Basicly they just reinforce that the autograder should not be doing anything wrong when running the code (being more explicit) --- 7/7.9 LAB/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/7/7.9 LAB/main.py b/7/7.9 LAB/main.py index a69aec1..9802d64 100644 --- a/7/7.9 LAB/main.py +++ b/7/7.9 LAB/main.py @@ -26,7 +26,7 @@ def save_output_keys(showdict): showdict = dict(sorted(showdict.items())) # Sort the dict by key for entry in showdict: # For every dict entry in our show dict - result += '{0}: '.format(entry) # Append a new line to represent that entry season count + result += '{0}: '.format(int(entry)) # Append a new line to represent that entry season count if isinstance(showdict[entry], list): # If the entry is a list of shows for show in showdict[entry]: # for very entry in that list of shows result += '{0}; '.format(show) # append that show, and a ";" @@ -34,7 +34,7 @@ def save_output_keys(showdict): else: # If the entry is not a list result += '{0}'.format(showdict[entry]) - with open('output_keys.txt', 'a') as f: + with open('output_keys.txt', 'w') as f: f.write(result) def save_output_titles(showdict): @@ -52,7 +52,7 @@ def save_output_titles(showdict): for item in result: # Convert list to stirng, with \n resultstring += str(item) + '\n' - with open('output_titles.txt', 'a') as f: + with open('output_titles.txt', 'w') as f: f.write(resultstring) if __name__ == '__main__': -- 2.43.0 From 8e662c58bff28a1112671c58ccb1426331abd12d Mon Sep 17 00:00:00 2001 From: Joe S <31870999+KenwoodFox@users.noreply.github.com> Date: Fri, 26 Feb 2021 00:14:07 -0500 Subject: [PATCH 3/3] Solve 7.8 LAB --- 7/7.8 LAB/input1.csv | 1 + 7/7.8 LAB/main.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 7/7.8 LAB/input1.csv create mode 100644 7/7.8 LAB/main.py diff --git a/7/7.8 LAB/input1.csv b/7/7.8 LAB/input1.csv new file mode 100644 index 0000000..82a6165 --- /dev/null +++ b/7/7.8 LAB/input1.csv @@ -0,0 +1 @@ +hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy \ No newline at end of file diff --git a/7/7.8 LAB/main.py b/7/7.8 LAB/main.py new file mode 100644 index 0000000..4e818f0 --- /dev/null +++ b/7/7.8 LAB/main.py @@ -0,0 +1,18 @@ +import csv + +with open(input(), newline='') as f: + reader = csv.reader(f) + data = list(reader)[0] + +result = {} + +for entry in data: + try: + result[entry] + + result[entry] = result[entry] + 1 + except KeyError: + result[entry] = 1 + +for key in result: + print('{0} {1}'.format(key, result[key])) -- 2.43.0