<?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 Adnan Sinanovic on Medium]]></title>
        <description><![CDATA[Stories by Adnan Sinanovic on Medium]]></description>
        <link>https://medium.com/@sinanovica?source=rss-b7fa431cd630------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*sEE2hzagrE3T-T1VvnmxzQ.png</url>
            <title>Stories by Adnan Sinanovic on Medium</title>
            <link>https://medium.com/@sinanovica?source=rss-b7fa431cd630------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:56:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sinanovica/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[How man-in-the-middle saved the day]]></title>
            <link>https://medium.com/mop-developers/how-man-in-the-middle-saved-the-day-f1eef6506d2e?source=rss-b7fa431cd630------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1eef6506d2e</guid>
            <category><![CDATA[documentation]]></category>
            <category><![CDATA[task-management]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Adnan Sinanovic]]></dc:creator>
            <pubDate>Tue, 13 Jun 2023 09:46:15 GMT</pubDate>
            <atom:updated>2024-03-13T08:29:55.939Z</atom:updated>
            <content:encoded><![CDATA[<h4>True software development horror experience</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9TCotKnPDaGiP1kjlx2H-g.jpeg" /></figure><h3>Innocent beginning</h3><p>Many, many years ago I worked with Windows desktop C♯ and C++ apps. The app was accessible through public interfaces, which we internally called Terminals. Terminals were very similar to the ATMs. Each Terminal was nothing more than a PC packaged into shiny armor and touch screen.</p><p>One day, the company decided to use smart cards to track users. I had to write a new smart-card-feature for the app. We were not beginners; we had some experience. On a daily basis, we worked with usb, rs232, rfid, nfc, ccTalk, SAS, AT, ICT, custom-made in-house PCBs, and all other kinds of hardware devices. Working on a smart card device feature looked like a task that you assign to yourself when you want to take a rest from the <em>real tasks</em>.</p><p>To accomplish the task, we received a low-budget, no-name, plug-n-play card reader, a few blank PVC smart cards, and SDK. There was no manual, or how-to-guide, or set of instructions, to tell us where to start, what to expect, to warn us about any danger (!), to tell us what type of smart cards are required (there are tons of different card types on the market), to provide any useful information — nothing.</p><p>Communication with the supplier was poor, practically useless. For unknown reasons, the company insisted that we should proceed with that specific supplier.</p><h3>Wrong turn</h3><p>Soon, we realized SDK is not the real SDK. SDK contained two files only. One file was <a href="https://www.computerhope.com/jargon/e/execfile.htm">executable </a>(.exe) file, and it was a demo app with one purpose only — to show us that the device and card work as expected.</p><p>The other file was <a href="https://docs.microsoft.com/en-us/troubleshoot/windows-client/deployment/dynamic-link-library">dynamic-link library</a> (.dll) file. We were supposed that the magic was taking place inside .dll file, and that we were supposed to import and use .dll file inside our app.</p><p>Although there were so many questions, it seemed like Demo App and the card reader device could communicate. With utilities like <a href="https://docs.microsoft.com/en-us/cpp/build/reference/dumpbin-reference?view=msvc-170">dumpbin </a>and <a href="https://www.dependencywalker.com/">Dependency Walker</a>, we figured out the definitions of <a href="https://docs.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-declspec-dllexport?view=msvc-170">exported </a>.dll functions. The names of the functions were almost identical to the labels on the buttons inside Demo App. It was a good sign — we could associate .dll function with a specific part of demo app.</p><p>When we tried to <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/interoperability-overview">import</a> .dll into our source code — nothing worked as expected. Actually, we could not establish any communication between our code and .dll. No matter what approach we tried, and we tried a lot of them, the result was always bad. We even tried to <a href="https://www.techtarget.com/whatis/definition/decompile">decompile</a> both Demo App and .dll, hoping that we could reuse the decompiled code, but both files were <a href="https://en.wikipedia.org/wiki/Obfuscation_(software)">obfusacted</a>, so the decompiled version was not very useful.</p><h3>Doom</h3><p>Accidentally, we found out that cards were Siemens SLE4442 EEPROM cards. SLE4442 has a security mechanism (PSC Verification), and improper use of the card can block the card for further usage. Later on, we would found out that we blocked some cards.</p><p>Also, we found out and that Demo App doesn’t work if <a href="https://docs.microsoft.com/en-us/windows/security/identity-protection/smart-cards/smart-card-smart-cards-for-windows-service"><em>Smart Cards for Windows Service</em></a><em> (SCWS) </em>is enabled. This fact was a huge surprise. SCWS is the core part of the OS. It manages all interactions between smart card devices and OS. Just by using common sense one would expect that Smart Card Service has to be enabled when interacting with a smart card reader. Anyway, we concluded that this illogical fact means that our card reader probably does not run on top of <a href="https://pcscworkgroup.com/">PC/SC</a> Workgroup Specifications.</p><p>Days passed by. Frustration was at its peak. We were almost beaten, without ideas on how to proceed. Only thing that kept us trying was the fact that Demo App could interact with the card reader device.</p><h3>Insidious App</h3><p>In the end, we tried to eavesdrop on communication between Demo App and .dll file. The idea was to record data sent from Demo App, and then to sent the same data from our source code.</p><p>The technique for eavesdropping is called <a href="https://medium.com/geekculture/basic-windows-api-hooking-acb8d275e9b8">API Hooking</a>. The concept is comparable to <a href="https://en.wikipedia.org/wiki/Man-in-the-middle_attack">man-in-the-middle attack</a> concept. API Hooking is often used by antivirus softwares, gaming cheaters, malicious softwares, etc. It’s a close relative to <a href="https://www.upguard.com/blog/dll-hijacking">dll hijacking </a>technique.</p><p>Eavesdropping created even more question marks above our heads. There was no communication between Demo App and .dll file. The magic was happening inside Demo App. DLL file was purposeless. The whole time we were trying to do the impossible.</p><h3>Revelation</h3><p>In the end, we discovered that Demo App communicated with some of the core OS system files (it was a long time ago, cannot recall exactly). Although it was a long and painful process, we recorded and reconstructed all data packets sent from Demo App. We put those reconstructed messages to our source code and everything worked fine.</p><h3>Happy ending</h3><p>It took a few weeks just to establish communication between our source code and the card reader device, but eventually the feature went to the production environment. Many, many years later, it still runs on Terminals all across the globe.</p><h3>Part 2</h3><p>Two-three years later, the company created a new terminal, aka Terminal 2. Terminal 2 use SLE5542 cards instead of SLE4442. A small software upgrade was required to run SLE5542 cards. We received the same package as before: a card reader, blank PVCs cards and SDK (Demo App + dll). Version of the .dll was incremented from 1.0 to 1.1 — it was not the same file as before.</p><p>Out of curiosity I tried to import new .dll to source code and to try it. It took me less than a 3 minutes to prepare everything. New .dll worked like a charm.</p><h3>Final word</h3><p>This is an example of HOW NOT TO APPROACH THE TASK.</p><p>Although this journey was unique, although some <em>hardcore</em> approaches were taken, it shows how poor communication, low-budget, personal interests can and will lead to overcomplicated solutions, enormous waste of time and resources, immense frustrations, and, in most cases, poor results.</p><p>Instead of <em>engineering</em>, we were <em>reverse-engineering</em>. There are use cases where reverse-engineering applies. However, if you need to add a simple new feature to the existing codebase, in most cases, that is not a place for reverse-engineering.</p><p><strong>…</strong></p><p><em>Ministry of Programming is a supercharged startup studio specialized in building startups and new products💡 We were voted in the top 1000 fastest growing companies in Europe by Financial Times. Twice.</em></p><p><em>We offer product management, design, development, and investment services to support entrepreneurs and startups towards product success.</em></p><p><em>Building your next startup? We would love to hear more. If you want to work with us on your startup feel free to reach out at — </em><a href="https://ministryofprogramming.com/contact/"><em>https://ministryofprogramming.com/contact/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1eef6506d2e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mop-developers/how-man-in-the-middle-saved-the-day-f1eef6506d2e">How man-in-the-middle saved the day</a> was originally published in <a href="https://medium.com/mop-developers">Ministry of Programming — Technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Terms Swagger and OpenAPI]]></title>
            <link>https://medium.com/mop-developers/terms-swagger-and-openapi-64e90b2e711b?source=rss-b7fa431cd630------2</link>
            <guid isPermaLink="false">https://medium.com/p/64e90b2e711b</guid>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[open-api]]></category>
            <category><![CDATA[swagger]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Adnan Sinanovic]]></dc:creator>
            <pubDate>Wed, 03 May 2023 08:40:48 GMT</pubDate>
            <atom:updated>2024-03-13T08:30:24.318Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K6R2bPwbWc6-5nbuolFdlw.jpeg" /></figure><p>While I was writing the previous article, which is never published, I realized that many do not understand the difference between terms <em>Swagger</em> and <em>OpenAPI (OAS)</em>. People use both terms interchangeably. I do not blame them since I had the same doubts.</p><blockquote>Swagger allows you to describe the structure of your APIs so that machines can read them. <br> — <a href="https://swagger.io/docs/specification/2-0/what-is-swagger/">SwaggerIO</a></blockquote><p>If you try to google <em>Swagger </em>or <em>OpenAPI</em>, in the most cases, you will end up at <a href="https://swagger.io/">SwaggerIO</a>, the official Swagger website. If you ask me, the website has obscured Swagger purpose since day one. Not intentionally, though. Information is not missing, it’s just not presented in a clear and concise manner.</p><blockquote>The OpenAPI Specification (OAS) defines a standard language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.<br> — <a href="https://swagger.io/specification/">SwaggerIO</a></blockquote><p>Today, there are many articles on this topic, thus it’s much easier to figure out the difference, but still it is possible to find writings which are intermingling the terms. <a href="http://www.swagger.io/">SwaggerIO</a> is promoting the Swagger as <em>a set of tools</em>, and the OpenAPI as a specification. We could say that <em>the</em> <em>Swagger is used to create OpenAPI specification</em>. Essentially, this statement is not wrong, but it is not the entire truth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hyd_LFPZTv61cn3VWb0JLw.png" /><figcaption><a href="http://www.swagger.io/">SwaggerIO</a> mingling terms <strong><em>Swagger</em></strong> with <strong>OpenAPI</strong></figcaption></figure><h4>Usage of the terms</h4><p>Usually, developers use term <em>Swagger</em> in, at least, two contexts:</p><ul><li>to describe a set of tools for implementing the specification. Some tools are Swagger Editor, Swagger UI, Swagger Codegen, etc. It is an ecosystem.</li><li>to describe the specification of version 2.*.*</li></ul><p>Other widely used terms are <em>Swagger 2</em>, <em>Swagger Specification 2</em>, <em>OpenAPI Specification 2</em>, etc.</p><p>Developers use the term <em>OpenAPI Specification 3 </em>mostly<em> </em>in a single context:</p><ul><li>to describe the specification of version 3.*.*. Community refers as OAS, OAS3, OpenAPI Spec, OpenAPI 3 Specification, etc. They adopted <em>OpenAPI 3</em> as the successor of <em>Swagger Specification 2.</em></li></ul><h4>Reasons</h4><p>Swagger and OpenAPI are close friends for a reason. History is the key ingredient here.</p><p>SmartBear company maintains Swagger. In November 2015, The Linux Foundation announced they are creating, together with SmartBear, Google, Microsoft, Paypal, and few other companies, a new organization, OpenAPI Initiative. The primary task of the initiative was to extend the Swagger specification.</p><p>A few months afterward, the initiative renamed Swagger to OpenAPI specification. The initiative cloned the code to the new repository. From that point, individuals have used both terms in various contexts.</p><h4>Final word</h4><p><em>Swagger</em> used to be an all-in-one, specification and toolset. Today, <em>Swagger</em> is a toolset. <em>OpenAPI</em> is a specification. That’s it.</p><p>It’s nice to know the difference. Yet whichever term you use, the other side will understand you — in the end, that is the only thing that matters.</p><p><strong>…</strong></p><p><em>Ministry of Programming is a supercharged startup studio specialized in building startups and new products💡 We were voted in the top 1000 fastest growing companies in Europe by Financial Times. Twice.</em></p><p><em>We offer product management, design, development, and investment services to support entrepreneurs and startups towards product success.</em></p><p><em>Building your next startup? We would love to hear more. If you want to work with us on your startup feel free to reach out at — </em><a href="https://ministryofprogramming.com/contact/"><em>https://ministryofprogramming.com/contact/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64e90b2e711b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mop-developers/terms-swagger-and-openapi-64e90b2e711b">Terms Swagger and OpenAPI</a> was originally published in <a href="https://medium.com/mop-developers">Ministry of Programming — Technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Should I choose Angular or React?]]></title>
            <link>https://sinanovica.medium.com/should-i-choose-angular-or-react-841a90a62139?source=rss-b7fa431cd630------2</link>
            <guid isPermaLink="false">https://medium.com/p/841a90a62139</guid>
            <dc:creator><![CDATA[Adnan Sinanovic]]></dc:creator>
            <pubDate>Fri, 19 Mar 2021 14:15:15 GMT</pubDate>
            <atom:updated>2021-03-19T14:15:15.214Z</atom:updated>
            <content:encoded><![CDATA[<h4>Each framework has its pros and cons, and sometimes it’s difficult to choose between them. Same story goes in a brawl between Angular and React. There are articles on this topic. Almost none of them will provide you with a straightforward answer — but this one will.</h4><p>Choose React.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=841a90a62139" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convert a  combination to a single number]]></title>
            <link>https://medium.com/mop-developers/convert-a-combination-to-a-single-number-eb4c9ea5476a?source=rss-b7fa431cd630------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb4c9ea5476a</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[combinatorics]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[primes]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Adnan Sinanovic]]></dc:creator>
            <pubDate>Tue, 02 Jul 2019 14:25:46 GMT</pubDate>
            <atom:updated>2019-07-02T14:25:46.531Z</atom:updated>
            <content:encoded><![CDATA[<h3>Convert a combination to a single number</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sKpJmj16sJwoAIkAlhkYgQ.jpeg" /></figure><p>How to convert a combination to a single number and vice versa? How to count and index all combinations of elements? How to assign a unique number or rank, to any combination?</p><p>It is a math problem. Math areas like <a href="https://en.wikipedia.org/wiki/Enumerative_combinatorics">Enumerative Combinatorics</a> or <a href="https://en.wikipedia.org/wiki/Combinatorial_number_system">Combinatorial Number System</a> (combinadics) have established algorithms for dealing with this and similar issues a long time ago.</p><p>This article does not dive deep into the math. It tries to provide some essential math insights to understand the problem better. The article attempts to present the problem and the solution from a<em> </em>more friendly<em> </em>perspective. Also, the time complexity of the provided source code is not in focus. Thus it is possible you will be able to find/write <em>faster</em> code.</p><h3>Primes</h3><p>Prime numbers are the building blocks of math. Other numbers are made of primes. This technique uses one of the essential math laws known as the <em>Fundamental Theorem of arithmetic</em>.</p><p>Fundamental Theorem of Arithmetic: any integer greater than 1 is either a prime number or can be written as a unique product of prime numbers (ignoring the order).</p><p>From another perspective: <strong>there is only one (unique!) set of prime factors for any number.</strong></p><p>Example: 42 = 2 × 3 × 7. There is no other possible set of prime numbers that can be multiplied to make 42.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPy6ywxHmNfuYHwZFUjmYA.png" /><figcaption>List of primes up to 1000</figcaption></figure><p>Knowing the <em>Fundamental Theorem of Arithmetic</em>, we can easily represent any combination of objects with a single number. We need to assign a unique prime number to every element. To get a unique number for any combination, multiply the assigned primes, and that’s it.</p><blockquote>For example how to represent the combination <em>C₁ = { &quot;B&quot;, &quot;D&quot;, &quot;E&quot; }</em>for the set<em>S₁ = { &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;, &quot;J&quot;, &quot;K&quot; }?<br></em>The positions of the elements B, D, E are 1, 3, and 4. The prime numbers on the positions 1, 3, and 4 are <strong>3</strong>, <strong>7</strong>, and <strong>11</strong>. <br>Thus, <em>3 × 7 × 11 = 231</em> represents combination <em>C₁ = { &quot;B&quot;, &quot;D&quot;, &quot;E&quot; }</em></blockquote><p>Real-life example: Cactus Kev used the <em>Fundamental theorem of arithmetic</em> in his <a href="http://suffe.cool/poker/evaluator.html">Poker Hand Evaluator</a>.</p><p>Note: The reverse process is known as prime factorization. While converting a combination to number is a swift operation, prime factorization is very slow. It is one of the reasons why it is used in cryptography.</p><p><strong>Source</strong>: <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example1/main.go">Go</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example1/index.js">JavaScript</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example1/app.py">Python</a>.</p><h3>Odometer effect</h3><p><em>The odometer effect</em> is what happens when a mechanical mileage counter on a car reads 9999 and then <em>flips over</em> to 10000. All the dials have to turn at the same time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*5c8GEg_Jnbqex0dUWwA6tQ.gif" /><figcaption>Car odometer</figcaption></figure><p>In this case, a dial represents a set. While a single dial has 10 values (0–9), a set can have any number of values, and the cardinality of each set can be different.</p><p>If we translate it to a more general form, we would say there are S<em>₁, S₂, S</em>₃<em>… Sn </em>sets and each set has <em>N₁, N₂, N</em>₃<em>… N</em>ₙ<em> </em>elements. How to <em>convert</em> a combination of <em>n</em> elements to a single number where each element belongs to only one set and the position of each element within a set is known?</p><blockquote>Example, for the given sets <em>S₁</em>, <em>S₂</em>, <em>S₃</em> and <em>S₄</em>:<br><em>S₁ = { &quot;John&quot;, &quot;Jack&quot;, &quot;Joe&quot; }</em><br><em>S₂ = { &quot;Water&quot;, &quot;Fire&quot; }</em><br><em>S₃ = { &quot;Brazil&quot;, &quot;UK&quot;, &quot;Russia&quot;, &quot;Japan&quot;, &quot;Morocco&quot;, &quot;France&quot; }</em><br><em>S₄ = { &quot;Blue&quot;, &quot;Red&quot;, &quot;Yellow&quot;, &quot;White&quot; }<br></em>how to represent the combinations <em>C₁</em> and <em>C₂</em> :<br><em>C₁ = { “John”, “Fire”, “Japan”, “Red” }</em><br><em>C₂ = { “Joe”, “Water”, “UK”, “White” }<br></em>with the unique numbers <em>X</em> and <em>Y?</em></blockquote><p>Notice<em> the odometer effect</em> is also presented in<em> weighted number systems (decimal, hexadecimal, octal, binary, etc.)</em>. In weighted number systems, each digit has a weight value assigned. For example, in the decimal system, the positions of digits are marked with numbers from 0 to 9, and the weight of the digit is the base (10) raised to the power of the position number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*l5UDdnO404L6_arkkbej-g.jpeg" /><figcaption>Decimal number with the position and weight of each digit shown and derivation of the value of the number.</figcaption></figure><p>According to the weighted number systems, we mark every set with a position from 0 to <em>n</em>. Since cardinality c (the number of elements in a set) is different for each set,<em> base-raised-to-the-power-of-position</em> formula is not applicable anymore. The idea is the same, but the formula is slightly modified</p><p>The weight of the first set is 1, always: <br>w₁ = 1.</p><p>The weight of the second set is the product of the first set weight and the first set cardinality: <br>w₂ = w₁ × c₁ = 1 × 3 = 3.</p><p>The weight of S₃ is equal to the product of the S₂ weight and theS₂ cardinality: <br>w₃ = w₂ × c₂ = 3 × 2 = 6.</p><p>Similarly, for S<em>₄</em>:<br>w<em>₄</em> = w<em>₃</em> × c<em>₃</em> = 6 × 6 = 36.</p><p>Generally: wₙ = wₙ₋₁ × cₙ₋₁</p><p>Now when we know the weight of each set, it is easy to derive the unique representation for any combination. If p is a position of an element in the set, then:</p><pre>X = w₁ × p₁ + w₂ × p₂ + w₃ × p₃ + … + wₙ × pₙ;</pre><h4>C₁ solution</h4><p>The positions of the elements<em>C₁ = { “John”, “Fire”, “Japan”, “Red” } are <br></em>p₁ =<em> 0 (John)<br></em>p₂ = <em>1 (Fire) <br></em>p₃ = <em>3 (Japan)<br></em>p<em>₄ </em>= <em>1 (Red).<br></em>X = 1 × 0 + 3 × 1 + 6 × 3 + 36 × 1 = 0 + 3 + 18 + 36 = 57</p><p><em>57 represents John</em>, <em>Fire</em>, <em>Japan, </em>and <em>Red</em>.</p><h4>C₂ solution</h4><p>The positions of the elements<em>C₂ = { “Joe”, “Water”, “UK”, “White” } are<br></em>p₁=<em> 2 (Joe)<br></em>p₂ = 0<em> (Water)<br></em>p₃ = 1<em> (UK) <br></em>p<em>₄ </em>= 3<em> (White).<br></em>Y = 1 × 2 + 3 × 0 + 6 × 1 + 36 × 3 = 116</p><p><em>116 represents Joe</em>, <em>Water</em>, <em>UK, </em>and <em>White.</em></p><p><strong>Source code</strong>: <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example2/main.go">Go</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example2/index.js">JavaScript</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example2/app.py">Python</a>.</p><h3>Combinadics</h3><p>What if there is a single set of <em>N</em> elements and we need to represent a combination of <em>x</em> elements (a real-life example is a lottery)? It is a classic <a href="https://en.wikipedia.org/wiki/Combinatorial_number_system"><em>combinadics</em></a> problem.</p><p>Since this example requires more than essential math skills, more than knowledge of multiplying and adding numbers, the article is not going into the details.</p><blockquote>For example, how to represent the combination <em>C₁ = { &quot;C&quot;, &quot;G&quot;, &quot;K&quot; }</em> for the given set: <em>S₁ = { &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;, &quot;J&quot;, &quot;K&quot; }?</em></blockquote><p>The indices for the elements <em>C, G, K</em> are 2, 6, 10 respectively (reversed <em>c₁ = 10</em>, <em>c₂ = 6</em> and <em>c₃ = 2</em>).</p><p>According to the <a href="https://en.wikipedia.org/wiki/Combinatorial_number_system">combinadics</a>, the bijection is the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*2qVheJzAZPzOOTlxRpO4jA.png" /><figcaption>Bijection for the previous example</figcaption></figure><p>As already noted, the article is not going to step deep into the math. If you are not familiar with combinatorics or you do not know how to <em>work</em> with the binomial coefficient, you can copy the <em>binomial</em> code from <a href="https://rosettacode.org/wiki/Evaluate_binomial_coefficients">here</a>.</p><p>More on this topic can be found <a href="https://en.wikipedia.org/wiki/Combinatorial_number_system">here</a> and <a href="https://arxiv.org/pdf/1601.05794.pdf">here</a>.</p><p><strong>Source code</strong>: <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example3/main.go">Go</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example3/index.js">JavaScript</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example3/app.py">Python</a>.</p><h3>Bit Flags</h3><p>This <em>technique</em> is well known and widely used. The idea behind <em>the bit flags</em> is not to convert a combination to a single number but to hold multiple values into a single variable. The bit flags technique is convenient for sets of few elements only; usually, it is used with enumerated values.</p><p>The smallest unit of memory is a bit. The smallest addressable unit is a byte (a group of 8 bits). It means that every variable must be at least 1 byte in size. One byte can hold ²⁸=256 unique values. For most data types, it is okay. However, boolean types (boolean type has only two values: true or false) need only 1 bit to store the value. Thus, 1 byte can hold up to 8 boolean values.</p><blockquote>For example how to represent the combination <em>C₁ = { &quot;C&quot;, &quot;F&quot;, &quot;H&quot; } for the set</em><em> S₁ = { &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;, &quot;J&quot;, &quot;K&quot; }?</em></blockquote><p>The indices for the elements <em>C, F, H,</em> are <em>2, 5, 7,</em> respectively. If we use bit flags to store the positions of combination elements, we get: 10100100 (put 1 on positions 2, 5, and 7, starting from right to left).</p><p>(10100100)<em>₂</em> equals to (164)<em>₁</em>₀.</p><p>The answer to the previous question is 164.</p><p>If bit flags are still not clear to you, I advise you to analyze IPv4 addresses and IPv4 address subnetting — it is based on bit manipulation.</p><p><strong>Source code</strong>: <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example4/main.go">Go</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example4/index.js">JavaScript</a>, <a href="https://github.com/adnansinanovic/comb-to-num/blob/master/example4/app.py">Python</a>.</p><h3>Final word</h3><p>A separate article (or a book) could be written on any of the presented techniques. In most cases, the position of an element within the set is essential (the value of the element is less important).</p><p>There are many other ways to derive a single number from the combination. As a professional, I have used only these four, and that is the main reason why I choose to write about them. Way back when I was a rookie, I struggled to figure out how to convert a combination to a single number, and without the right background, it was hard even to google it. Hopefully, this article is a good clue on a path of converting combinations to a number.</p><h3>Related Readings</h3><p>As an addition, a few connected readings:</p><ul><li><a href="http://riad.pk.edu.pl/~zk/pubs/95-1-006.pdf">Algorithms for Unranking Combinations and Other Related Choice Functions</a></li><li><a href="https://en.wikipedia.org/wiki/Enumerative_combinatorics">Enumerative Combinatorics</a></li><li><a href="https://en.wikipedia.org/wiki/Combinatorial_number_system">Combinatorial number system</a></li><li><a href="https://arxiv.org/pdf/1601.05794.pdf">Proof of bijection for combinatorial number system</a>, Abu Bakar Siddique, Saadia Farid, And Muhammad Tahir</li><li><a href="https://www.researchgate.net/publication/221405560_Index_to_Constant_Weight_Codeword_Converter">Index to Constant Weight Codeword Converter</a>, Jon T. Butler, Tsutomu Sasao</li><li>Quora Discussion: <a href="https://www.quora.com/How-do-I-convert-a-given-combination-to-a-single-number">How do I convert a given combination to a single number?</a></li><li><a href="http://code.iamkate.com/articles/hakmem-item-175/">HAKMEM item 175</a></li><li><a href="https://computationalcombinatorics.wordpress.com/2012/09/10/ranking-and-unranking-of-combinations-and-permutations/">Ranking and Unranking of Combinations and Permutations</a>, Derick Stolee</li><li><a href="https://saliu.com/bbs/messages/348.html">Algorithms, Code, Software to Calculate Combination Lexicographical Order, Rank, Index</a>, Ion Saliu</li><li><a href="http://suffe.cool/poker/evaluator.html">Cactus Kev’s Poker Hand Evaluator</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb4c9ea5476a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mop-developers/convert-a-combination-to-a-single-number-eb4c9ea5476a">Convert a  combination to a single number</a> was originally published in <a href="https://medium.com/mop-developers">Ministry of Programming — Technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>