Cartesian Product
Programming challenge description:
The Cartesian product of two lists of numbers A and B is defined to be the set of all points (a,b) where a belongs in A and b belongs in B. It is usually denoted as A x B, and is called the Cartesian product since it originated in Descartes' formulation of analytic geometry.
Given two sets of real numbers, their Cartesian product comes in form of ordered pairs. e.g.
A = [1, 2, 3]
B = [4, 5]
Cartesian product is
C = [(1, 4), (1, 5), (2,4), (2.5), (3,4), (3,5)]
Now given a coordinate tuple (1-12 where indicates A[1] and indicates B) with A, B known, implement a function that returns the index of a member in Cartesian product Caccording to (10)
For example:
coordinate (1, 0)
return index: 2
coordinate (2, 1)
return index: 5
The time complexity of this algorithm should be (1)
Input:
• real number set
• real number set
• coordinate tutple (1,1)
Output:
Index of a member in AX B (Cartesian product of A and B)
If inputs are invalid, return -1
• real number set A
• real number set B
• coordinate tutple (1,1)
Output:
Index of a member in AX B (Cartesian producto
If inputs are invalid, return-1
Test 1
Test Input
['a', 'b', 'c')
[1,2,3]
(0, 1)
Expected Output
-1
Test 2
Test Input
[1, 2, 3]
[4, 5]
(2, 1)
Expected Output
5
Test Input
[1, 2, 3]
[4, 5]
(2, 1)
Expected Output
5
Test 3
Test Input
[1, 2, 3]
[4, 5]
(1, 0)
Expected Output
2