-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathGroupB_Practical13(python).py
More file actions
145 lines (112 loc) · 4.66 KB
/
GroupB_Practical13(python).py
File metadata and controls
145 lines (112 loc) · 4.66 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
Experiment No. 13 : Write a python program to maintain club members, sort on roll numbers in ascending order.
Write function “Ternary_Search” to search whether particular student is member of club or not.
Ternary search is modified binary search that divides array into 3 halves instead of two.
'''
# Accepting Roll Numbers of the Students
def accept_roll():
roll_no = []
no_of_students = int(input("Enter the number of students : "))
for i in range(no_of_students):
roll_no.append(int(input("Enter Roll Number of Student {0} : ".format(i+1))))
return roll_no
#<--------------------------------------------------------------------------------------------->
# Printing the Roll Numbers of the Students
def print_roll(roll_no):
for i in range(len(roll_no)):
print(roll_no[i],sep = "\n")
#<--------------------------------------------------------------------------------------------->
# Insertion Sort for Sorting the list of Roll Numbers
def insertion_sort(roll_no):
for i in range(1,len(roll_no)):
key = roll_no[i]
j = i-1;
while j >= 0 and key < roll_no[j]:
roll_no[j+1] = roll_no[j]
j -= 1
roll_no[j+1] = key
return roll_no
#<---------------------------------------------------------------------------------------------->
# Function for performing Non-Recursive Ternary Search
def NR_Ternary_Search(roll,roll_find):
left = 0
right = len(roll) - 1
while left <= right:
mid1 = left + (right - left) // 3
mid2 = left + 2 * (right - left) // 3
if roll_find == roll[left]:
return left
elif roll_find == roll[right]:
return right
elif roll_find < roll[left] or roll_find > roll[right]:
return -1
elif roll_find <= roll[mid1]:
right = mid1
elif roll_find > roll[mid1] and roll_find <= roll[mid2]:
left = mid1 + 1
right = mid2
else:
left = mid2 + 1
return -1
#<------------------------------------------------------------------------------------------------->
# Function for performing Recursive Ternary Search
def R_Ternary_Search(roll, left, right, roll_find):
if (right >= left):
mid1 = left + (right - left) // 3
mid2 = right - (right - left) // 3
if (roll[mid1] == roll_find):
return mid1
if (roll[mid2] == roll_find):
return mid2
if (roll_find < roll[mid1]):
return R_Ternary_Search(roll, left, mid1 - 1, roll_find)
elif (roll_find > roll[mid2]):
return R_Ternary_Search(roll, mid2 + 1, right, roll_find)
else:
return R_Ternary_Search(roll, mid1 + 1, mid2 - 1, roll_find)
return -1
#<---------------------------------------------------------------------------------------------------->
# Main
unsort_Roll = []
sort_Roll = []
flag = 1
while flag == 1:
print("\n---------------------MENU---------------------")
print("1. Accept Student Roll Numbers")
print("2. Display the Roll Numbers of Student")
print("3. Sort Roll Numbers from the list")
print("4. Perform Non-Recursive Ternary Search")
print("5. Perform Recursive Ternary Search")
print("6. Exit\n")
ch = int(input("Enter your choice (from 1 to 6) : "))
if ch == 1:
unsort_Roll = accept_roll()
elif ch == 2:
print_roll(unsort_Roll)
elif ch == 3:
print("Elements after performing Insertion Sort : \n")
sort_Roll = insertion_sort(unsort_Roll)
print_roll(sort_Roll)
elif ch == 4:
find_roll = int(input("Enter the Roll Number to be searched : "))
index = NR_Ternary_Search(sort_Roll,find_roll)
if index != -1:
print("The Roll Number",find_roll,"is found at position",index+1)
else:
print("Roll Number",find_roll,"nor found!!")
elif ch == 5:
find_roll = int(input("Enter the Roll Number to be searched : "))
left = 0
right = len(sort_Roll) - 1
index = R_Ternary_Search(sort_Roll,left,right,find_roll)
if index != -1:
print("The Roll Number",find_roll,"is found at position",index+1)
else:
print("Roll Number",find_roll,"nor found!!")
elif ch == 6:
print("Thanks for using this program!!")
flag=0
else:
print("Wrong choice!!")
flag = 0
#<-------------------------------------END OF PROGRAM------------------------------------------>