Skip to content

Commit

Permalink
Added Python programs Part - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakKeshari committed Apr 5, 2021
1 parent 2dd2ec2 commit e32f66c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Arrays/Array Reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''This is a Program to reverse an array
i.e. Input: 1,2,3,4,5
Output:5,4,3,2,1'''

# Taking array input
l=input().split()

#Creating reverse array
r=[]
for i in range(0,len(l)):
r.append(int(l[len(l)-i-1]))

print(r)

22 changes: 22 additions & 0 deletions Arrays/CommonElements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'''Finding common elements between two arrays in which the elements are distinct.
Eg:
1 2 3 4 5 12 8
6 7 3 4 2 9 11
O/P:
2 3 4 '''

# Taking array input
l1=input().split()
l2=input().split()

#Creating array of common elements
r=[]
for i in l1:
if i in l2:
r.append(i)

print(r)

25 changes: 25 additions & 0 deletions Arrays/duplicates_in_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Example - 1 2 3 4 6 3 3 5 5
Duplicates are -> 3 5
'''

# Taking array input
l=input().split()

# Changing string array to integer array
for i in range(0,len(l)):
l[i]=int(l[i])

newl=[]
dupli=[]

# Logic for catching duplicate elements
for i in l:
if i not in newl:
newl.append(i)
else:
if i not in dupli:
dupli.append(i)

print(dupli)
21 changes: 21 additions & 0 deletions Arrays/remove_duplicates_from_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Taking array input
l=input().split()

# Changing string array to integer array
for i in range(0,len(l)):
l[i]=int(l[i])

#sorting
l.sort()

#Removing duplicate elements
newl = []
if(len(l)>0):
newl.append(l[0])
for i in range(1,len(l)):
if l[i-1]!=l[i]:
newl.append(l[i])

print(newl)


19 changes: 19 additions & 0 deletions Arrays/subset_of_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''
Given two arrays: arr1[0..m-1] of size m and arr2[0..n-1] of size n. Task is to check whether arr2[] is a subset
of arr1[] or not. Both the arrays can be both unsorted or sorted. It may be assumed that elements in both array are distinct.
'''

# Taking array input
l1=input().split()
l2=input().split()

#Creating array of common elements
r=[]
for i in l1:
if i in l2:
r.append(i)

if r==l2:
print("Array 2 is the subset of Array 1")
else:
print("Array 2 is NOT the subset of Array 1")

0 comments on commit e32f66c

Please sign in to comment.