Write a function collision(p1, start1, p2, start2) that takes p1 and start1 of one vehicle, and...

80.2K

Verified Solution

Question

Programming

Write a function collision(p1, start1, p2,start2) that takes p1 and start1 of one vehicle, and p2and start2 of another vehicle, and checks whether the two vehicleswill collide (i.e., at some stage they will be in the same cell atthe same time). The function should return a tuplecontaining the (x,y) coordinates of the cell where theycollide or None if they do not ever collide. If the twovehicles collide in multiple cells, return the coordinates of thefirst 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 thevehicle.
  • program2 is a list of actions, representing a program.
  • start2 is a tuple representing a location and direction of thevehicle.

Assumptions:

  • Both vehicles start executing their programs at the sametime.
  • 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 samelocation.
  • A collision occurs when two vehicles that are in the same cellat the same time, irrespective of their directions.

Preferably do not use the zip function/ importitertools :)

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)

Answer & Explanation Solved by verified expert
4.1 Ratings (713 Votes)
def collisionprogram1start1program2start2    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students