<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tiago M. Alexandre on Medium]]></title>
        <description><![CDATA[Stories by Tiago M. Alexandre on Medium]]></description>
        <link>https://medium.com/@totalys?source=rss-4dc92fd83e17------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*w-n2SF-L6_VJasDt.jpg</url>
            <title>Stories by Tiago M. Alexandre on Medium</title>
            <link>https://medium.com/@totalys?source=rss-4dc92fd83e17------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:31:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@totalys/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sequential IFs vs Dictionaries Maps]]></title>
            <link>https://medium.com/@totalys/sequential-ifs-vs-dictionaries-maps-55bad909e88e?source=rss-4dc92fd83e17------2</link>
            <guid isPermaLink="false">https://medium.com/p/55bad909e88e</guid>
            <category><![CDATA[lambdas-in-dictionaries]]></category>
            <category><![CDATA[dictionary-vs-ifs]]></category>
            <category><![CDATA[dictionary]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Tiago M. Alexandre]]></dc:creator>
            <pubDate>Wed, 25 Sep 2019 04:29:34 GMT</pubDate>
            <atom:updated>2019-11-20T02:10:56.879Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*d_xVTgK5MjP6lejFFXf2qA.png" /></figure><p>Sometimes we face a situation where we need to decide path on taking decisions based on several scenarios or conditions.</p><p>A good example is taken from this challenge in <a href="http://www.hackerrank.com/">Hackerrank web site</a>:</p><h3>The challenge</h3><p>Consider a list (list = []). You can perform the following commands:</p><ul><li>insert i e: Insert integer (e) at position (i).</li><li>print: Print the list.</li><li>remove e: Delete the first occurrence of integer (e).</li><li>append e: Insert integer (e) at the end of the list.</li><li>sort: Sort the list.</li><li>pop: Pop the last element from the list.</li><li>reverse: Reverse the list.</li></ul><p>Initialize your list and read in the value of followed by lines of commands where each command will be of the types listed above. Iterate through each command in order and perform the corresponding operation on your list.</p><h4>Input Format</h4><p>The first line contains an integer, denoting the number of commands. Each line of the subsequent lines contains one of the commands described above.</p><h4>Constraints</h4><p>The elements added to the list must be integers.</p><h4>Output Format:</h4><p>For each command of type print, print the list on a new line.</p><p><strong>Sample Input:</strong></p><p>12</p><p>insert 0 5</p><p>insert 1 10</p><p>insert 0 6</p><p>print</p><p>remove 6</p><p>append 9</p><p>append 1</p><p>sort</p><p>print</p><p>pop</p><p>reverse</p><p>print</p><p><strong>Sample Output</strong></p><p>[6, 5, 10] [1, 5, 9, 10] [9, 5, 1]</p><h4>Engaging the problem</h4><p>Several solutions are possible. Switch case for some programming languages, sequential ifs, recursive functions and even, a dictionary map!</p><p>In this article, I’ll discuss two solutions for this challenge that take different approaches. I’ll present the code in python but I made a similar one in C# at the end.</p><p>I encourage you to solve <a href="https://www.hackerrank.com/challenges/python-lists/problem">this challenge in the hackerrank web site</a> before continuing from this point.</p><h3>Proposed solutions</h3><p>As mentioned before, I’m not aiming to pursue the finest solution here. The purpose of this article is to compare the usage of a dictionary map instead of using a simple naive sequence of ifs. Remember that <strong>python does not have Switch Cases.</strong></p><h4>Sequential ifs solution:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f9d0962365ce69c8203fbffb842fb36/href">https://medium.com/media/5f9d0962365ce69c8203fbffb842fb36/href</a></iframe><h4>Dictionaries Maps Solution:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99bfc9cf1c75ac4e598d849d7395e37b/href">https://medium.com/media/99bfc9cf1c75ac4e598d849d7395e37b/href</a></iframe><h3>Discussion</h3><p>Testing both codes we’ve got the following performance results:</p><p>With a simple StopWatch, the functions were called 500 times and yielded the following results:</p><p>Dictionary: 8.70 seconds</p><p>Sequential IFs: 7.82 seconds</p><p><strong>In C#:</strong></p><p>Sequential ifs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a15b39998c2e88763c64edc1e08219b/href">https://medium.com/media/7a15b39998c2e88763c64edc1e08219b/href</a></iframe><p>Dictionaries Maps:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/630cd35d493202f155b31080b25e3ca0/href">https://medium.com/media/630cd35d493202f155b31080b25e3ca0/href</a></iframe><p>Dictionary: 4.42 ms</p><p>Sequential IFs: 3.15 ms</p><p>For the C# comparison, it was used the BenchmarkDotNet tool. The results were slightly different from the python implementation (do not use this to compare C# vs Python this is not the point and I did not run the codes under the same circumstances). I could not use Switch statement in C# because, since this example takes more than 5 statements, the Switch could be internally converted into a custom dictionary in the intermediate language and it would result into the same scenario as the other case or let us be confused about what we are comparing (more about this <a href="https://channel9.msdn.com/Events/DevDays/DevDays-2011-Netherlands/Devdays041">here</a>).</p><p>In these tests, sequential IFs showed slightly faster than dictionary mapping in both languages.</p><p>Despite these results, the computational effort for sequential IFs solution increases linearly as options are being added to the list since each option will have to be tested at run time.</p><p>Another observation to make is that if the test scenario uses a lot of options that are tested for last, like, too much <strong>print</strong> commands, for example, this function will start to be slower. Dictionaries are implemented over HashTables that have constant access time.</p><p>Finally, there is another point that I think is also very appealing for choosing dictionaries: Elegance and the alignment with the clean code best practices.</p><p>Take a fine look at both implementations. See how easy is to read and understand the responsibility of each action when using dictionaries. It could be a little odd at the first look when you are not familiar with lambdas but when you get how delegate works, it becomes very clear that each entry is responsible for that part of the code.</p><p>If you need to maintain it, just add a new entry into the dictionary and write the proper function. You do not need to go into the “if” logic that is a lot more difficult to handle. Also, the cyclomatic complexity of your code on that function is just 1.</p><h3>Acknowledgment</h3><p>I wish to thank Leonardo Carvalho de Almeida, Raphael Almeida and Rodrigo Rosauro for reviewing this article and added great and valuable improvements.</p><h3>References</h3><ul><li>Python codes are <a href="https://github.com/totalys/Python-Dic-vs-Ifs/blob/master/ex_dictionary.py">here</a>.</li><li>BenchmarkDot: <a href="https://github.com/dotnet/BenchmarkDotNet">https://github.com/dotnet/BenchmarkDotNet</a></li><li>Switch in C#: <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch">https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch</a></li><li>Python lambda: <a href="https://www.w3schools.com/python/python_lambda.asp">https://www.w3schools.com/python/python_lambda.asp</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55bad909e88e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>