Developer's notes
4 subscribers
2 photos
1 video
16 links
Developer's notes: memories about projects, tasks, and advice to the beginners
Download Telegram
Is there a limit?

Last time when I was translating the task into probability theory terms, I assumed that each separate random value is distributed accordingly to the Normal distribution, and then I applied a rule that the means and variances can be summed up. Actually, there is a more interesting way to solve this task, namely to use the Central limit theorem (CLT).  As you may guess there is no way that I’ll burden my readers with a proof of the theorem or with its truly mathematical representation.  Without further ado, the theorem states: there are n equally and independently distributed values with a finite mean and variance then its sum is approximately distributed according to the Normal distribution and its mean and variance are the same as in the previous post.

Explain it one more time: we get the same result with weakened conditions. It doesn’t require that each separate value is normally distributed instead of that we say that any distribution with a finite mean and variance is suitable (except heavy-tailed ones because they don’t have a mean). However, having rejected this assumption I’d need to calculate a variance of one value by the given error not relying on the 3-sigma rule.

This short article shows why the Normal distribution is so popular and important. I won’t talk about it any longer, next time I’ll return to the heavy-tailed distributions.

#flood #education #math #probability_theory
🔥1
Unexpected problems with fundamental types

Recently I came across an article about uncommon programming languages, and there was a phrase: “Cobol has a dedicated money type”, I remembered that a long time ago my colleagues and I faced difficulties trying to represent different money amounts in C++. It seems to be a kind of a joke at first sight because C++ (as well as Java and C#) has float and double datatypes for real numbers, why isn’t it possible to use them? Because they don’t work, the float type“breaks” even in this simple example:

    float a = 0.01;
float b = 999999.00;
float sum = a + b;
std::cout << sum << std::endl;


Printing 999999. For the doubles it’s a bit more complicated to come up with an instance, though, when someone finds such an example in your program – it won’t make you laugh. There are a couple of main reasons to be pointed out: those types can’t exactly represent most decimal fractions like 0.1, 0.2, also during arithmetical operations these numbers are “aligned” to be represented at the same degree, but the mantissa has a finite number of bits, so that a small number can be lost during that (it’s what we saw in the code above).

Well, how to store prices and things like that in C++, then? Easily, store them as a number of cents in a long long int variable, of course, you should create a separate class with a field of long long int and implement all necessary operations and constructors. Amusingly, neither the Standard C++ library nor Qt has something suitable for money representation. In SQL there is a special type decimal.

Obviously, fundamental C++ types are inherited from its predecessor – C language, which in its turn is just a high-level assembly language. So, its types are basically what CPU architecture provides: one-byte value, 2,4, probably 8 for integral types, and 4 and 8 bytes for floating-point types. By the way, I don’t claim that the floats and doubles are useless: in any task involving real physical values like weight, torque, etc. the “error” with one hundredth would be neglectable against the entire magnitude.

#job #IT #c_plus_plus #math
How copy-paste saved a month

Among developers copy-paste is treated as an antipattern, however, everyone does it. And we have to admit that there is nothing wrong with that: if it’s possible to paste a piece of suitable code, and the most important thing – to tweak and adapt it. One more time: I don’t talk about giant classes, not knowing SOLID, and so on, I say that copy-paste is a correct step as long as it takes its place sometimes before opening a merge request for code review. Nowadays, many developers manage to write code with ChatGPt, in comparison with that copy-paste is just nothing.

However, I wanted to remember when copy-paste saved me a lot of time and helped me solve a task successfully. It happened while I was working on the project of a web engine, mentioned in this post. I’d already worked there for a couple of months when I took a bug with a humble description: “Flex layout doesn’t work correctly in a certain border case, look at the picture we’ve got and at another picture how it should be”. The developers added: “There are references to the specification in the code, check them out, probably they’re helpful”. Well, it sounded like a bug solving in a couple of code lines, everything that is left to find where and what I need to insert.

Before that I hadn’t heard about the flex layout: in Qt other layouts are used and I’ve never needed to implement one – just to apply it. Having looked at the specification, I decided that it’d be simpler to use a debugger to figure out why the calculation is incorrect in certain cases. It took me 3 days, after that I opened a merge request, assuming that the task was done. It was a huge surprise when I was told that my task was to implement an entire algorithm from the specification.

I felt sad because, obviously enough, it’d take me months: the specification is written in a very vague way, and the meaning of many terms and how to bring them down on our code was unclear. I quickly realized that the only suitable way to solve this task was to find a good project from which I could copy this sophisticated algorithm. I googled the source code of the WebKit project, and rapidly found exactly what I needed there after that things got better: it was possible to “translate” the algorithm from WebKit to our project, the only thing to take into account was that WebKit important values for the task are stored in different classes and our project they are somehow mixed up. However, finally, I just copied a needed piece of code from there and pasted it into our project, lightly adapting it…I opened a new merge request, it was quickly approved, and the task was completed.

#job #c_plus_plus #IT #web #HTML
🔥1
Compare it

Part 1

It’s going to be a long post, furthermore, it’ll include code, I’ve warned you.
I took a simple task that I found on a popular platform called Leetcode. Write a function taking a string and returning a Boolean value, it should return true if all the symbols in the string occur the same amount of times, otherwise, it returns false, its length is between 1 and 1000, and only English lower-case letters are allowed. Leetcode provides a convenient web-based interface with buttons “Run” and “Submit”, everything that we need just to write code and then it’ll tell us whether it’s correct or not, and if it is we’ll see how fast it works.

The task is simple, the task is understood, basically speaking, we need two cycles: the first one to count occurrences and to store them somewhere and another one to check the stored occurrences. The complexity of the first cycle is O(n), where n is the length of the string, and the complexity of another one is O(1), because the English alphabet consists of 26 letters.

Ask ourselves: “What should we try to use in a C++ program?” – right: “Standard library”. Leetcode hints the same providing a starch of the solution with the string represented as a std::string rather than old-fashioned char*. However, what does a std string provide to help us solve this task? Well, since the C++11 standard, there is a range-based for loop, perhaps, that’s all. Let’s look at the code:

    bool areOccurrencesEqual(string s) {
map<char, int> dict;
for (const auto& ch: s)
{
auto it = dict.find(ch);

if (dict.end() == it)
{
dict[ch] = 1;
}
else
{
++it->second;
}
}

int count = 0;

for (const auto& [_, value]: dict)
{
if (count != value && count)
{
return false;
}

if (!count)
{
count = value;
}
}

return true;
}


This is an absolutely straightforward solution: we’ve done what we’ve written, first, it iterates over the string counting letters and storing the frequencies into the dict, and then it iterates over the dict, comparing the frequencies, also we took into account that initially the count is zero, so we should carefully assign in the loop encountering the first non-zero value.

There is a catch: this solution beats only 27 percent of its competitors, barely I’d decide to write about such a bleak result:)

