Question: 8 . 3 Midterm project - part 3 In this third 'assignment, you will write the function genGraph ( ) . Unlike the first two
Midterm project part
In this third 'assignment," you will write the function genGraph Unlike the first two assignments, you will design and implement at least one more new function to help you implement genGraph In testing the genGraph and the new functions you also need to copy your code from the last two assignments to here. Make sure you include a docstring for each new function and choose a meaningful function name.
Besides the correctness, your program will also be evaluated based on readability eg comments, variable names, structures, etc and design of functions for only This question carries points.
Important reminders and restrictions Points will be deducted if violating these restrictions.
This is an individual project.
You are expected to write all the code yourself.
No import statements allowed.
You have to use all the functions given.
You cannot change the doctrings of all functions and their names.
You cannot change the code in explorepaths and findallshortestpaths
You cannot use any builtin functions except print and type conversion functions.
You cannot use list comprehensions.
Use the template below and name the file midtermgraph.py
def genGraphS:
Generate a graph for the MCGW problem
Parameters
S : a tuple of strings
A tuple of all possible states
Returns
Dictionary key : string, value : a list of strings
A graph for the MCGW problem
# Print the graph
printTest : The graph"
graph genGraphgenStates
for node in graph:
printnode graphnode
The expected results are:
Test : The graph
EEEE WEWE
EEEW WEWW 'WWEW'
EEWE WEWE 'WEWW', 'WWWE'
EWEE WWEW 'WWWE'
EWEW WWEWWWWW
WEWE EEEE 'EEWE'
WEWW EEEW 'EEWE'
WWEW EEEW 'EWEE', 'EWEW'
WWWE EEWE 'EWEE'
WWWW EWEW
CS Midterm project part
Midterm project part
@author: Blake Johnson
@date: Fall,
def genstates:
Generate a tuple of all states for the MCGW problem
Parameters
None
Returns
Tuple of strings
All possible states
# Generate all states by iterating over every combination
states
sides EW
for m in sides:
for c in sides:
for g in sides:
for w in sides:
state m c g w
states.appendstate
return tuplestates
# testing the function
printTest : All possible states"
printgenstates
@author: Blake Johnson
@date: Fall,
def isAStateLegalstate:
Determine whether a state is legal
Parameters
state : string
A state string composed of W and E
Returns
Boolean
True if the state is legal; False otherwise
# Check if the state is composed entirely of Es
if state E lenstate:
return True
# Check for alternating pattern of W and E
for i in range lenstate:
if statei statei : # Adjacent characters should not be the same
return False
return True
# testing the function
printTest : Testing the legality of states"
printEEEE is legal:", isAStateLegalEEEE # Expected: True
printWEEE is legal:", isAStateLegalWEEE # Expected: False
printWEWE is legal:", isAStateLegalWEWE # Expected: True
printWEEW is legal:", isAStateLegalWEEE # Expected: False
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
