FAQ: Survey of Computer Science - The Tale of Kenny - Optimization

This community-built FAQ covers the “Optimization” exercise from the lesson “Survey of Computer Science - The Tale of Kenny”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Code Foundations

FAQs on the exercise Optimization

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is it that when i use the binary search that it returns the same entry multiple times before moving forward?

Here you can find a screenshot of it. Image

3 Likes

It’s to do with the recursion as the list gets shortened.

If the middle value comes after the search term, the function recurses over the left half, else the right. Check again for relativity, recurse half again, and so on. We can see it only takes about half a dozen recursions.

Feed in a range of numbers to see a clearer picture…

binary_search(range(1000),679)
Checking the bookshelf and finding: 0
Checking the bookshelf and finding: 501
Checking the bookshelf and finding: 501
Checking the bookshelf and finding: 626
Checking the bookshelf and finding: 626
Checking the bookshelf and finding: 658
Checking the bookshelf and finding: 674
Checking the bookshelf and finding: 674
Checking the bookshelf and finding: 678

Found the book: 679
1 Like

May I suggest replacing the line
print("Checking the bookshelf and finding: {0}".format(lst[0]))
with
print("Checking the bookshelf and finding: {0}".format(lst[len(lst) // 2]))

That way the program displays the book in the middle of the selection, which seems more relevant to me than the book at the start of the selection.

2 Likes

Could you please explain in a bit simpler language ?

3 Likes
def binary_search(lst, search_val):
  if len(lst) == 0:
    print("Book is not on the shelf!")
    return
  print("Checking the bookshelf and finding: {0}".format(lst[0]))
  mid = len(lst) // 2
  if lst[mid] == search_val:
    print("\nFound the book: {0}\n\n\n\n\n\n".format(search_val))
    return
  elif lst[mid] > search_val:
    binary_search(lst[:mid], search_val)
  else:
    binary_search(lst[(mid + 1):], search_val)

The simplest language is the code itself.

Why my binary seach does not work?

I wrote binary_search(bookshelf, "Do Androids Dream of Electric Sheep?") in my code as the exercise explains. Unfortunately, when I run the code I do not see the book found.

This is what is shown:

Checking the bookshelf and finding: 1st To Die
Checking the bookshelf and finding: 1st To Die
Checking the bookshelf and finding: 1st To Die
Checking the bookshelf and finding: Cat’s Cradle
Checking the bookshelf and finding: Cat’s Cradle
Checking the bookshelf and finding: Defending Jacob

Found the book: Do Androids Dream of Electric Sheep?

So, where is “Do Androids Dream of Electric Sheep?”

By the way, the linear search worked.

I don’t understand how inside the definition of a function the function is called within itself.

image

This will come up in the lessons, at some point, if it hasn’t already. It’s a concept known as recursion. Notice that the calls are inside a conditional, of which one is the base case (in this case, search_val is found) and the other two are recursive cases.

If you have the time to segue, look up the topic of recursion to gain a greater understanding. There are even a few examples here in the forums that you may glean something from.

1 Like

I also didn’t understood a single word about this… :exploding_head:

1 Like

I have no idea what`s wrong, but I have an error. Could you help me, please?

We cannot really tell where the error actually is. It could be above that point. Check all your indentation from the top of the code.

It may be better if you post the raw code in its entirety in a reply.

I copy and paste this code in vs code I got a problem with inconsistent use of tabs and spaces in indentation in line 23 and 24. How can I fix this problem?

Is it somehow possible to get books.csv file to run this algorithms locally?

books.csv.txt (2.5 KB)

Comment out all the code then use this:

with open("books.csv") as f:
  for line in f:
    print (line)

Then copy the output to a text editor. Mind, you won’t need to do that, now, since the file is prepared for you above. Save it on your machine then remove the .txt extension.

Also, I just tried,

print (books)

and all the books are listed in the output.

1 Like

Thank you very much!

1 Like

How did your example come up with those numbers?

I hate how I don’t understand what is being explained on this page of the forum. If I’m lost this early in the game, I don’t know if I should see it as a challenge or as writing on the wall.

In the lesson it explains very little what the code means, and I feel like because they are showing the code that we are supposed to understand everything. I don’t even know what language they are teaching. Python? They explain very little. Just paste this here and paste that there. I don’t know, I learn best with traditional teaching with clear definitions that are listed and clear examples that contain the items defined. but I’m patient and not going to quit unless it is just painfully obvious that this is not for me. I thought that by having Asperger Syndrome that I would learn this easily but nope.

Please post a link to the exercise. After five years I will need a refresher.

@mtf Sure!

https://www.codecademy.com/paths/code-foundations/tracks/cf-computer-science/modules/survey-of-computer-science/lessons/the-tale-of-kenny/exercises/optimization

The language is Python (at least my code is) and this problem is way into the weeds for a new learner so best set this one aside and go into the Learn Python 3 course track to get your feet wet with that language.

If I may ask, where does your interest lie? Programming, in general or some kind of web development? If programming is your goal, Python is a good starting language, though many would suggest starting with JavaScript which can be leveraged in web and application projects that don’t require any extra software. Everything is in the browser, already. HTML, CSS, and JavaScript. Either way, the choice is yours. But, start at the beginning, not in a computer science or data science track.

If you want to work off line on Python, go to python.org and download the latest version of Python. It can be installed in your logged in user account (local machine) so you don’t need to exercise an Admin install. On a Windows machine it will install in,

C:\Users\%yourusername%\AppData\Local\Programs\Python\Python311\

Be sure to install a taskbar button for easy access. That will open the Interactive Shell from which you can create and open files (File menu).

When saving, let Python save to the Scripts folder (the default) and organize your work in that folder so all your Python scripts are in one place.

C:\Users\%yourusername%\..\Python311\Scripts\

Until you are really settled into learning the language don’t explore other IDE’s or install any libraries. Work with the Shell and get familiar with how to create files, open files, and run code. Commands typed directly into the shell are executed immediately.

>>> print ('Hello World!')
Hello World!
>>> 

If JS is to be your starting language, there is nothing to install, except maybe a free text editor like Notepad++. Learn how to create files, save files, and how to run them in the browser Console.

There are also free sandboxes such as REPL.it, CodePen, and so on where you can write, save and run code online. This is a good place to get started.

1 Like