sidrs

FDS (KR) - Ass 3 (Lin and Sent Search)

Sep 4th, 2024 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. def createList():
  2.     result = []
  3.     n = int(input("Enter the number of students: "))
  4.     print("Enter the Roll Numbers: ")
  5.     for i in range(0, n):
  6.         roll = int(input())
  7.         result.append(roll)
  8.     return result
  9.  
  10. # Linear Search
  11. def linearSearch(list):
  12.     flag = 0
  13.     index = 0
  14.     key = int(input("Enter the Roll Number to be checked: "))
  15.     for i in range(0, len(list) - 1):
  16.         if list[i] == key:
  17.             flag = 1
  18.             index = i
  19.             break
  20.         else:
  21.             flag = 0
  22.     if (flag == 1):
  23.         print(f"Roll Number: {key} was PRESENT at Index {index}")
  24.     elif (flag == 0):
  25.         print(f"Roll Number: {key} was ABSENT")
  26.  
  27.  
  28. ## Sentinel Search
  29. def sentinelSearch(list):
  30.     key = int(input("Enter the Roll Number to be checked: "))
  31.     size = len(list)
  32.     last = list[size-1]
  33.     list[size-1] = key
  34.     i = 0
  35.     index = 0
  36.     flag = 0
  37.     while list[i] != key:
  38.         i += 1
  39.     list[size-1] = last
  40.     if ((i < (size - 1)) or (key == list[size - 1])):
  41.         flag = 1
  42.     else:
  43.         flag = 0
  44.  
  45.     if (flag == 1):
  46.         print(f"Roll Number: {key} was PRESENT at Index {index}")
  47.     elif (flag == 0):
  48.         print(f"Roll Number: {key} was ABSENT")
  49.  
  50. rolls = createList()            
  51.  
  52. while (true):
  53.     print("--------- MENU ----------")
  54.     print("CHOICE 1: Linear Search")
  55.     print("CHOICE 2: Sentinel Search")
  56.     print("CHOICE 3: Break")
  57.     choice = input("Enter a choice: ")
  58.     if choice == 1:
  59.         linearSearch(rolls)
  60.     elif choice == 2:
  61.         sentinelSearch(rolls)
  62.     elif choice == 3:
  63.         break
  64.     else:
  65.         print("Invalid Choice")
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment