Question

In: Computer Science

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 of the first cell where they collide.

The parameters to this function are as follows:

  • program1 is a list of actions, representing a program.
  • start1 is a tuple representing a location and direction of the vehicle.
  • program2 is a list of actions, representing a program.
  • start2 is a tuple representing a location and direction of the vehicle.

Assumptions:

  • 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 that are in the same cell at the same time, irrespective of their directions.

Preferably do not use the zip function/ import itertools :)

Example calls to function:

>>> collision(['Drive', 'TurnR', 'Drive'], (0, 0, 'E'), ['Drive', 'Drive'], (1, 1, 'S'))
(1, 0)
>>> collision(['Drive', 'Drive', 'Drive'], (2, 2, 'N'), ['Drive', 'Drive', 'Drive'], (2, 1, 'N'))
None
>>> collision(['Drive', 'Drive', 'Drive'], (2, 2, 'N'), ['Drive', 'Drive', 'Drive'], (2, 2, 'N'))
(2, 2)
>>> collision(['Drive', 'TurnR', 'Drive'], (2, 2, 'N'), ['Drive', 'Drive', 'Drive'], (2, 1, 'N'))
(2, 3)

Solutions

Expert Solution

def collision(program1,start1,program2,start2):
direction1,direction2 = start1[2],start2[2]# Variables to store current direction of vehicle
i,j=0,0
pos1=(start1[0],start1[1]) #Position of vehicle 1
pos2=(start2[0],start2[1]) #Position of vehicle 2
while(i<len(program1) or j<len(program2)):
if pos1==pos2:
#if both positions are same then return position
return pos1
if i<len(program1):
#Execute current program of vehicle 1
if program1[i]=='Drive':
#if program is drive then move forward in current direction
if direction1=='N':
pos1=(pos1[0],pos1[1]+1)
elif direction1=='S':
pos1=(pos1[0],pos1[1]-1)
elif direction1=='E':
pos1=(pos1[0]+1,pos1[1])
elif direction1=='W':
pos1=(pos1[0]-1,pos1[1])
elif program1[i]=='TurnR':
#if program is TurnR then change direction to right
if direction1=='N':
direction1='E'
elif direction1=='E':
direction1='S'
elif direction1=='S':
direction1='W'
elif direction1=='W':
direction1='N'
elif program1[i]=='TurnL':
#if program is TurnL then change direction to left
if direction1=='N':
direction1='W'
elif direction1=='W':
direction1='S'
elif direction1=='S':
direction1='E'
elif direction1=='E':
direction1='N'
i+=1
if(j<len(program2)):
#Execute current program of vehicle 2
if program2[j]=='Drive':
#if program is drive then move forward in current direction
if direction2=='N':
pos2=(pos2[0],pos2[1]+1)
elif direction2=='S':
pos2=(pos2[0],pos2[1]-1)
elif direction2=='E':
pos2=(pos2[0]+1,pos2[1])
elif direction2=='W':
pos2=(pos2[0]-1,pos2[1])
elif program2[j]=='TurnR':
#if program is TurnR then change direction to right
if direction2=='N':
direction2='E'
elif direction2=='E':
direction2='S'
elif direction2=='S':
direction2='W'
elif direction2=='W':
direction2='N'
elif program2[j]=='TurnL':
#if program is TurnL then change direction to left
if direction2=='N':
direction2='W'
elif direction2=='W':
direction2='S'
elif direction2=='S':
direction2='E'
elif direction2=='E':
direction2='N'
j+=1
return None
print(collision(['Drive','TurnR','Drive'],(0,0,'E'),['Drive','Drive'],(1,1,'S')))
print(collision(['Drive','Drive','Drive'],(2,2,'N'),['Drive','Drive','Drive'],(2,1,'N')))
print(collision(['Drive','Drive','Drive'],(2,2,'N'),['Drive','Drive','Drive'],(2,2,'N')))
print(collision(['Drive','TurnR','Drive'],(2,2,'N'),['Drive','Drive','Drive'],(2,1,'N')))


Related Solutions

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...
. Consider this hypothesis test: H0: p1 - p2 = < 0 Ha: p1 - p2...
. Consider this hypothesis test: H0: p1 - p2 = < 0 Ha: p1 - p2 > 0 Here p1 is the population proportion of “happy” of Population 1 and p2 is the population proportion of “happy” of Population 2. Use the statistics data from a simple random sample of each of the two populations to complete the following:​​​​​​ Population 1 Population 2 Sample Size (n) 1000 1000 Number of “yes” 600 280 a. Compute the test statistic z. b....
Co-planar test is an important step in continuous collision detection. Let p0= [0,0,0], p1= [1,0,1], p2=...
Co-planar test is an important step in continuous collision detection. Let p0= [0,0,0], p1= [1,0,1], p2= [0,1,1], p3= [1,1,0] be four points and v0= [0,0,0], v1= [1,0,0], v2= [0,1,0], v3= [0,1,1] be their velocities. Please derive the formula to compute the time when these four points are co-planar. (Hint: You may use Unity or calculator to compute dot and cross products. You don’t need to give the time solution.)
1. Consider this hypothesis test: H0: p1 - p2 = < 0 Ha: p1 - p2...
1. Consider this hypothesis test: H0: p1 - p2 = < 0 Ha: p1 - p2 > 0 Here p1 is the population proportion of “happy” of Population 1 and p2 is the population proportion of “happy” of Population 2. Use the statistics data from a simple random sample of each of the two populations to complete the following:​​​​​​ Population 1 Population 2 Sample Size (n) 1000 1000 Number of “yes” 600 280 a. Compute the test statistic z. b....
Consider this hypothesis test: H0: p1 - p2 = 0 Ha: p1 - p2 > 0...
Consider this hypothesis test: H0: p1 - p2 = 0 Ha: p1 - p2 > 0 Here p1 is the population proportion of “yes” of Population 1 and p2 is the population proportion of “yes” of Population 2. Use the statistics data from a simple random sample of each of the two populations to complete the following: (8 points) Population 1 Population 2 Sample Size (n) 500 700 Number of “yes” 400 560   Compute the test statistic z. What is...
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
k1 = k2 m1=9m^2 P1=P2 What is the relationship between P1 and P2? The momentum will...
k1 = k2 m1=9m^2 P1=P2 What is the relationship between P1 and P2? The momentum will be the same? K represents kinetic energy
Assume you have two processes, P1 and P2. P1 has a high priority, P2 has a...
Assume you have two processes, P1 and P2. P1 has a high priority, P2 has a low priority. P1 and P2 have one shared semaphore (i.e., they both carry out waits and posts on the same semaphore). The processes can be interleaved in any arbitrary order (e.g. P2 could be started before P1).             i.             Explain the problem with priority inversion Briefly explain whether the processes could deadlock when:            ii. both processes run on a Linux system as...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT