forked from SushmitaY/mca101_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.Insertion.py
More file actions
43 lines (34 loc) · 1.34 KB
/
7.Insertion.py
File metadata and controls
43 lines (34 loc) · 1.34 KB
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
def findIndex(ele,lst,indx):
'''
objective: to find the index at which the element would be inserted
input parameters: -> ele - element to be inserted
-> lst - sorted list in which element is to be inserted
-> indx - index
return value: index of the position where the given element is to be inserted so that the list remain sorted
'''
#approach : function findIndex() is called recursively untill the first element of the list becomes greater than the element to be added
if len(lst) == 0:
return indx
if lst[0] < ele:
return findIndex(ele, lst[1:], indx+1)
else:
return indx
def insertElement(ele, lst):
'''
objective: to insert a given element at correct position in a sorted list
input parameters: -> ele - element to be added
-> lst - sorted list in which element is to be added
'''
#approach : correct index is evaluated using function findIndex(), then the given element is inserted at the resultant index
indx = findIndex(ele,lst,0)
lst.insert(indx,ele)
#TEST CASES
test1 = [10,20,30,40]
insertElement(25, test1)
print("Result : ", test1)
test2 = [10,20,30,40]
insertElement(50, test2)
print("Result : ", test2)
test3 = [10,20,30,40]
insertElement(5, test3)
print("Result : ",test3)