Learn to Code by Solving Problems: A Python Programming Primer Homepage for the book Learn to Code by Solving Problems: A Python Programming Primer

More Stuff

If you’ve worked through everything in My book Learn to Code by Solving Problems, including the exercises at the end of each chapter, and the additional exercises here, then…

First: wow! And second: you might be looking for more.

The material on this page isn’t directly connected to material in the book. But if you’re looking for more to learn or explore, some of these directions may be of interest to you.

Algorithms

Remember Chapter 9, where you learned about algorithm design and complete search algorithms? And Chapter 10, where you learned how to make your algorithms more efficient?

This is key stuff for writing scalable, efficient code. As you learned in the book, you want to be able to design algorithms that don’t get bogged down when they have to solve problems with lots of input.

If you want to continue with algorithms where Learn to Code leaves off, check out my other book Algorithmic Thinking. For super speed and super control over every aspect of our algorithm design, the book uses the C programming language, not Python. So if you were thinking of learning a language like C or C++ or Java next, or you’re willing to dive into some C to step up your algorithms game, this book may help. It uses programming judge problems, just like Learn to Code. How are dictionaries like those in Python implemented under the hood? (Hint: hash tables.) How does binary search work, and how can it be used to solve all kinds of challenging problems? Ever heard the term recursion and want to know what it’s all about? All of this and lots more awaits.

Computer Math, Ack!

Check out this Sample Size problem. It’s a nice complete search problem that almost fits in Chapter 9.

… almost, but not quite. If you try solving it, you might not pass some of the test cases. The reason may have to do with how numbers are internally stored in your computer. Watch this:

>>> 46*100/80
57.5  # Good
>>> 46/80*100
57.49999999999999  # Huh?

In both cases, we’re starting with 46, multiplying it by 100, and dividing by 80. It shouldn’t matter whether we do the multiplication or division first.

Except: it clearly does matter! You may be able to “fix” your solution by switching around the order of operations. But the bulletproof way to fix this for real is not to use floating point numbers at all.

Here’s another head scratcher:

>>> 1.1+2.2
3.3000000000000003

Gotcha? All those CPU cores and gigablahblahs and we can’t add 1.1 and 2.2? For a deep dive on what’s going on, start with Python’s Decimal Module.