Answer:
The program written in Python is as follows: (See Attachments for source file and output)
def cartesian(AB):
if not AB:
yield ()
else:
for newlist in AB[0]:
for product in cartesian(AB[1:]):
yield (newlist,)+product
A = []
B = []
alist = int(input("Number of items in list A (10 max): "))
while(alist>10):
alist = int(input("Number of items in list A (10 max): "))
blist = int(input("Number of items in list B (10 max): "))
while(blist>10):
blist = int(input("Number of items in list B (10 max): "))
print("Input for List A")
for i in range(alist):
userinput = int(input(": "))
A.append(userinput)
print("Input for List B")
for i in range(blist):
userinput = int(input(": "))
B.append(userinput)
print(list(cartesian([A,B])))
Explanation:
The function to print the Cartesian product is defined here
def cartesian(AB):
This following if condition returns the function while without destroying lists A and B
if not AB:
yield ()
If otherwise
else:
The following iteration generates and prints the Cartesian products
for newlist in AB[0]:
for product in cartesian(AB[1:]):
yield (newlist,)+product
<em>The main method starts here</em>
The next two lines creates two empty lists A and B
A = []
B = []
The next line prompts user for length of list A
alist = int(input("Number of items in list A (10 max): "))
The following iteration ensures that length of list A is no more than 10
while(alist>10):
alist = int(input("Number of items in list A (10 max): "))
The next line prompts user for length of list B
blist = int(input("Number of items in list B (10 max): "))
The following iteration ensures that length of list B is no more than 10
while(blist>10):
blist = int(input("Number of items in list B (10 max): "))
The next 4 lines prompts user for input and gets the input for List A
<em>print("Input for List A")</em>
<em>for i in range(alist):</em>
<em> userinput = int(input(": "))</em>
<em> A.append(userinput)</em>
The next 4 lines prompts user for input and gets the input for List B
<em>print("Input for List B") </em>
<em>for i in range(blist):</em>
<em> userinput = int(input(": "))</em>
<em> B.append(userinput)</em>
This line calls the function to print the Cartesian product
print(list(cartesian([A,B])))
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark">
txt
</span>