Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are employed by a new company called AlgoZon, with the task of enhancing the package delivery process. The company aims to
You are employed by a new company called "AlgoZon," with the task of enhancing the package
delivery process. The company aims to identify whether a driver has taken a circular route during
package deliveries. Your responsibility is to determine if at any point in the trip, the driver returned
to a location they had previously visited.
The monitoring system currently in use at AlgoZon tracks routes by drivers as a Linked List, where
each node has an ID integer and a timestamp in Unix format
Each node represents a package pickup zone where a driver gets packages.
To check for inefficiency, AlgoZon has assigned you the task of writing a function that will detect
whether a driver has returned to the same pickup zone. Additionally, you must also audit whether
the linked list of destinations is in chronological order; in order words, we want to detect an error
if a driver reaches a destination before the previous one in the linked list.
Write a function that takes in the first node in the monitoring linked list and returns the total time
for the cycle, or null if there isnt one. The function should throw an InvalidRouteError if any
destination in the linked list is reached before its predecessor even after a cycle
The code for a node class and InvalidRouteError class is provided below.
Code Author:
class PickupSnapshotNode:
def initself locationid: int, timestamp: int, nextnode: 'PickupSnapshotNode':
self.id locationid
self.timestamp timestamp
self.next nextnode
def getidself:
return self.id
def gettimestampself:
return self.timestamp
def getnextself:
return self.next
def setnextself node: 'PickupSnapshotNode':
self.next node
class InvalidRouteErrorException:
def initself:
superinit
def detectcyclicroutestart: PickupSnapshotNode:
pass
class TestingBase:
def printtestresultself testname, result:
color m if result else m
reset m
printfcolorresulttestnamereset
def testanswerself testname, expected, result:
if result expected:
self.printtestresulttestname, True
else:
self.printtestresulttestname, False
printfExpected: expected
Got: result
# Unit Testing Class
class TestRideSnapshotNodeTestingBase:
def rununittestsself:
self.testdetectcyclicride
self.testdetectcyclicridenocycle
self.testbackintime
self.testbackintime
self.testdetectscyclicride
self.testdetectsnoncyclicride
self.testbackintimeaftercycle
def testdetectcyclicrideself:
# Create nodes for a cyclic ride
node PickupSnapshotNode None
node PickupSnapshotNode node
node PickupSnapshotNode node
node PickupSnapshotNode node
# Call the detectcyclicride function
result detectcyclicroutenode
# Cycle is node node node
expectedanswer
self.testanswertestdetectcyclicridewithcycle", expectedanswer, result
def testdetectcyclicridenocycleself:
# Create nodes for a noncyclic ride
node PickupSnapshotNode None
node PickupSnapshotNode node
node PickupSnapshotNode node
node PickupSnapshotNode node
# Call the detectcyclicride function
result detectcyclicroutenode
# Assert that no cycle is detected result should be None or False
self.testanswertestdetectcyclicridenocycle", None, result
def testbackintimeself:
node PickupSnapshotNode None
node PickupSnapshotNode None
node PickupSnapshotNode None
node PickupSnapshotNode None
node PickupSnapshotNode None
nodesetnextnode
nodesetnextnode
nodesetnextnode
nodesetnextnode
try:
result detectcyclicroutenode
self.printtestresulttestBackInTime False
printExpected to get error but got: result
except InvalidRouteError:
self.printtestresulttestBackInTime True
def testbackintimeself:
node PickupSnapshotNode None
node PickupSnapshotNode None
node PickupSnapshotNode None
node PickupSnapshotNode None
node PickupSnapshotNode None
nodesetnextnode
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started