Question

In: Computer Science

QUESTION: write a program for the function : def collision(program1, start1, program2, start2): - takes program1...

QUESTION:

write a program for the function : def collision(program1, start1, program2, start2):

- takes program1 and start1 of a vehicle and program2 and start2 of another vehicle and checks whether the two vehicles will collide

- if a collision occurs then a tuple must be returned containing (x,y) coordinates of the cell where the collision has occurred   

- if the two vehicles collide in multiple cells the coordinates of the first cell must be returned where the collision has occurred

- if the vehicles never collide then None must be returned

FOR THIS QUESTION IS CAN BE ASSUMED:

- both vehicles start executing their programs at the same time

- both vehicles perform actions at the same rate

- all actions take the same amount of time

- once a vehicle completes its program it remains in the same location

- a collision occurs when two vehicles are in the same cell at the same time, irrespective of their directions

EXAMPLE CALLS:

- collision([‘Drive', ‘TurnR', ‘Drive’], (0, 0, ‘E’), [‘Drive', ‘Drive’], (1, 1, ‘S’)) returns (1, 0)

- collision([‘Drive', ‘Drive', ‘Drive’], (2, 2, ‘N’), [‘Drive', ‘Drive', ‘Drive’], (2, 1, ‘N’)) returns None

- collision([‘Drive', ‘Drive', ‘Drive’], (2, 2, ‘N’), [‘Drive', ‘Drive', ‘Drive’], (2, 2, ‘N’)) returns (2, 2)

- collision([‘Drive', ‘TurnR', ‘Drive’], (2, 2, ‘N’), [‘Drive', ‘Drive', ‘Drive’], (2, 1, ‘N’)) returns (2, 3)

Solutions

Expert Solution

Please find the solution below:

  1. All example calls have been tested. I have run some more random scenarios.
  2. I have added comments in the program. Please increase/remove as per your convenience
  3. If importing "itertools" is allowed, please import itertools and use itertools.zip_longest method instead of zip in the for loop. It will cover the case where 1 vehicle was in its final position but the other one was moving and collided with it. I have not made any imports as it was not specified in the question.

SOLUTION:

#move the vehicle by one space in the current direction
def move(position):
    if(position[2] == 'E'):
        position[0] = position[0] + 1 
    elif(position[2] == 'N'):
        position[1] = position[1] + 1
    elif(position[2] == 'S'):
        position[1] = position[1] - 1
    elif(position[2] == 'W'):
        position[0] = position[0] - 1
        
#changing the direction of the vehicle
def changeDirection(position, action):
    if(action == "TurnR" and position[2] == 'E'):
        position[2] = 'S'
    elif(action == "TurnR" and position[2] == 'S'):
        position[2] = 'W'
    elif(action == "TurnR" and position[2] == 'W'):
        position[2] = 'N'
    elif(action == "TurnR" and position[2] == 'N'):
        position[2] = 'E'
    elif(action == "TurnL" and position[2] == 'E'):
        position[2] = 'N'
    elif(action == "TurnL" and position[2] == 'N'):
        position[2] = 'W'
    elif(action == "TurnL" and position[2] == 'W'):
        position[2] = 'S'
    elif(action == "TurnL" and position[2] == 'S'):
        position[2] = 'E'

#detect collision based on programs of the vehicles
def collision(program1, start1, program2, start2):
    position1 = list(start1)
    position2 = list(start2)
    
    # checking collision for initial position of vehicles
    if(position1[0]==position2[0] and position1[1]==position2[1]):
            return ((position1[0],position2[0]))
    
    #tracking simultaneous movement of vehicles
    for x,y in zip(program1,program2):
        if(x=="Drive"):
            move(position1)
        elif(x=="TurnR" or x=="TurnL"):
            changeDirection(position1,x)
        if(y=="Drive"):
            move(position2)
        elif(y=="TurnR" or y=="TurnL"):
            changeDirection(position2,y)
        #checking if vehicles collide after finishing one action
        if(position1[0]==position2[0] and position1[1]==position2[1]):
            return (position1[0],position1[1])
    
    #vehicles did not collide throughout programs
    return "None"

Thanks


Related Solutions

Write a function collision(p1, start1, p2, start2) that takes p1 and start1 of one vehicle, and...
Write a function collision(p1, start1, p2, start2) that takes p1 and start1 of one vehicle, and p2 and start2 of another vehicle, and checks whether the two vehicles will collide (i.e., at some stage they will be in the same cell at the same time). The function should return a tuple containing the (x,y) coordinates of the cell where they collide or None if they do not ever collide. If the two vehicles collide in multiple cells, return the coordinates...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Starting from mynewton write a function program mysymnewton that takes as its input a symbolic function...
Starting from mynewton write a function program mysymnewton that takes as its input a symbolic function f and the ordinary variables x0 and n. Let the program take the symbolic derivative f ′ , and then use subs to proceed with Newton’s method. Test it on f(x) = x 3 − 4 starting with x0 = 2. Turn in the program and a brief summary of the results
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
Exercise: Write a program named factorial.py that contains the following two functions: def while_factorial(num) def for_factorial(num)...
Exercise: Write a program named factorial.py that contains the following two functions: def while_factorial(num) def for_factorial(num) These should calculate the factorial of a given number represented by the argument num using a while loop and a for loop respectively.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT