forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1Darray.py
71 lines (61 loc) · 1.53 KB
/
1Darray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
max_size = 20
def create(arr, n):
for i in range(n):
arr.append(int(input("Enter: ")))
def remove(arr):
if len(arr) > 0:
arr.pop(0)
print("\nAfter removing the first element:")
print(arr)
else:
print("Array is empty!")
def remove_at(arr):
x = int(input("\nEnter the element you want to delete: "))
if x in arr:
i = arr.index(x)
print(f"\nElement {x} found at index {i}, after removing it:")
arr.pop(i)
print(arr)
else:
print("\nElement not found!")
def replace(arr):
x1 = int(input("\nEnter an element you want to replace: "))
if x1 in arr:
i = arr.index(x1)
print("\nElement is found! Now enter the replacing element:")
x2 = int(input())
arr[i] = x2
print(arr)
else:
print("Element not found!")
def merge(a, b):
c = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] < b[j]:
c.append(a[i])
i += 1
else:
c.append(b[j])
j += 1
while i < len(a):
c.append(a[i])
i += 1
while j < len(b):
c.append(b[j])
j += 1
print(c)
if __name__ == "__main__":
a = []
b = []
n = int(input("Enter number of elements in array 1: "))
create(a, n)
print(a)
# Uncomment these to test removal and replacement
# remove(a)
# remove_at(a)
# replace(a)
m = int(input("Enter number of elements in array 2: "))
create(b, m)
print(b)
merge(a, b)