
Frogs Collision

A frog hopping along on the infinite two-dimensional grid of integers is represented as a 4-tuple of the form (x, y, dx, dy)
so that x, y
is the frog’s starting position at time t = 0
, and dx, dy
is its constant direction vector for each hop. Time advances in discrete integer steps so that each frog makes one hop at every tick of the clock.
Given two frogs frog1, frog2
, that initially stand on different squares, return the time when both frogs hop into the same square. If these two frogs never hop into the same square at the same time, this function should return None
.
Let's look at the example (0, 0, 0, 2), (0, 10, 0, 1)
. X
's are equal and don't change (dx1 == dx2 == 0
), so we may ignore them.
Your function should not contain any loops whatsoever, but the result should be computed with conditional statements and integer arithmetic.
Input: Two tuples of four integers int.
Output: Integer or None.
Examples:
assert frogs_collision((0, 0, 0, 2), (0, 10, 0, 1)) == 10 assert frogs_collision((10, 10, -1, 0), (0, 1, 0, 1)) == None assert ( frogs_collision( (620775675217287, -1862327025651882, -3, 9), (413850450144856, 2069252250724307, -2, -10), ) == 206925225072431 ) assert frogs_collision((0, 0, 0, 0), (1, 1, 3, 3)) == None
The mission was taken from Python CCPS 109. It is taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen