Putting Python to use: a doomsday scenario

Eugene Lebedev
Coding in Simple English
4 min readFeb 21, 2020
Photo by Maxwell Nelson on Unsplash

There’s so much you can do with Python, like raw data processing and machine learning. But those things will take more practice. So, let’s try something a little different to get to know Python right now.

In this mini-project, we’ll use Python to crack a math problem. This problem is simple enough to solve without programming — if you wanted to, you could just solve some equations. But we have Python, so let’s not.

Here’s the math problem:

  1. In a lab, there is a nanobot. It feeds on bacteria. Every second, one nanobot eats one bacterium.
  2. With every bacterium eaten, the nanobot replicates — that is, it copies itself.
  3. The bacteria that these nanobots eat are everywhere; they cover the earth in a thin layer. There is no place on earth that doesn’t have these bacteria.
  4. Each bacterium feeds on whatever is available to it, and also replicates every second.
  5. If a single nanobot breaks free from the lab, will the nanobots eventually consume all the bacteria on earth? And if so, how long will this take?

Scientists call this a ‘Grey Goo’ doomsday scenario, by the way. Grey Goo refers to a pile of nanobots which consume all the living things on earth just for the sake of it.

The mathematical solution

If I didn’t have the Jupyter Notebook(an app for programming in Python), I’d solve this problem with math. I won’t bother you with the details, but the answer would be surprising: with the given conditions, it will take the nanobots as many seconds to consume all the bacteria, as there were bacteria at the beginning. So, if there were 20 quadrillion bacteria on earth, it would take the nanobots 20 quadrillion seconds to consume them all.

It’s just math. You don’t have to worry about it.

The Python solution

What I’d much rather do is use Python to run a simulation of the process. First of all, it’s much easier, and error-proof. Second, it’s much more fun.

Let’s start by writing down the basics of the problem. (Note that in Python, we can write comments on our code by starting a line with the # symbol. So, any line that begins with # is an explanation, not part of the code itself.)

# 1. How many bacteria we have in the beginning
bact = 10
# 2. How many nanobots we have in the beginning
nano = 1
# 3. Elapsed time in seconds
sec = 0
# 4. The following loop will continue while there is at least one bacterium
while bact > 0:
# At every step:
# 5. each bot eats a bacterium, so the number of bacteria decreases by the number of bots
bact = bact — nano
# 6. Then the nanobots double
nano = nano * 2
# 7. and the bacteria double.
bact = bact * 2
# 8. Time passes.
sec = sec + 1
# 9. If the program ever exits the loop, this means that all the bacteria have been eaten.
# 10. The program throws the number of seconds it took for the bots to do all the eating.
print(str(sec) + ‘ seconds until doomsday’)

If we paste this into Jupyter Notebook and run the program (Shift + Enter), then we get ’10 seconds until doomsday.’ This means the loop is running, and our simulation is giving us the exact result I got from my mathematical calculation.

But wait, that’s not even the best part.

The best part

The best part is that now, having all the basics in place, we can make the problem infinitely more complex, and still get valid results. For example:

  1. We can introduce a ‘death factor’ that kills a certain number of nanobots every second
  2. We can introduce new species of bacteria that replicate at slower or faster rates
  3. We can introduce bacteria that resist being eaten by nanobots and eat nanobots instead

We can introduce all that into our While loop and run the simulation. And we can add a whole lot more complexity, like throwing in random events, mutating the bacterial population over time, and much more — all we need to do is first think about it, and then explain it in simple Python code. And hit Run, so that the loop loops and the simulation simulates. Simple as that. Doing the same thing in a mathematical equation would be infinitely more complex, while in Python, it’s just a matter of a few lines of code.

So, there you go. That’s an example of Python put to good use. :-)

Tell me what you think!

If you enjoyed diving into Python today, feel free to visit Practicum by Yandex. This online educational environment has courses and mentors to teach you to code and help you find jobs in tech.

--

--