Distinct Length Pairs
Distinct Length Pairs
Program:
The program must accept M integers and N integers as the input. The program must form pairs of integers by selecting one integer from M integers and another integer from N integers. Then the program must print the number of pairs containing two integers with different numbers of digits as the output.
Input:
3 4
12 1 454
988 2 112 47
Output:
8
Program:
n1,n2 = map(int,input().split())
list1 = list(map(str,input().split()))
list2 = list(map(str,input().split()))
dict1 = dict()
dict2 = dict()
for i in list1:
try:
dict1[len(i)] += 1
except:
dict1[len(i)] = 1
for i in list2:
try:
dict2[len(i)] += 1
except:
dict2[len(i)] = 1
count = 0
for i in dict1:
for j in dict2:
if i!=j:
count += (dict1[i]*dict2[j])
print(count)
Explanation:
1. Accept the number of integers and two lists.
2. Declare two dictionaries.
3. Iterate through the first list, find the length of each number. If the length of the number exists in the dictionary, increment the count. If not, declare the length of the number in the dictionary and assign 1 to it.
4. Repeat the step 3 for the second list.
5. Iterate through the dictionaries, if the length is not the same, multiply the count and add the results.
Comments
Post a Comment