我正在参加“计算机科学与计算机简介”。在Udacity。我在课程的最后一个项目。我一直在尝试使用Python中的以下代码解决问题 -
# --------------------------- #
# Intro to CS Final Project #
# Gaming Social Network #
# --------------------------- #
#
# Background
# ==========
# You and your friend have decided to start a company that hosts a gaming
# social network site. Your friend will handle the website creation (they know
# what they are doing, having taken our web development class). However, it is
# up to you to create a data structure that manages the game-network information
# and to define several procedures that operate on the network.
#
# In a website, the data is stored in a database. In our case, however, all the
# information comes in a big string of text. Each pair of sentences in the text
# is formatted as follows:
#
# <user> is connected to <user1>, ..., <userM>.<user> likes to play <game1>, ..., <gameN>.
#
# For example:
#
# John is connected to Bryant, Debra, Walter.John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.
#
# Note that each sentence will be separated from the next by only a period. There will
# not be whitespace or new lines between sentences.
#
# Your friend records the information in that string based on user activity on
# the website and gives it to you to manage. You can think of every pair of
# sentences as defining a user's profile.
#
# Consider the data structures that we have used in class - lists, dictionaries,
# and combinations of the two (e.g. lists of dictionaries). Pick one that
# will allow you to manage the data above and implement the procedures below.
#
# You may assume that <user> is a unique identifier for a user. For example, there
# can be at most one 'John' in the network. Furthermore, connections are not
# symmetric - if 'Bob' is connected to 'Alice', it does not mean that 'Alice' is
# connected to 'Bob'.
#
# Project Description
# ====================
# Your task is to complete the procedures according to the specifications below
# as well as to implement a Make-Your-Own procedure (MYOP). You are encouraged
# to define any additional helper procedures that can assist you in accomplishing
# a task. You are encouraged to test your code by using print statements and the
# Test Run button.
# -----------------------------------------------------------------------------
# Example string input. Use it to test your code.
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 merge_lists(list1, list2):
for entry in list2:
if entry not in list1:
list1.append(entry)
return list1
def single_entry_list(list):
result = []
for e in list:
if e not in result:
result.append(e)
return result
# -----------------------------------------------------------------------------
# create_data_structure(string_input):
# Parses a block of text (such as the one above) and stores relevant
# information into a data structure. You are free to choose and design any
# data structure you would like to use to manage the information.
#
# Arguments:
# string_input: block of text containing the network information
#
# You may assume that for all the test cases we will use, you will be given the
# connections and games liked for all users listed on the right-hand side of an
# 'is connected to' statement. For example, we will not use the string
# "A is connected to B.A likes to play X, Y, Z.C is connected to A.C likes to play X."
# as a test case for create_data_structure because the string does not
# list B's connections or liked games.
#
# The procedure should be able to handle an empty string (the string '') as input, in
# which case it should return a network with no users.
#
# Return:
# The newly created network data structure
def create_data_structure(string_input):
network = {}
dig_list = string_input.split('.')[:-1]
for s in dig_list:
c = s.find(' is connected to ') # identifies connections
g = s.find(' likes to play ') # identifies games
if g == -1:
name = s[:c]
if name not in network:
network[name] = [[], []]
network[name][0] = merge_lists(network[name][0], s[c+17:].split(', '))
if c == -1:
name = s[:g]
network[name][1] = merge_lists(network[name][1], s[g+15:].split(', '))
return network
# ----------------------------------------------------------------------------- #
# Note that the first argument to all procedures below is 'network' This is the #
# data structure that you created with your create_data_structure procedure, #
# though it may be modified as you add new users or new connections. Each #
# procedure below will then modify or extract information from 'network' #
# ----------------------------------------------------------------------------- #
# -----------------------------------------------------------------------------
# get_connections(network, user):
# Returns a list of all the connections that user has
#
# Arguments:
# network: the gamer network data structure
# user: a string containing the name of the user
#
# Return:
# A list of all connections the user has.
# - If the user has no connections, return an empty list.
# - If the user is not in network, return None.
def get_connections(network, user):
if user in network:
return network[user][0]
# -----------------------------------------------------------------------------
# get_games_liked(network, user):
# Returns a list of all the games a user likes
#
# Arguments:
# network: the gamer network data structure
# user: a string containing the name of the user
#
# Return:
# A list of all games the user likes.
# - If the user likes no games, return an empty list.
# - If the user is not in network, return None.
def get_games_liked(network,user):
if user in network:
return network[user][1]
# -----------------------------------------------------------------------------
# add_connection(network, user_A, user_B):
# Adds a connection from user_A to user_B. Make sure to check that both users
# exist in network.
#
# Arguments:
# network: the gamer network data structure
# user_A: a string with the name of the user the connection is from
# user_B: a string with the name of the user the connection is to
#
# Return:
# The updated network with the new connection added.
# - If a connection already exists from user_A to user_B, return network unchanged.
# - If user_A or user_B is not in network, return False.
def add_connection(network, user_A, user_B):
if user_A not in network or user_B not in network:
return False
network[user_A][0] = merge_lists(network[user_A][0], [user_B])
return network
# -----------------------------------------------------------------------------
# add_new_user(network, user, games):
# Creates a new user profile and adds that user to the network, along with
# any game preferences specified in games. Assume that the user has no
# connections to begin with.
#
# Arguments:
# network: the gamer network data structure
# user: a string containing the name of the user to be added to the network
# games: a list of strings containing the user's favorite games, e.g.:
# ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
#
# Return:
# The updated network with the new user and game preferences added. The new user
# should have no connections.
# - If the user already exists in network, return network *UNCHANGED* (do not change
# the user's game preferences)
def add_new_user(network, user, games):
if user not in network:
network[user] = [[], games]
return network
# -----------------------------------------------------------------------------
# get_secondary_connections(network, user):
# Finds all the secondary connections (i.e. connections of connections) of a
# given user.
#
# Arguments:
# network: the gamer network data structure
# user: a string containing the name of the user
#
# Return:
# A list containing the secondary connections (connections of connections).
# - If the user is not in the network, return None.
# - If a user has no primary connections to begin with, return an empty list.
#
# NOTE:
# It is OK if a user's list of secondary connections includes the user
# himself/herself. It is also OK if the list contains a user's primary
# connection that is a secondary connection as well.
def get_secondary_connections(network, user):
if user in network:
list = []
for e in network[user][0]:
if e in network:
list = list + network[e][0]
return single_entry_list(list)
# -----------------------------------------------------------------------------
# count_common_connections(network, user_A, user_B):
# Finds the number of people that user_A and user_B have in common.
#
# Arguments:
# network: the gamer network data structure
# user_A: a string containing the name of user_A
# user_B: a string containing the name of user_B
#
# Return:
# The number of connections in common (as an integer).
# - If user_A or user_B is not in network, return False.
def count_common_connections(network, user_A, user_B):
if user_A not in network or user_B not in network:
return False
list = []
for entry in network[user_A][0]:
for e in network[user_B][0]:
if e == entry and e not in list:
list.append(e)
return len(list)
# -----------------------------------------------------------------------------
# find_path_to_friend(network, user_A, user_B):
# Finds a connections path from user_A to user_B. It has to be an existing
# path but it DOES NOT have to be the shortest path.
#
# Arguments:
# network: The network you created with create_data_structure.
# user_A: String holding the starting username ("Abe")
# user_B: String holding the ending username ("Zed")
#
# Return:
# A list showing the path from user_A to user_B.
# - If such a path does not exist, return None.
# - If user_A or user_B is not in network, return None.
#
# Sample output:
# >>> print find_path_to_friend(network, "Abe", "Zed")
# >>> ['Abe', 'Gel', 'Sam', 'Zed']
# This implies that Abe is connected with Gel, who is connected with Sam,
# who is connected with Zed.
#
# NOTE:
# You must solve this problem using recursion!
#
# Hints:
# - Be careful how you handle connection loops, for example, A is connected to B.
# B is connected to C. C is connected to B. Make sure your code terminates in
# that case.
# - If you are comfortable with default parameters, you might consider using one
# in this procedure to keep track of nodes already visited in your search. You
# may safely add default parameters since all calls used in the grading script
# will only include the arguments network, user_A, and user_B.
def find_path_to_friend(network, user_A, user_B):
l = user_A
if not isinstance(user_A, list):
if user_A in network:
if network[user_A][0] == []:
return None
if user_A == user_B:
return [user_A]
newlist = []
for e in network[user_A][0]:
if e == user_B:
return [user_A, user_B]
newlist.append([user_A, e])
l = newlist
else:
return None
for e in l:
newlist = []
if e[-1] in network:
for i in network[e[-1]][0]:
if i not in e:
if i == user_B:
return e + [i]
newlist.append(e + [i])
l = find_path_to_friend(network, newlist, user_B)
if l[-1] == user_B:
return l
# Make-Your-Own-Procedure (MYOP)
# -----------------------------------------------------------------------------
# Your MYOP should either perform some manipulation of your network data
# structure (like add_new_user) or it should perform some valuable analysis of
# your network (like path_to_friend). Don't forget to comment your MYOP. You
# may give this procedure any name you want.
# Replace this with your own procedure! You can also uncomment the lines below
# to see how your code behaves. Have fun!
net = create_data_structure(example_input)
print net
print get_connections(net, "Debra")
print get_connections(net, "Mercedes")
print get_games_liked(net, "John")
print add_connection(net, "John", "Freda")
print add_new_user(net, "Debra", [])
print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
print get_secondary_connections(net, "Mercedes")
print count_common_connections(net, "Mercedes", "John")
print find_path_to_friend(net, "John", "Ollie")
我坚持使用以下部分:
find_path_to_friend(network,user_A,user_B)
我是初学程序员,我不知道如何在代码中[用递归方法]编写上述部分。感觉像傻瓜,并寻求帮助正确编写代码。
(我是新来的,这是我的第一个查询。)
答案 0 :(得分:0)
如果你是新手,不理解递归是完全没问题的,我之前已经回答过,这里是代码:
def path_to_friend(network, user_A, user_B):
#Calls a recursive function with one more argument.
return path_to_friend_2(network, user_A, user_B, [], [])
def path_to_friend_2(network, user_A, user_B, history, output):
# add the current user to the output
output.append(user_A)
# add the connection to history in order to avoid infinite loops
history.append(user_A)
# get the connections of the current user
connections = get_connections(network, user_A)
# if a user has no connections, then remove the user from the output and break
if len(connections) == 0:
output.remove(user_A)
return None
# loop through each connection, checking for the second user
for each in connections:
# if the connection is the user we are looking for, add it to output
if each == user_B:
output.append(each)
return output
# if this connection has not already been checked, check its own connections
elif each not in history:
path = path_to_friend_2(network, each, user_B, history, output)
# as long as this path has more connections it's possible it's connectected to
# user_B
if path != None:
return output
# since no connection found remove the current user from the output array
output.remove(user_A)
return None
答案 1 :(得分:0)
感谢Houzayfa Rifai的贡献。我终于找到了解决方案。代码如下:
def find_path_to_friend(network, user_A, user_B):
if not isinstance(user_B, list):
if user_A in network:
if user_A == user_B:
return [user_A]
if user_B in network[user_A][0]:
return [user_A, user_B]
l = []
for i in network:
if user_B in network[i][0]:
l.append([i, user_B])
return find_path_to_friend(network, user_A, l)
return None
for e in user_B:
l = []
for i in network:
if e[0] in network[i][0]:
if i == user_A:
return [i] + e
k = [i] + e
l.append(k)
return find_path_to_friend(network, user_A, l)
network = {'Freda': [['Olive', 'John', 'Debra'], ['Starfleet Commander', 'Ninja Hamsters', 'Seahorse Adventures']], 'Ollie': [['Mercedes', 'Freda', 'Bryant'], ['Call of Arms', 'Dwarves and Swords', 'The Movie: The Game']], 'Debra': [['Walter', 'Levi', 'Jennie', 'Robin'], ['Seven Schemers', 'Pirates in Java Island', 'Dwarves and Swords']], 'Olive': [['John', 'Ollie'], ['The Legend of Corgi', 'Starfleet Commander']], 'Levi': [['Ollie', 'John', 'Walter'], ['The Legend of Corgi', 'Seven Schemers', 'City Comptroller: The Fiscal Dilemma']], 'Jennie': [['Levi', 'John', 'Freda', 'Robin'], ['Super Mushroom Man', 'Dinosaur Diner', 'Call of Arms']], 'Mercedes': [['Walter', 'Robin', 'Bryant'], ['The Legend of Corgi', 'Pirates in Java Island', 'Seahorse Adventures']], 'John': [['Bryant', 'Debra', 'Walter'], ['The Movie: The Game', 'The Legend of Corgi', 'Dinosaur Diner']], 'Robin': [['Ollie'], ['Call of Arms', 'Dwarves and Swords']], 'Bryant': [['Olive', 'Ollie', 'Freda', 'Mercedes'], ['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']], 'Walter': [['John', 'Levi', 'Bryant'], ['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man']]}
print find_path_to_friend(network, 'John', 'Mercedes')
虽然结果并没有提供给朋友的最短路径,但我很高兴我已经解决了这个问题。现在我将尝试改进解决方案。
感谢所有将来做出贡献的人。