Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Python

तीन क्रमबद्ध सरणियों में सामान्य तत्वों को खोजने के लिए पायथन कार्यक्रम?

यहां पहले हम 3 एरे बनाते हैं जो यूजर इनपुट अनसोल्ड ऐरे हैं, फिर हम सभी 3 अनसोल्ड एरे को सॉर्ट करते हैं। सरणियों का आकार n1,n2,n3 है। प्रत्येक सरणी का प्रारंभिक पता 0.i=0,j=0,k=0 है, फिर तीन सरणी के सभी तत्वों को पार करें और तीन सरणियों के तत्व की जांच करें या समान हैं या नहीं, यदि समान है तो तत्व को प्रिंट करें अन्यथा अगले तत्व पर जाएं।

उदाहरण

A = {1, 2, 3, 4, 5}
B = {2, 5, 12, 22, 7}
C = {1, 9, 2, 89, 80}

आउटपुट

2

एल्गोरिदम

commonele(A1,A2,A3,n1,n2,n3)
/* A1, A2, A3 are three integer sorted array and size1, size2, size3 are the sizes of the array.*/
Step 1: Initialize the starting indexes for A1, A2, A3.
   i=0, j=0, k=0
Step 2:  iterate through three Arrays while arrays are nonempty. 
   While(i<size1 && j<size2 && k<size3)
Step 3: then check every value of A1,A2,A3.if the value are same then print otherwise move ahead in all arrays.
Step 4: End While

उदाहरण कोड

# To print common elements in three sorted arrays
def commonele (X, Y, Z, n1, n2, n3):   
   i, j, k = 0, 0, 0    
   print("Common elements are ::>")
   while (i < n1 and j <n2 and k< n3):
      if (X[i] == Y[j] and Y[j] == Z[k]):
         print (X[i])
         i += 1
         j += 1
         k += 1
      elif X[i] < Y[j]:
         i += 1
      elif Y[j] < Z[k]:
         j += 1
      else:
         k += 1
# Driver program 
A=list()
B=list()
C=list()
n1=int(input("Enter the size of the First List ::"))
n2=int(input("Enter the size of the Second List ::"))
n3=int(input("Enter the size of the Third List ::"))
print("Enter the Element of First List ::")
for i in range(int(n1)):
   k=int(input(""))
   A.append(k)
print("Enter the Element of Second List ::")
for j in range(int(n2)):
   k1=int(input(""))
   B.append(k1)
print("Enter the Element of Third List ::")
for j in range(int(n3)):
   k1=int(input(""))
   C.append(k1)
X=sorted(A)
Y=sorted(B)
Z=sorted(C)
print("First Sorted List ::>",X)
print("Second Sorted List ::>",Y)
print("Third Sorted List ::>",Z)
commonele(X, Y, Z, n1, n2, n3)

आउटपुट

Enter the size of the First List :: 4
Enter the size of the Second List :: 4
Enter the size of the Third List :: 5
Enter the Element of First List ::
23
12
45
8
Enter the Element of Second List ::
34
8
45
120
Enter the Element of Third List ::
2	
4
8
45
1
First Sorted List ::> [8, 12, 23, 45]
Second Sorted List ::> [8, 34, 45, 120]
Third Sorted List ::> [1, 2, 4, 8, 45]
Common elements are ::>
8
45

  1. सूची में तत्वों का योग खोजने के लिए पायथन कार्यक्रम

    इस लेख में, हम दिए गए समस्या कथन को हल करने के लिए समाधान और दृष्टिकोण के बारे में जानेंगे। समस्या कथन एक इनपुट के रूप में एक सूची को देखते हुए, हमें दी गई सूची के योग की गणना करने की आवश्यकता है। यहां हमारे पास विचार करने के लिए दो दृष्टिकोण हैं यानी अंतर्निहित फ़ंक्शन का उपयोग करना और ब्रूट-फोर्

  1. दो सूचियों के चौराहे को खोजने के लिए पायथन कार्यक्रम?

    इंटरसेक्शन ऑपरेशन का मतलब है, हमें लिस्ट 1 और लिस्ट 2 से सभी सामान्य तत्वों को लेना होगा और सभी तत्वों को दूसरी तीसरी सूची में स्टोर करना होगा। List1::[1,2,3] List2::[2,3,6] List3::[2,3] एल्गोरिदम Step 1: input lists. Step 2: first traverse all the elements in the first list and check with the el

  1. दो सूचियों के सभी सामान्य तत्वों को प्रिंट करने के लिए पायथन प्रोग्राम।

    दो सूचियों को देखते हुए, दो सूचियों के सभी सामान्य तत्वों को प्रिंट करें। उदाहरण - इनपुट :L1 =[5, 6, 7, 8, 9] L2 =[5, 13, 34, 22, 90] आउटपुट :{5} स्पष्टीकरण दोनों सूची के सामान्य तत्व 5 हैं। एल्गोरिदम Step1 :दो उपयोगकर्ता इनपुट सूचियां बनाएं। Step2 :सूचियों को सेट में बदलें और फिर set1&set2.Step3 प