তিনটি ইউজার ইনপুট লিস্ট দেওয়া থাকলে, আমাদের কাজ হলো এই তিনটি লিস্ট থেকে কমন এলিমেন্ট বের করা। এখানে আমরা intersection মেথড ব্যবহার করছি।
উদাহরণ
Input: A=[2, 3, 4, 5, 6]
B=[2, 3, 7, 6, 90]
C=[2, 3, 45, 34]
Common elements=[2, 3]
অ্যালগরিদম
Step 1: তিনটি লিস্টের এলিমেন্ট ইনপুট নিন। Step 2: Intersection মেথড ব্যবহার করুন। প্রথমে লিস্টগুলোকে সেটে রূপান্তর করুন, তারপর দুটি সেটের intersection মেথড ব্যবহার করে কমন এলিমেন্ট বের করুন, তারপর এই সেটটি তৃতীয় সেটের সাথে intersection করুন।
উদাহরণ কোড
def common_ele(my_A, my_B, my_C):
my_s1 = set(my_A)
my_s2 = set(my_B)
my_s3 = set(my_C)
my_set1 = my_s1.intersection(my_s2)
output_set = my_set1.intersection(my_s3)
output_list = list(output_set)
print(output_list)
if __name__ == '__main__':
# First List
A = list()
n = int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n)):
p = int(input("Size="))
A.append(int(p))
print(A)
# Second List
B = list()
n1 = int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n1)):
p = int(input("Size="))
B.append(int(p))
print(B)
# Third Array
C = list()
n2 = int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n2)):
p = int(input("Size="))
C.append(int(p))
print(C)
# Calling Function
common_ele(A, B, C)
আউটপুট
Enter the size of the List 3 Enter the number Size= 2 [2] Size= 1 [2, 1] Size= 2 [2, 1, 2] Enter the size of the List 3 Enter the number Size= 2 [2] Size= 1 [2, 1] Size= 4 [2, 1, 4] Enter the size of the List 4 Enter the number Size= 3 [3] [] Size= 2 [3, 2] [2] Size= 1 [3, 2, 1] [1, 2] Size= 3 [3, 2, 1, 3] [1, 2]