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") # -> []...
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.
Rewrite the grade program from Question 2.2 using a user-defined function called computegrade that takes a...
Rewrite the grade program from Question 2.2 using a user-defined function called computegrade that takes a score as its parameter and returns a grade as a string. (15 points) USING PYTHON LANGUAGE >= 0.9 A >= 0.8 B >= 0.7 C >= 0.6 D < 0.6 F Run the program repeatedly as shown below to test the various different values for input. Enter score: 0.95 A Enter score: perfect Bad score Enter score: 10.0 Bad score Enter score: 0.75 C...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT