Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def createList():
- result = []
- n = int(input("Enter the number of students: "))
- print("Enter the Roll Numbers: ")
- for i in range(0, n):
- roll = int(input())
- result.append(roll)
- return result
- # Linear Search
- def linearSearch(list):
- flag = 0
- index = 0
- key = int(input("Enter the Roll Number to be checked: "))
- for i in range(0, len(list) - 1):
- if list[i] == key:
- flag = 1
- index = i
- break
- else:
- flag = 0
- if (flag == 1):
- print(f"Roll Number: {key} was PRESENT at Index {index}")
- elif (flag == 0):
- print(f"Roll Number: {key} was ABSENT")
- ## Sentinel Search
- def sentinelSearch(list):
- key = int(input("Enter the Roll Number to be checked: "))
- size = len(list)
- last = list[size-1]
- list[size-1] = key
- i = 0
- index = 0
- flag = 0
- while list[i] != key:
- i += 1
- list[size-1] = last
- if ((i < (size - 1)) or (key == list[size - 1])):
- flag = 1
- else:
- flag = 0
- if (flag == 1):
- print(f"Roll Number: {key} was PRESENT at Index {index}")
- elif (flag == 0):
- print(f"Roll Number: {key} was ABSENT")
- rolls = createList()
- while (true):
- print("--------- MENU ----------")
- print("CHOICE 1: Linear Search")
- print("CHOICE 2: Sentinel Search")
- print("CHOICE 3: Break")
- choice = input("Enter a choice: ")
- if choice == 1:
- linearSearch(rolls)
- elif choice == 2:
- sentinelSearch(rolls)
- elif choice == 3:
- break
- else:
- print("Invalid Choice")
Advertisement
Add Comment
Please, Sign In to add comment