-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.py
53 lines (39 loc) · 1.06 KB
/
practice.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
# Write a Python program to reverse an array in-place.
# def rev_array(arr):
# arr[:] = arr[::-1]
#
# array = [1, 2, 3, 4, 5]
# rev_array(array)
# print(array)
# Implement a function to find the second largest element in an array.
def second_largest(arr):
lar = 0
sec = 0
for i in arr:
if i > lar:
sec = lar
lar = i
elif i > sec and i != sec:
sec = i
return sec
print(second_largest([1,2,10,3,4,5]))
# Write a program to merge two sorted arrays into a single sorted array.
def merge_sorted_array(arr1,arr2):
merge = []
i, j = 0, 0
while i < len(arr1) and j < len(arr2):
if arr1[i] <= arr2[j]:
merge.append(arr1[i])
i += 1
else:
merge.append(arr2[j])
j += 1
while i < len(arr1):
merge.append(arr1[i])
i += 1
# Append the remaining elements from arr2, if any
while j < len(arr2):
merge.append(arr2[j])
j += 1
return merge
print(merge_sorted_array([1,2,4,5], [12,23,34,45]))