#IT #c_plus_plus #leetcode #today #ToBeContinued
Long pause

Since my last post here all my ‘writing energy’ has been absorbed with deeper articles published on Habr: two of them were mentioned in the channel, another one – not. Though, the latter finally attracted attention not only in terms of discussion but also in terms of likes and views bringing me correspondingly 52 likes (at the current moment) and 6 or so thousand views (the stat may be inaccurate according to the Habr). It’s worth pointing out that the articles and their topics are brainstorming results rather than coming out of working experience. One more article is coming, being finished, and scheduled to be posted in a corporate blog, there are other ideas…

Being a sober man, I realize that everything is more than just questionable in the long run: lack of motivation and everyday life with its duties have killed a multitude of intents scattered across different fields. It brings me to an image gathered from a popular fiction book: a library of never-written books living only in the minds of their authors…

Things are getting more entertaining because a couple of days ago I “put” a book on the library shelf: I was astonished by that because when I deliberately tried to make up at least something vaguely decent for a fiction book – I couldn’t. The plot won’t be given unless it turns into a real written story...

#flood #today #this_channel #habr #plans
👍2
After all, an article, aforementioned in the previous post, came out, bringing positive but mysterious stats, which consist of many likes, few comments, and a dislike. The latter doesn’t sadden me but raises certain thoughts: if one doesn’t find my job interesting why just don’t skip it, taking into account that I shed light on a deeply technical and specific topic which can’t touch their personal feelings or views. I keep silent about considerable efforts spent on a little or no paid job.

Well, let me quote Marcus Aurelius Antoninus: “Begin each day by telling yourself: Today I shall be meeting with interference, ingratitude, insolence, disloyalty, ill-will, and selfishness – all of them due to the offenders’ ignorance of what is good or evil.”

In general, it has changed nothing, the plans for future articles remain in the same state as I described them previously.

#flood #today #habr #plans
1
I'm still here

A week ago, I was going to write a post in the spirit of “long time no see but…”, it didn’t come true, so this week I’m going to give it another try. However, another thought came to my mind: little by little winter came to my place, currently it’s a true Xmas view in my window as well as in the neighborhood, and, obviously enough, it means that the year is about to finish…isn’t it time to sum the results and make a Happy new year’s resolution?

Regarding the former: a half year ago I began writing content for this channel and for a Russian twin of this channel, a month later I made up my mind to publish it on Habr. Both activities met moderately positive acclaim. At the moment there are 5 technical articles there, and most of them are mentioned in the channel. As a content maker, I realized a simple thing: it takes time and effort, especially the aforementioned articles, no way to write one in a day – in the best case it takes 2-3 days. The task is quite versatile: choosing a good topic, finding sources, writing code, creating illustrations, and last but not least making up a readable and coherent text minding the grammar and the register.

Now we’re getting to the resolution part: in spite of the fact that I do enjoy writing my articles, there is no way to create content of this level on a regular basis. For instance, I was planning on the article about cryptography, I did write the code and read a lot of related math stuff, but it’s already been two months and I can’t start arranging it all together due to a lack of time and energy. Perhaps, our cold weather isn’t the last factor why it’s so but a more major role played by my job and everyday chores. There is one unpleasant observation staying true through the passing years: the closer to the end of the year it is the more tired you are, on top of that even a humble celebration of it makes some fuss itself. The best thing I can do in terms of blogging is to drop an average-sized post once in several weeks.

Perhaps, that’s all that I wanted to say today.

#flood #superflood #today #this_channel #plans #habr #end_of_year
1
Channel photo updated
Channel photo updated
I broke my promise and kept the channel untouched. It's not a sin, though, and this isn't a confession. Recently, a song from The Hunger Games has come to my mind - I remember its lyrics by heart. The unusual thing is that I've never watched the movie and probably never will because it doesn't seem very appealing , so I don't know how deeply it's intertwined with the storyline, and I don't see much sense in finding out. I'm just struck by the image the song casts, so that I would like to share it.

https://genius.com/Lorde-flicker-kanye-west-rework-lyrics

https://youtu.be/3Bg_bj8pI5c?si=Jdj4D-Dm3kFSbz4r

#today #flood #music
1
Channel photo updated
Channel photo updated