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]
Gribouillis write Jul-23-2024, 07:44 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.