C# Algorithms – Usage of Nested Lists and TryGetValue of Dictionary

Since the beginning of the month, I am visiting a course called Algorithms. As far as I have to work with one of the following languages – C#, C++, Java or Python and somehow Python is rather slow for such a course, I have decided to look back at the good old C# language.

c#

The quest for the day was to make something called “Birthday range”. The idea is the following, e.g. this is the input:

You need to take a look at the list liBirthdays and to come up with a solution of how many people have birthdays in the range liRanges. A birthday may be any given value between 0 and 365. E.g., as you may see my comments, in the first range, between day 1 and day 100, the people with a birthday are 14 (each entry in the liBirthdays is a single person). In the second range (9-100), the result is 3. And in total it is 22, because some of the ranges overlap, so I count them once again.

It really seems trivial to be done. If you know how to do it or if you have a lot of experience in C#. Or probably both. 🙂

In my way, I have not written in this language for quite a long time (especially in Mint and in Ubuntu!), thus it was a challenge. The solution of the riddle is the following:

  1. We generate an object with Linq liOriginal, where we count the number of occurrences of each entry of liBirthdays;
  2. We generate a dictionary myDictionary, where we count the number of occurrences of a given date as value and the date as a key.
  3. The we come up with something, that I really like from the ABC analysis – cumulative calculation of all previous values. This calculation is stored for each value in a list liBirthdaysOrdered  at the value position.
  4. At the end we take a look at the list of list (see the long way of initializing something like this in C#, compared to Python) liBirthdays and we start calculating how many of our people have birthdays in a given range.
  5. That is all, the last thing to do is to print the code! 🙂

Here comes the code:

Enjoy it and try to improve it! 🙂

Tagged with: , , , ,