Compare commits

...

4 Commits

Author SHA1 Message Date
Kenwood fe4c7b3701 Merge pull request 'week-7' (#9) from week-7 into master
Reviewed-on: https://kitsunehosting.net/gitea/Kenwood/SNHU-IT-140/pulls/9
2021-02-26 00:14:33 -05:00
Joe S 8e662c58bf Solve 7.8 LAB 2021-02-26 00:14:07 -05:00
Joe S 030dd0b611 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)
2021-02-22 19:53:46 -05:00
Joe S 83fb61e63b Solution so far 2021-02-21 00:51:59 -05:00
4 changed files with 92 additions and 0 deletions

1
7/7.8 LAB/input1.csv Normal file
View File

@ -0,0 +1 @@
hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy
1 hello cat man hey dog boy Hello man cat woman dog Cat hey boy

18
7/7.8 LAB/main.py Normal file
View File

@ -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]))

12
7/7.9 LAB/file1.txt Normal file
View File

@ -0,0 +1,12 @@
20
Gunsmoke
30
The Simpsons
10
Will & Grace
14
Dallas
20
Law & Order
12
Murder, She Wrote

61
7/7.9 LAB/main.py Normal file
View File

@ -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(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 ";"
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', 'w') 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', 'w') as f:
f.write(resultstring)
if __name__ == '__main__':
showdict = pharse_shows(input())
save_output_keys(showdict)
save_output_titles(showdict)