Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- example_input="John is connected to Bryant, Debra, Walter.\
- John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
- Bryant is connected to Olive, Ollie, Freda, Mercedes.\
- Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
- Mercedes is connected to Walter, Robin, Bryant.\
- Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
- Olive is connected to John, Ollie.\
- Olive likes to play The Legend of Corgi, Starfleet Commander.\
- Debra is connected to Walter, Levi, Jennie, Robin.\
- Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
- Walter is connected to John, Levi, Bryant.\
- Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
- Levi is connected to Ollie, John, Walter.\
- Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
- Ollie is connected to Mercedes, Freda, Bryant.\
- Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
- Jennie is connected to Levi, John, Freda, Robin.\
- Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
- Robin is connected to Ollie.\
- Robin likes to play Call of Arms, Dwarves and Swords.\
- Freda is connected to Olive, John, Debra.\
- Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
- def create_data_structure(string_input):
- network = {}
- lineList = string_input.split('.')
- del lineList[-1]
- for line in lineList:
- if lineList.index(line) % 2 == 0:
- name = line.split()[0]
- network[name] = [line[line.find('to') + 3:].split(', ')]
- else:
- network[line.split()[0]].append(line[line.find('play') + 5:].split(', '))
- return network
- def print_network(network):
- for item in network.iteritems():
- print item
- def get_connections(network, user):
- return network[user][0]
- def get_games_liked(network,user):
- return network[user][1]
- def add_connection(network, user_A, user_B):
- if user_A not in network or user_B not in network:
- return False
- elif user_B in network[user_A][0]:
- return network
- else:
- network[user_A][0].append(user_B)
- return network
- def add_new_user(network, user, games):
- if user in network:
- return network
- else:
- network[user] = [[], games]
- return network
- def get_secondary_connections(network, user):
- if user not in network:
- return None
- secondaryFriends = []
- for friend in network[user][0]:
- for secondaryFriend in network[friend][0]:
- if secondaryFriend not in secondaryFriends:
- secondaryFriends.append(secondaryFriend)
- return secondaryFriends
- def count_common_connections(network, user_A, user_B):
- if user_A not in network or user_B not in network:
- return False
- commonConnections = []
- for friendA in network[user_A][0]:
- for friendB in network[user_B][0]:
- if friendA == friendB and friendA not in commonConnections:
- commonConnections.append(friendA)
- return commonConnections
- # def find_path_to_friend(network, user_A, user_B, currentFriends=[]):
- # currentFriends.append(user_A)
- # if user_A == user_B:
- # currentFriends.append('eymomma')
- # return currentFriends
- # friendsOfUser_A = network[user_A][0]
- # for friend in network[user_A][0]:
- # if friend in currentFriends:
- # friendsOfUser_A.remove(friend)
- # if friendsOfUser_A == []:
- # pass
- # for friend in friendsOfUser_A:
- # print 'user_A:', user_A
- # print 'user_B:', user_B
- # print 'friend in currentFriends:', friend
- # print 'friendsOfUser_A:', friendsOfUser_A
- # return find_path_to_friend(network, friend, user_B, currentFriends)
- #
- # So..this didn't work
- # maybe you could find a way to not use a for loop but instead use list splitting...
- # the 'None' case would trace all the way back to the original set of friends, using
- # 'currentFriends' to loop throught he recursion
- def find_path_to_friend(network, user_A, user_B, currentFriends=None):
- if user_A not in network or user_B not in network:
- return None
- if currentFriends is None:
- currentFriends = []
- currentFriends.append(user_A)
- if user_A == user_B:
- return currentFriends
- friendsOfUser_A = network[user_A][0]
- for friend in network[user_A][0]:
- if friend in currentFriends:
- friendsOfUser_A.remove(friend)
- if friendsOfUser_A == [] and user_A == currentFriends[0]:
- return None
- elif friendsOfUser_A == []:
- return find_path_to_friend(network, currentFriends[currentFriends.index(user_A) - 1], user_B, currentFriends)
- else:
- for friend in friendsOfUser_A:
- return find_path_to_friend(network, friend, user_B, currentFriends)
- network = create_data_structure(example_input)
- print find_path_to_friend(network, 'John', 'Jennie')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement