Python Forum
Learning from Grokking Algorithms
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning from Grokking Algorithms
#5
(Jul-23-2024, 01:07 PM)deanhystad Wrote: Indenting in Python is syntax, not just a way to make the code look pretty. Indenting defines code blocks. In your version of binary_search the function returns None if the first guess is incorrect. This happens because the indenting for "return None" places it inside the block of code executing in the while loop. In the version posted above indenting places "return None" outside the while loop. "return None" does not execute until the "while low <= high:" loop has completed. Four spaces making all the difference here.

You make the same error in find_smallest(). Because of the indent error you return the first element instead of the smallest element. When you've removed all but the last element the for loop doesn't execute and the function returns None (default value for functions that don't execute a "return" statement). You get the error message when this happens.
Oh man... I feel like it's really a stupid mistake.

Now the codes is working fine.

1
[Finished in 64ms]
[3]
[Finished in 63ms]
Thank you very much man, now I've learn what indent is.
Reply


Messages In This Thread
Learning from Grokking Algorithms - by Zepher - Jul-23-2024, 08:01 AM
RE: Learning from Grokking Algorithms - by Zepher - Jul-23-2024, 12:43 PM
RE: Learning from Grokking Algorithms - by Zepher - Jul-23-2024, 02:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Greedy algorithms on logical problems Opensourcehacker 0 2,236 Nov-22-2020, 05:12 PM
Last Post: Opensourcehacker
  I need help with Python code to implement this algorithms Saemmanuex 1 2,659 Jul-07-2019, 02:07 PM
Last Post: DeaD_EyE
  Looking for good doc on Scraping coverage algorithms Larz60+ 0 2,536 Jan-05-2019, 03:22 PM
Last Post: Larz60+
  Python - sorting algorithms hrca 3 4,144 Nov-06-2018, 07:06 PM
Last Post: hrca
  compare algorithms tygaf 1 3,632 Feb-14-2018, 07:26 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020