Python 3 and C++11 Code Conciseness Comparison

Kevin Haritmonds
4 min readMar 4, 2022

--

Programming languages are evolving. The modern C++ is not the same as the one back in our father’s era :). Well then, can modern C++ compete with Python in terms of code conciseness and readability? Here I would like to compare the code written in both languages.

I’m taking a case from HackerRank “Two Two”. It is an advanced problem, meaning it’s not too simple and may require some kind of data structures to solve. I’ll be writing the solution in both programming languages, so we can compare between the two.

Handling Inputs

The inputs are read from Standard Input (STDIN).

Wow.. Apart of having to declare types in C++11, both are pretty concise.

Precomputing

There may be more than one correct approach to solve the problem. I chose to precompute a data structure called “graph”.

  1. Calculate 2⁰, 2¹, 2², …, 2⁸⁰⁰ numbers. Yes, I need to calculate all digits of each number. In Python, it’s straightforward: num = 2 ** i. Python computes all the digits for us.
    In C++11, we’d need to create vector<int> to store the long digits and write a helper function multiply().
    Here Python 3 wins as it can handle integer of unlimited digits without any effort from our side.
  2. Construct “graph” data structure. From these numbers above, I combined all the digits of all the numbers into a single data structure. It is a huge data structure.

I will show how I populate with examples:
– Let’s start with the first four: 2⁰, 2¹, 2², 2³. Here we circle the digit that “ends”, which they all are. This is important later when we want to count.

– Next ones: 2⁴, 2⁵, 2⁶. Again we circle the digit that “ends”. Notice that “1” was circled earlier.

– Next ones: 2⁷ = 128, 2⁸ = 256.

– So for 2⁰, 2¹, …, 2⁸, the “graph” data structure will look like this:

– We shall continue until 2⁸⁰⁰.

How does the code look like?

Not too bad! The way we declare the data structure and to access them are quite similar on both Python 3 and C++11.

Main Algorithm

Given a number we must count number of occurrences of 2⁰ to 2⁸⁰⁰ inside it. The number could be very large however, up to 10,000 digits.

The way I approach is as follows:

– Process the first digit of the number. Then we process the second digit, the third digit, etc. until the very last digit. Each digit is processed only once.

– When we process a digit, we create a new pointer pointing to the start of the “graph” data structure. We add this pointer into a list called “traverses”. Then we process each pointer in our traverses list, we move them to the current digit if possible. If cannot then we drop it from the list. If we find the “end” marking means we have found an occurrence of 2^x.

– We continue this process until the very last digit of the number. This way, we will find all occurrences of 2^x inside the number.

– Notice that we only have a single large “graph” data structure. And there are many pointers pointing to different section of the “graph” data structure.

– Finally, you may ask why did I reverse the digits. I noticed that 2^x ends with “2”, “4”, “6” or “8” digit. So by reversing the digits, we can skip processing the others digits thus reducing / pruning the search.

Complete Code

https://github.com/mckev/prog-challenges-misc/tree/main/20220228-hackerrank-two-two

So what do you think about modern C++11? Do you think it can compete with Python 3 in terms of code conciseness and readability?

--

--