指定された整数配列のすべての個別の要素を出力するPythonプログラム。
与えられた整数配列。配列の要素が重複している可能性があります。私たちのタスクは、個別の値を表示することです。
例
Input::A=[1,2,3,4,2,3,5,6] Output [1,2,3,4,5,6]
アルゴリズム
Step 1: input Array element. Step 2: Then pick all the elements one by one. Step 3: then check if the picked element is already displayed or not. Step 4: use one flag variable which initialized by 0.if the element is displayed earlier flag variable is 1 and if the element is not displayed earlier flag variable is 0. Step 5: Display distinct elements.
サンプルコード
# Python program to print all distinct
# elements in a given array
def distinctelement(A, n1):
print("Distinct Elements are ::>")
for i in range(0, n1):
c = 0
for j in range(0, i):
if (A[i] == A[j]):
c = 1
break
if (c == 0):
print(A[i])
# Driver code
A=list()
n1=int(input("Enter the size of the List ::"))
print("Enter the Element of List ::")
for i in range(int(n1)):
k=int(input(""))
A.append(k)
distinctelement(A, n1)
出力
Enter the size of the List ::4 Enter the Element of List :: 1 2 2 4 Distinct Elements are ::> 1 2 4
-
指定された文字列のすべての順列を出力するPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −文字列の可能なすべての順列を表示するために必要な文字列が与えられます。 次に、以下の実装のソリューションを見てみましょう- 例 # conversion def toString(List): return ''.join(List) # permutations def permute(a, l, r): if l == r: print (toString(a)) e
-
リストのすべてのサブリストを出力するPythonプログラム。
リストを指定して、リストのすべてのサブリストを印刷します。 例- Input : list = [1, 2, 3] Output : [], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]] アルゴリズム Step 1 : given a list. Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of th