SEARCH ALGORITHMS - Searchcademy Project

Hi everyone,

https://www.codecademy.com/courses/search-algorithms/projects/searchcademy

Could you help me to do step 6 ( create a while (True) loop means?)
And could you see entire my code if it works?

Thank you

while (True): is Python’s version of a do while loop. It will be an infinite loop if there is no break condition.

Though instruction 6 states, in part …

Within the if statement, create a while(True) loop.

… such a loop header is usually coded without parentheses as …

      while True:

Either with or without the parentheses, it will work.

Thank you mtf. I got it now.

2 Likes

Thank you applypye. I’ve done as your instruction. And check again the entire my code. Now it works.

1 Like

Hey there! I also have a question concerning the Searchcademy project. I wrote my code and it is totally fine but it does not work for all the control questions… I don’t see any difference in the complexity of these but my terminal simply says that certain values cannot be found. Could someone please have a look?

The project link : https://www.codecademy.com/paths/computer-science/tracks/search-algorithms/modules/searchcademy/projects/searchcademy

My code:

def sparse_search(data, search_val):
  print("Data: " + str(data))
  print("Search Value: " + str(search_val))
  first = 0
  last = len(data)-1

  while first <= last:
    mid = (first + last) // 2

    if not data[mid]:

      left = mid - 1
      right = mid + 1

      while True:
        if left < first and right > last:
          print("{} is not in the dataset".format(search_val))
          return
        
        elif right <= last and data[right]:
          mid = right
          break
        
        elif left >= first and data[left]:
          mid = left
          break

        right += 1
        left -= 1

    if data[mid] == search_val:
      print("{} found at position {}".format(search_val, mid))
      return

    if search_val < data[mid]:
      last = mid - 1
    
    if search_val > data[mid]:
      first = mid + 1
  
    break

  print("{} is not in the dataset".format(search_val))
  

I think it is because of your last break statement, you don’t need it. It will stop the code running before to check all the datas in the list.