help with this python question. You can't use break, str.endswith, list.index, keywords like: await, as, assert, class, except, lambda, and built in functions like: any, all, breakpoint, callable.   (Question in images at bottom)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Need help with this python question. You can't use break, str.endswith, list.index, keywords like: await, as, assert, class, except, lambda, and built in functions like: any, all, breakpoint, callable.

 

(Question in images at bottom)

 

Starter Code

import random



def spider_web(web_map, starting_place, destination):

    pass



def spider_web_rec(web_map, starting_place, destination, visited):

    pass



def make_spider_web(num_nodes, seed=0):

    if seed:

        random.seed(seed)

 

    web_map = {}

 

    for i in range(1, num_nodes + 1):

        web_map[f'Node {i}'] = []

 

    for i in range(1, num_nodes + 1):

        sample = random.sample(list(range(i, num_nodes + 1)), random.randint(1, num_nodes - i + 1))

        print('sample', i, sample)

        for x in sample:

            if i != x:

                web_map[f'Node {i}'].append(f'Node {x}')

                web_map[f'Node {x}'].append(f'Node {i}')

    return web_map

 

if __name__ == '__main__':

    num_nodes, seed = [int(x) for x in input('Input num_nodes, seed: ').split(',')]

    the_web = make_spider_web(num_nodes, seed)

    print(spider_web(the_web, 'Node 1', f'Node {num_nodes}'))

 

Sample Output

linux5[109]% python3 spider_web.py

Input num_nodes, seed: 5, 101

{'Node 1': ['Node 2', 'Node 3', 'Node 5', 'Node 4'], 'Node 2': ['Node 1', 'Node 4', 'Node 3'], 'Node 3': ['Node 1', 'Node 2', 'Node 4', 'Node 5'], 'Node 4': ['Node 1', 'Node 2', 'Node 3', 'Node 5'], 'Node 5': ['Node 1', 'Node 3', 'Node 4']}

['Node 1', 'Node 2', 'Node 4', 'Node 3', 'Node 5']


linux5[110]% python3 spider_web.py

Input num_nodes, seed: 8, 2323

{'Node 1': ['Node 8', 'Node 3', 'Node 5', 'Node 6', 'Node 7', 'Node 4'], 'Node 2': ['Node 6', 'Node 8'], 'Node 3': ['Node 1', 'Node 7', 'Node 6', 'Node 4'], 'Node 4': ['Node 1', 'Node 3', 'Node 8', 'Node 5'], 'Node 5': ['Node 1', 'Node 4', 'Node 8', 'Node 6'], 'Node 6': ['Node 1', 'Node 2', 'Node 3', 'Node 5', 'Node 8', 'Node 7'], 'Node 7': ['Node 1', 'Node 3', 'Node 6'], 'Node 8': ['Node 1', 'Node 2', 'Node 4', 'Node 5', 'Node 6']}

['Node 1', 'Node 8']


linux5[111]% python3 spider_web.py

Input num_nodes, seed: 5, 6554

{'Node 1': [], 'Node 2': ['Node 4', 'Node 5'], 'Node 3': [], 'Node 4': ['Node 2'], 'Node 5': ['Node 2']}

[] 

([] means no path)



Coding Standards

  1. Constants above your function definitions, outside of the "if __name__ == '__main__':" block.  
    1. A magic value is a string which is outside of a print or input statement, but is used to check a variable, so for instance:
      1. print(first_creature_name, 'has died in the fight. ') does not involve magic values.
      2. However, if my_string == 'EXIT':  exit is a magic value since it's being used to compare against variables within your code, so it should be:
        EXIT_STRING = 'EXIT'

...
if my_string == EXIT_STRING:

  1. A number is a magic value when it is not 0, 1, and if it is not 2 being used to test parity (even/odd).
  2. A number is magic if it is a position in an array, like my_array[23], where we know that at the 23rd position, there is some special data.  Instead it should be
    USERNAME_INDEX = 23

my_array[USERNAME_INDEX]

  1. Constants in mathematical formulas can either be made into official constants or kept in a formula.  
  1. Previously checked coding standards involving:
    1. snake_case_variable_names
    2. CAPITAL_SNAKE_CASE_CONSTANT_NAMES
    3. Use of whitespace (2 before and after a function, 1 for readability.)
However, if we do this, we'll end up getting a RecursionError:
RecursionError: maximum recursion depth exceeded in comparison
Don't worry we're going to fixit. Let's create a new dictionary with the nodes the same as the nodes in the web_map.
Then we'll set all of them to False. You can call this visited, seen, been_there, whatever as long as it records when we visit
a node.
Whenever you visit a node, mark it as visited, and then don't go there again.
But how do we set all this up if the function we need to call is:
de: apides_mab (mab_map, atasting place, deatination) :
Here's the next hint: Make a recursive helper function that does most of the work that is called by the function you are
required to make.
Can the signature be the same gtthe recursive helper?
er apides wab halper (wab map, Curzent place, deatination, visitad) :
Note that the signature is different The reason is because we need to keep the visited list and pass it around all the
recursive calls.
Lastiy, how do we stop? Perhaps we should have considered this first, but that's the way I wrote this so let's consider it
now. We stop in two ways. Either we exhaust all our options and everything is visited. We never actually check if
everything is visited but we will run out of options and not find a path.
But, if we do find a path, we should return a list representing the path. What is the first link in the chain of that path?
When the current_place is equal to the destination. That tells us that our search is complete and we found the end of
the path.
As the recursion unwinds we should add the current_place to the path until we have reached the top of the recursion,
then return that list to the big spider_web function. Finally that function will return that list to the caller.
Transcribed Image Text:However, if we do this, we'll end up getting a RecursionError: RecursionError: maximum recursion depth exceeded in comparison Don't worry we're going to fixit. Let's create a new dictionary with the nodes the same as the nodes in the web_map. Then we'll set all of them to False. You can call this visited, seen, been_there, whatever as long as it records when we visit a node. Whenever you visit a node, mark it as visited, and then don't go there again. But how do we set all this up if the function we need to call is: de: apides_mab (mab_map, atasting place, deatination) : Here's the next hint: Make a recursive helper function that does most of the work that is called by the function you are required to make. Can the signature be the same gtthe recursive helper? er apides wab halper (wab map, Curzent place, deatination, visitad) : Note that the signature is different The reason is because we need to keep the visited list and pass it around all the recursive calls. Lastiy, how do we stop? Perhaps we should have considered this first, but that's the way I wrote this so let's consider it now. We stop in two ways. Either we exhaust all our options and everything is visited. We never actually check if everything is visited but we will run out of options and not find a path. But, if we do find a path, we should return a list representing the path. What is the first link in the chain of that path? When the current_place is equal to the destination. That tells us that our search is complete and we found the end of the path. As the recursion unwinds we should add the current_place to the path until we have reached the top of the recursion, then return that list to the big spider_web function. Finally that function will return that list to the caller.
Our goal is to find a path (not the best path, but just any path) from A to Z You see that the green path is not the
shortest but it does let us navigate from start to finish.
How can we do this?
First, we need to know A and Z, our starting and ending points Well pass these into our function.
I'm going to use a dictionary to represent this graph. Each node (vertex, circle) will have a name, in this case "A" and 'Z"
were the names of the nodes, but in the generated maps l'm going to use "Node T, "Node 2", "Node 3, etc.
Here is an example web_map
web_map = {
"Node 1: ['Node 3, 'Node 2].
"Node 2: ['Node 1, 'Node 4].
"Node 3: [Node 11
"Node 4: [Node 2]
Node l is connected to 2 and 3 for instance, and then also note that Node 3 is connected back to Node 1. Similarly. Node
2 is connected back to Node 1.
Then there's a connection between Node 2 and Node 4 that also goes both ways. All connections in our web will be
bi-directional.
So, in order to find the path from the start to the finish we should check if there's a path recursively through any of the
nodes connected to wherever we start.
Transcribed Image Text:Our goal is to find a path (not the best path, but just any path) from A to Z You see that the green path is not the shortest but it does let us navigate from start to finish. How can we do this? First, we need to know A and Z, our starting and ending points Well pass these into our function. I'm going to use a dictionary to represent this graph. Each node (vertex, circle) will have a name, in this case "A" and 'Z" were the names of the nodes, but in the generated maps l'm going to use "Node T, "Node 2", "Node 3, etc. Here is an example web_map web_map = { "Node 1: ['Node 3, 'Node 2]. "Node 2: ['Node 1, 'Node 4]. "Node 3: [Node 11 "Node 4: [Node 2] Node l is connected to 2 and 3 for instance, and then also note that Node 3 is connected back to Node 1. Similarly. Node 2 is connected back to Node 1. Then there's a connection between Node 2 and Node 4 that also goes both ways. All connections in our web will be bi-directional. So, in order to find the path from the start to the finish we should check if there's a path recursively through any of the nodes connected to wherever we start.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY