Created
March 25, 2022 15:59
-
-
Save abenezerangelos/b30c4b9205a1a2748890d0165f6c168b to your computer and use it in GitHub Desktop.
The amazing graph recursion with 3 different mysteries questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 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,"C")) | |
# print(longest_path1(graph, "A")) | |
## 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 | |
""" | |
numbers.strip(" ") | |
class counter(): | |
def __init__(self,words): | |
self.start=words[0] | |
# 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment