Jul-23-2024, 08:01 AM
Hello all
I'm trying to learn algorithms from Grokking Algorithms by Aditya Y. Bhargava, but when I write the code, it doesn't show the same result like the writer. Can someone pointing out what's actually the problem?
I'm trying to learn algorithms from Grokking Algorithms by Aditya Y. Bhargava, but when I write the code, it doesn't show the same result like the writer. Can someone pointing out what's actually the problem?
def binary_search(list, item): low = 0 high = len(list)-1 while low <= high: mid = int((low + high)/2) guess = list[mid] if guess == item: return mid if guess > item: high = mid - 1 else: low = mid + 1 return None my_list = [1, 3, 5, 7, 9] print (binary_search(my_list, 3)) # => 1The result supposed to be 1, but I got "None" result
None [Finished in 169ms]
def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1, len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i return smallest_index def selectionSort(arr): newArr = [] for i in range(len(arr)): smallest = findSmallest(arr) newArr.append(arr.pop(smallest)) return newArr print(selectionSort([5,3,6,2,10]))This one I got an error in code "newArr.append(arr.pop(smallest))" and "print(selectionSort([5,3,6,2,10]))"
Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python312\Hellow.py", line 16, in <module> print(selectionSort([5,3,6,2,10])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python312\Hellow.py", line 13, in selectionSort newArr.append(arr.pop(smallest)) ^^^^^^^^^^^^^^^^^ TypeError: 'NoneType' object cannot be interpreted as an integer [Finished in 173ms]