Skip to content

Instantly share code, notes, and snippets.

@abenezerangelos
Created March 27, 2022 14:29
Show Gist options
  • Save abenezerangelos/a8fe4c385f19c01226119f859ed3dad3 to your computer and use it in GitHub Desktop.
Save abenezerangelos/a8fe4c385f19c01226119f859ed3dad3 to your computer and use it in GitHub Desktop.
One of the best assignments ever regardless of how much penalty i have on it. It was worth all the penalty! You can come back and refer to this and get all caught up pretty fast!
# CptS 355 - Spring 2022 - Assignment 3 - Python
# Please include your name and the names of the students with whom you discussed any of the problems in this homework
# Name:
# Collaborators:
debugging = False
def debug(*s):
if debugging:
print(*s)
## problem 1 - all_games - 8%
wsu_games = {
2018: { "WYO":(41,19), "SJSU":(31,0), "EWU":(59,24), "USC":(36,39), "UTAH":(28,24),
"ORST":(56,37), "ORE":(34,20), "STAN":(41,38), "CAL":(19,13), "COLO":(31,7),
"ARIZ":(69,28), "WASH":(15,28), "ISU":(28,26)},
2019: {"NMSU":(58,7), "UNCO":(59,17), "HOU":(31,24), "UCLA":(63,67), "UTAH":(13,38),
"ASU":(34,38), "COLO":(41,10), "ORE":(35,37), "CAL":(20,33), "STAN":(49,22),
"ORST":(54,53), "WASH":(13,31), "AFA":(21,31) },
2020: {"ORST":(38,28), "ORE":(29,43), "USC":(13,38), "UTAH":(28,45)},
2021: { "USU":(23,26), "PORT ST.":(44,24), "USC":(14,45), "UTAH":(13,24), "CAL":(21,6),
"ORST":(31,24), "STAN":(34,31), "BYU":(19,21), "ASU":(34,21), "ORE":(24,38),
"ARIZ":(44,18), "WASH":(40,13), "CMU":(21,24)} }
def all_games(wsu_games):
pgames = {}
for dates in wsu_games:
for team, score in wsu_games[dates].items():
if team not in pgames:
pgames[team]= {}
pgames[team][dates]=score
return pgames
from functools import reduce
## problem 2 - common_teams - 15%
games=all_games(wsu_games)
def common_teams(wsu_games):
pgames= all_games(wsu_games)
ngames={}
for team in pgames:
for date,scores in pgames[team].items():
if pgames[team].keys()== wsu_games.keys():
if team not in ngames:
ngames[team]=[]
ngames[team].append(scores)
return ngames
## problem 3 - get_wins - 16%
def get_wins(wsu_games, team):
new=list(map((lambda x: (x[0],x[1].get(team,(0,0)))),(wsu_games.items())))
ult=list(filter((lambda x: x[1][0] > x[1][1]), new))
return ult
## problem 4 - wins_by_year - 16%
def wins_by_year(wsu_games):
new=list(map((lambda x: (x[0],len(list(filter(lambda x: x[0]>x[1],dict(list(x[1].items())).values()))))),(wsu_games.items())))
return new
## problem 5 - longest_path - 16%
graph = {'A':{'B','C','D'}, 'B':{'C'}, 'C':{'B','E','F','G'}, 'D':{'A','E','F'},
'E':{'F'}, 'F':{'E', 'G'}, 'G':{}, 'H':{'F','G'}}
def dfs(fgraph,node, visited, count):
max= len(visited)
t=32
print(visited)
if len(fgraph[node])!=0:
for item in fgraph[node]:
if item not in visited:
print(item)
count+=1
length=dfs(fgraph=fgraph,node=item,visited=visited+[item], count=count)
if length>max:
max = length
return max
def longest_path(fgraph,node):
count=0
visited=[node]
number_for_longest_path=dfs(fgraph=fgraph,node=node,count=count,visited=visited)
return number_for_longest_path
def dfs1(fgraph,node, visited, count):
max= len(visited)
t=32
print(visited)
if len(fgraph[node])!=0:
for item in fgraph[node]:
if item not in visited:
print(item)
visited.append(item)
updated=visited
count+=1
length=dfs1(fgraph=fgraph,node=item,visited=updated, count=count)
if length>max:
max = length
return max
def longest_path2(fgraph,node):
count=0
visited=[node]
number_for_longest_path=dfs1(fgraph=fgraph,node=node,count=count,visited=visited)
return number_for_longest_path
from copy import copy
def dfs2(fgraph, node, visited, count):
max = len(visited)
t = 32
if node not in visited:
visited.append(node)
updated = visited
print(updated)
for item in fgraph[node]:
print(item)
count += 1
length = dfs2(fgraph=fgraph, node=item, visited=copy(updated), count=count)
if length > max:
max = length
if len(graph[node])==0:
return len(visited)
return max
def longest_path3(fgraph, node):
count = 0
visited = []
number_for_longest_path = dfs2(fgraph=fgraph, node=node, count=count, visited=visited)
return number_for_longest_path
def longest_path1(graph, node):
# Variable
max_length = 0
# Helper functions
def walk_paths(graph, current_node, travelled_nodes):
# Variables
max_length = len(travelled_nodes)
print(travelled_nodes)
current_length = 0
# Base case
# Reach halting (accepting) node
if len(graph[current_node]) == 0:
return len(travelled_nodes)
# Recursive case
else:
for node in graph[current_node]:
if node not in travelled_nodes:
print(node)
current_length = walk_paths(graph, node, travelled_nodes + [node])
# if current_length > max_length:
max_length = current_length
# Return max_length
return max_length
# Call helper function
max_length = walk_paths(graph, node, [node])
return max_length
print(longest_path(graph,"A"))
print("\n\n\n\n\n")
# # print(longest_path1(graph, "A"))
# print("\n\n\n\n\n\n")
# print(longest_path2(graph, "A"))
# print("\n\n\n\n\n\n")
print(longest_path3(graph,"F"))
## problem 6 - counter - 20%
numbers = """
one
one two
one two three
one two three four
one two three four five
one two three four five six
"""
text = """
Python's philosophy is summarized as
Beautiful is better than ugly
Explicit is better than implicit
Simple is better than complex
Complex is better than complicated
Readability counts
"""
# def get_next(numbers):
# for i in range(len(numbers)):
# if not numbers[i].isspace():
# while
class counter():
def method(self,words):
self.controller=True
#this line is super important it basically controls the whole class
#here i am not using len to consume the whole string but instead checking if we got to the end of the line
while self.controller==True:
self.j=self.c
while not words[self.c].isspace() and words[self.c] != "\n":
self.c+=1
self.controller=False
self.c+=1
# print(self.c)
# print("#####")
# print(self.j)
word=words[self.j:self.c-1].lower()
if word=="":
raise StopIteration
return word
def get_next(self):
try:
current = self.method(self.text)
except :
current= None
return current
def __init__(self,text):
self.j=0
self.i=0
self.c=0
self.controller=True
self.store=[]
self.text=text
self.word= self.get_next()
def __next__(self):
n=0
if self.word is None:
raise StopIteration
final=self.word
# if word is None
# print(word)
self.store.append(self.word)
for item in self.store:
if self.word == item and self.word != None:
n+=1
elif self.word == None:
return (final)
self.word = self.get_next()
# print((final,n))
return ((final,n))
def __iter__(self):
return self
# return
######################################################################
# print(list(wsu_games.values())[0]["WYO"])
# print(dict(list(wsu_games.items())[0][1].items()).values())
# print(list(wsu_games.values()))
# print(get_wins(wsu_games, "STAN"))
# print(wins_by_year(wsu_games))
rest= []
mywords= counter(text)
for words in mywords:
rest.append(words)
# print("#######$$$#$#$#$#$##$$$$$$$$$#######")
print(rest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment