<?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[Tech Front - Medium]]></title>
        <description><![CDATA[Bringing you the frontier of tech articles. We post tutorials about programming, algorithms, design, cloud, data science and more. Follow us to be updated! - Medium]]></description>
        <link>https://medium.com/techfront?source=rss----e5f458f93849---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Tech Front - Medium</title>
            <link>https://medium.com/techfront?source=rss----e5f458f93849---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:21:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/techfront" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Step-by-step visual guide on deploying a Flask application on AWS EC2]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/techfront/step-by-step-visual-guide-on-deploying-a-flask-application-on-aws-ec2-8e3e8b82c4f7?source=rss----e5f458f93849---4"><img src="https://cdn-images-1.medium.com/max/2600/1*2lZnEoXRKp8qQ28k-RzOvw.png" width="3000"></a></p><p class="medium-feed-snippet">Visually see and learn how to deploy your first flask app on AWS with gunicorn, nginx and EC2.</p><p class="medium-feed-link"><a href="https://medium.com/techfront/step-by-step-visual-guide-on-deploying-a-flask-application-on-aws-ec2-8e3e8b82c4f7?source=rss----e5f458f93849---4">Continue reading on Tech Front »</a></p></div>]]></description>
            <link>https://medium.com/techfront/step-by-step-visual-guide-on-deploying-a-flask-application-on-aws-ec2-8e3e8b82c4f7?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/8e3e8b82c4f7</guid>
            <category><![CDATA[techfront-web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kim Huiyeon]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 02:31:01 GMT</pubDate>
            <atom:updated>2020-06-09T12:30:46.761Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Memoization and Decorators]]></title>
            <link>https://medium.com/techfront/memoization-and-decorators-c6a0cc523af7?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/c6a0cc523af7</guid>
            <category><![CDATA[decorator-pattern]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[board-games]]></category>
            <category><![CDATA[tech-front-algorithm]]></category>
            <dc:creator><![CDATA[GABRIEL MANUEL SIDIK]]></dc:creator>
            <pubDate>Thu, 04 Jun 2020 14:31:00 GMT</pubDate>
            <atom:updated>2020-06-04T14:31:00.864Z</atom:updated>
            <content:encoded><![CDATA[<h4>WINNING GAMES WITH PYTHON</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5hsK0haa9A1Nh4zuF7tUaQ.png" /></figure><p><em>(Click </em><a href="https://medium.com/techfront/winning-at-risk-with-python-part-1-40d17d39d1e2"><em>here</em></a><em> for the first article of the series, and </em><a href="https://medium.com/techfront/winning-games-with-python-1bc99edaebb3"><em>here</em></a><em> for the previous article!)</em></p><h3>Recursion — solving subproblems to a larger problem</h3><p>To calculate the probabilities of the various outcomes in an extended battle in Risk, we would have to compute the probabilities through a recursion tree/recurrence equations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*TqCLHI3EntI6ZL30.png" /></figure><p>Imagine if we were to do this calculation by hand — it’s a little tedious but not that hard. In order to calculate the outcome probability distribution of a 4v2 deathmatch, we have to calculate the distributions of 4v0, 3v1, 2v2 deathmatches respectively. In the above image, we can see that these are “subtrees” to the larger 4v2 recurrence tree.</p><p>That’s going to take some calculations but it is still pretty manageable! However, if we were to perhaps analyze an EPIC deathmatch (say, a 15v15 deathmatch), we’ll probably decide to let a program run it.</p><p>We can use the recursive get_distribution_of_results function (introduced in the previous <a href="https://medium.com/techfront/winning-games-with-python-1bc99edaebb3">article</a>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27958021101b8973594cce8ee011bce4/href">https://medium.com/media/27958021101b8973594cce8ee011bce4/href</a></iframe><p>Let’s see the results -</p><p>…</p><p>It’s taking a while isn’t it?</p><p>…</p><p>A timed run of get_distribution_of_results(15,15) takes ~15* seconds on my local machine! Not too shabby, but let’s try it to calculate the distribution for a slightly bigger 16v16 deathmatch!</p><p>…</p><p>16v16 takes around ~45* seconds. And the 17v17 takes almost 2 minutes! (~110* seconds)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da3c294c9b15cc9c8098a74f878c68c/href">https://medium.com/media/4da3c294c9b15cc9c8098a74f878c68c/href</a></iframe><p>Why is this the case? Well, to demonstrate, let’s visualize the recurrence tree for a the 4v4 deathmatch:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3KqZ7qnN61Z72yBe5hL5aw.png" /><figcaption>Trees all the way down…</figcaption></figure><p>Notice that the 4v4 recurrence tree (the entire tree) is ~3 times bigger than the 3v3 sub-tree (bounded in the light blue box). We can also see this similar relationship between the 3v3 (light blue) and 2v2 subtrees (light red)!</p><p>This is a fundamental problem about recursive algorithms — particularly those which break the original problem into <strong><em>multiple </em></strong>sub-problems. Such recursive algorithms can be described as having exponential computational complexity with respect to the problem size.</p><p>Every time we move 1 level higher in the tree, there’ll be ~3 times more computations required. Going up just 2 levels, we’ll almost increase the problem by tenfold! — This is consistent with what we observed comparing the time taken for 15v15 against 17v17.</p><p>If one were to attempt to solve this manually using pen and paper, we’ll have to calculate them one by one, scribbling through many pieces of papers, perhaps using quite a few pieces of rough paper/sticky notes/<strong><em>memos </em></strong>to record the calculations that we have computed.</p><p>Wait! Hang on… perhaps we don’t have to keep doing the same calculations over and over again! If we were doing these calculations by hand, we could <strong><em>use the same results from other sub-problems which have already been solved!</em></strong></p><p>Visualizing this from the tree diagram, we have -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*tTFu7lI7160dkdLvAKvveA.png" /><figcaption>Visualization of calculations (using in order traversal) — Calculated results (thick boxes) will be reused later again instead of calculating repeated configurations (dotted boxes). Grayed out boxes are not calculated at all.</figcaption></figure><p>If we “<strong>memoize</strong>” or “<strong>cache</strong>” our calculated results, we would not need to repeat many of the subproblems that we have previously solved.</p><h3>Memoization</h3><p>Memoization is an idea in computer science where previous/intermediate computations are stored and recorded such that repetitions of the same calculation need not take place.</p><p>We can implement the exact same concept for our problem as well!</p><p>Let’s breakdown the pseudocode for this problem:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd28dfa13b0a65ef0c44047338baa22a/href">https://medium.com/media/cd28dfa13b0a65ef0c44047338baa22a/href</a></iframe><p>One approach to implement this properly in code is to rewrite the entire function. But is there a way to easily transform our previously recursive function get_distribution_of_results into a memoized version?</p><h3>Decorators</h3><p>The answer, in short, is yes! Using decorators, we can “decorate” our function and give it a “memo feature”.</p><p><strong>A decorator is a function which takes in a function as an input (?!), and outputs another function (?!) </strong>(typically an extended version of the input function).</p><p>This idea is pretty counterintuitive, a function taking in a function as an input and outputting another function?</p><p>Let’s see how it would look:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5bae025b313384f33019ae382bbc969/href">https://medium.com/media/f5bae025b313384f33019ae382bbc969/href</a></iframe><p>Here, we extend the original get_distribution_of_results function with the <em>ability to memoize. </em>Within the memoize function, we introduce a dictionary memo which is used as our cache (memory in the pseudocode).</p><p>We define a new function memoized_function within the memoize function and return it.</p><p>The memoized_function acts as an extension of our original function by checking the memo first for the subproblem — if it exists, then we can simply return the previously calculated answer, else, we will have no choice but to solve the subproblem. However, every time we solve a new problem, we will add the result into our memo! — This ensures that we do not repeat the same calculations that we have previously done.</p><p>Running this “upgraded” get_distribution_of_results function allows us to solve much larger problems much quicker! For example, running a 100v100 problem only takes 0.365 seconds.</p><p>In addition, we have the added benefit of “memory” — if we were to run a 90v90 problem right after calculating the results for 100v100, we would get the resultant probability distribution almost instantaneously as the 90v90 subproblem has already been solved and our new get_distribution_of_results function “remembers” the solution to the 90v90 problem!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*VlMACumN_pRcavX1mbhRkw.png" /><figcaption>Probability Distribution for a 100v100 deathmatch</figcaption></figure><h3>Conclusion</h3><p>This has been a challenging article — we’ve touched on 3 pretty challenging concepts:</p><ol><li>Computational Complexity</li><li>Memoization/Caching</li><li>Decorator Functions</li></ol><p>Don’t worry if you still don’t feel like you have a strong grasp on these concepts — this article covered a lot of ground!</p><p>Using these concepts, we have managed to make our Risk battle analyzer much more efficient. In the next few articles, we will be focusing more on the statistical aspect of the board game (leveraging on our memoized function to not have computation time holding us back!)</p><p>Till next time!</p><p><em>(Find the first article of the series </em><a href="https://medium.com/techfront/winning-at-risk-with-python-part-1-40d17d39d1e2"><em>here</em></a><em>)</em></p><h3>Further Resources</h3><p><a href="https://www.youtube.com/watch?v=v4cd1O4zkGw">Computational Complexity</a></p><p><a href="https://www.youtube.com/watch?v=P8Xa2BitN3I">Memoization/Caching</a></p><p><a href="https://www.python-course.eu/python3_memoization.php">Decorator Functions</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6a0cc523af7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techfront/memoization-and-decorators-c6a0cc523af7">Memoization and Decorators</a> was originally published in <a href="https://medium.com/techfront">Tech Front</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python for the artsy person]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/techfront/python-for-the-artsy-person-a0ef1d4debf1?source=rss----e5f458f93849---4"><img src="https://cdn-images-1.medium.com/max/1756/1*h51notIMmuH0OYnJT5gAbg.jpeg" width="1756"></a></p><p class="medium-feed-snippet">Overcoming fears and challenging assumptions</p><p class="medium-feed-link"><a href="https://medium.com/techfront/python-for-the-artsy-person-a0ef1d4debf1?source=rss----e5f458f93849---4">Continue reading on Tech Front »</a></p></div>]]></description>
            <link>https://medium.com/techfront/python-for-the-artsy-person-a0ef1d4debf1?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/a0ef1d4debf1</guid>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[life-lessons]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[life]]></category>
            <dc:creator><![CDATA[Lim Yuen Ying]]></dc:creator>
            <pubDate>Thu, 04 Jun 2020 00:31:01 GMT</pubDate>
            <atom:updated>2020-06-04T00:31:01.151Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Extended battles and Recursion]]></title>
            <link>https://medium.com/techfront/winning-games-with-python-1bc99edaebb3?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/1bc99edaebb3</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[recursion-tree]]></category>
            <category><![CDATA[board-games]]></category>
            <category><![CDATA[techfront-algorithm]]></category>
            <dc:creator><![CDATA[GABRIEL MANUEL SIDIK]]></dc:creator>
            <pubDate>Tue, 26 May 2020 02:31:01 GMT</pubDate>
            <atom:updated>2020-06-07T11:18:17.266Z</atom:updated>
            <content:encoded><![CDATA[<h4>Winning Games with Python</h4><h3>Using Recursion to analyze games</h3><p><em>(Find the previous related article </em><a href="https://medium.com/techfront/winning-at-risk-with-python-part-1-40d17d39d1e2"><em>here</em></a><em>)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BZyjb6brS0ELU7YmHTQCZw.png" /></figure><p>Last we left of, we had derived the probabilities of any given Risk battle scenario:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c95b8af3be11ed6269cc8e3e977e2662/href">https://medium.com/media/c95b8af3be11ed6269cc8e3e977e2662/href</a></iframe><p>We found that the best odds for the attacker are when there are 3 active attackers.</p><p>However, this only describes the probabilities of 1 battle — 1 round of dice rolls.</p><p>Imagine that the attacker has 4 attacking units and the defender has 2, and the attacker seeks to keep attacking until all the defending units are defeated or he is no longer able to attack (for simplicity, we shall call this a death match), we can map out all the possible scenarios as such:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*_exfWkHTdPMCT6KiH-He7w.png" /><figcaption>Scenario Tree for 4 versus 2 battles — blue boxes indicate the scenarios that the attacker wins, red boxes are the scenarios that the defender wins.</figcaption></figure><p>It would intuitively seem that there are more scenarios in which the attacker would win — but by how much? To calculate this, we would have to calculate the probability of each end-state occurring, which we can do by multiplying the probabilities that we have obtained in the previous article!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*oXXUWkyvNOdVtcTlnLPAhQ.png" /><figcaption>Bolded numbers below each “end” scenario is the probability of reaching that scenario</figcaption></figure><p>We can find the probability of each end-node by multiplying the probabilities of the branches leading down to it — for example, to calculate the “3v0” end node, you can multiply 0.336 (at the first level) by 0.660 (at the 2nd level), giving us 0.336 * 0.660 = 0.2218.</p><p>To figure out the total probability of winning, we would have to sum the probabilities of all the <em>winning</em> end nodes — 0.372 + 0.2218 + 0.07205 + 0.02185 + 0.06680 + 0.03959 = 0.794.</p><p>Almost a 4 in 5 chance of winning!</p><h3>Calculating the probabilities of winning — programmatically</h3><p>When we calculated the probabilities of winning manually, we had to calculate the probabilities of each of the end states, and subsequently sum them all up.</p><p>What we could do programmatically is to figure out for the top node state, what the probabilities of each of the subsequent states are. If the subsequent state is an end state, our job is done and we can add that to our final sum; but if it isn’t, we will have to check what the probabilities of each of the subsequent states are. In pseudocode, this would look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4234ac80c161f68198e4edc6fa52799a/href">https://medium.com/media/4234ac80c161f68198e4edc6fa52799a/href</a></iframe><p>Notice that we are calling the function itself again — we are calling find_end_state_prob within itself to solve the same problem for a sub-node. This is an idea in computer science known as <strong>recursion.</strong></p><h4><strong>Recursion</strong></h4><p>The idea of a function calling itself is something that can be pretty confusing for someone not familiar with programming/computer science. In this context, I feel that it is helpful to think about manually solving this problem -</p><p>If we were seeking to calculate manually the possible outcomes of this 4v2 battle, we will start by constructing the tree, and realizing that we have the same subproblem, only for a smaller case this time.</p><p>We could then perhaps call a friend (say Alice) and say “Hey, can you help me calculate the probability of winning a 3v1 deathmatch?”</p><p>Alice would then construct the tree and then ask <em>her</em> friend (say Bob) if he could help her calculate the probability of winning a 2v1 deathmatch. When Bob returns Alice the probabilities for 2v1, she can then compute the probability of the 3v1 deathmatch problem.</p><p>The key idea is to solve the big problem by breaking it down into smaller subproblems!</p><h4>Recursion in Risk…</h4><p>Properly implementing the pseudocode presented earlier — we have:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeb7d1a6369a616bb4c29be5c7357342/href">https://medium.com/media/aeb7d1a6369a616bb4c29be5c7357342/href</a></iframe><p>A lot of comments have been added to this function for easy following.</p><p>This function, in addition to being recursive (calling itself), also uses a helper function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34123323c069458093311cd962a195d6/href">https://medium.com/media/34123323c069458093311cd962a195d6/href</a></iframe><p>There’s quite a lot to take in for this recursive function — I would highly recommend you to try writing your own version of this recursive function as practice!</p><h3>Some Results</h3><p>Let’s find out and visualize the probabilities associated with a 10v10 deathmatch:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be5ef40d0f29d2975e67b5d3999e7f5b/href">https://medium.com/media/be5ef40d0f29d2975e67b5d3999e7f5b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*GRj4ETYXfUBcv2tl-b8x2w.png" /><figcaption>Probability distribution of 10v10 deathmatch</figcaption></figure><p>The x-axis of the distribution represents the various possible end-states, with negative numbers indicating that the defender wins (-10 refers to when a defender wins with 10 defenders remaining).</p><p>An interesting observation to note — the probability distribution is actually bimodal. We can also obtain some basic statistics from formatted_results as such:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54073e821c79c09dde021b4cb80e00d9/href">https://medium.com/media/54073e821c79c09dde021b4cb80e00d9/href</a></iframe><p>The first is the expected value — that is, the expected amount of units we will have left (in this case just about 1). The probability of winning is also significantly above 50% (at 56.8%) for a 10v10 battle.</p><p>How much different would it be if it was 10v9 instead?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*rnLzOeeeu68m3pRNRuuuNQ.png" /><figcaption>Comparing the histograms of 10v10 and 10v9 we can see a small but significant advantage for the attacker in the 10v9 deathmatch scenario.</figcaption></figure><p>We have a significantly higher probability of winning (65%) and an expected value of almost 2 (1.96).</p><p>Let’s do a sensitivity analysis around the 10v10 death match</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04b4ff4374014d860de2c8a465d3ecde/href">https://medium.com/media/04b4ff4374014d860de2c8a465d3ecde/href</a></iframe><p>So it seems that even the slightest change in numbers one way or the other can have a drastic effect on the probability of winning a deathmatch!</p><h4>Conclusion</h4><p>The moral of the story still seems to be — Attack whenever you have numerical superiority! Just a small edge changes the probabilities by a significant extent!</p><p>In this article, we’ve explored <strong>probabilities trees</strong> (mathematics/probability concept) as well as tried our hand at implementing them in Python using <strong>recursion</strong>!</p><p>Some of you may recognize probability trees in a different context (binomial/trinomial models in mathematical finance) and if so, you may be interested in seeing a more formal mathematical treatise of the problem <a href="https://drive.google.com/file/d/1yF-b6AVoTgD2oPgL2rbvUiffsr0S0yZV/view?usp=sharing">here</a>!</p><p>Till next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1bc99edaebb3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techfront/winning-games-with-python-1bc99edaebb3">Extended battles and Recursion</a> was originally published in <a href="https://medium.com/techfront">Tech Front</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Winning at Risk with Python — Part 1]]></title>
            <link>https://medium.com/techfront/winning-at-risk-with-python-part-1-40d17d39d1e2?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/40d17d39d1e2</guid>
            <category><![CDATA[techfront-data-science]]></category>
            <category><![CDATA[tutorial-python]]></category>
            <category><![CDATA[game-theory]]></category>
            <category><![CDATA[probability]]></category>
            <category><![CDATA[board-games]]></category>
            <dc:creator><![CDATA[GABRIEL MANUEL SIDIK]]></dc:creator>
            <pubDate>Tue, 19 May 2020 02:31:00 GMT</pubDate>
            <atom:updated>2020-05-24T11:55:17.938Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Winning games with Python</h4><h3>Hasbro Risk Part 1: Mechanics and basic probabilities</h3><figure><img alt="Risk: The World Conquest Game" src="https://cdn-images-1.medium.com/max/628/1*IQ7dnbR9Hh_xeewhepwIHQ.png" /><figcaption>(Image taken from Hasbro’s official <a href="https://www.hasbro.com/common/instruct/risk.pdf">rule book</a>)</figcaption></figure><p>In my own spare time during this circuit breaker (a.k.a social distancing) period, I sometimes fall back to explore online versions of traditional board games I used to play. One of which is Hasbro’s Risk.</p><p>As someone who likes to understand the mechanics of games, I decided to dig deeper to understand this beloved childhood game I used to play. Looking online, I found quite a few resources (and even journal articles!) of analyses done on this well-loved game (this article on <a href="https://www.businessinsider.com/how-to-use-math-to-win-at-the-board-game-risk-2013-7?IR=T">Business Insider</a> is an easy read, or if you feel especially technical, you can check out these deeper analyses done by folks at <a href="https://web.mit.edu/sp.268/www/risk.pdf">MIT</a> and <a href="https://web.stanford.edu/~guertin/risk.notes.html">Stanford</a>)</p><p>I’ve had initially intended to build an interactive web application for exploring various scenarios and probabilities in Risk. However, while planning out what I would like to explore, I’ve realized that there are quite a few interesting concepts that pop up (Hint: probability, statistics, recursion, memoization, game theory, even a bit of finance etc.). Therefore, I’ve decided to write a series to walkthrough my personal journey in learning more about this game — hopefully also highlighting and teaching some of these relevant concepts along the way.</p><h3>An Introduction to Risk</h3><blockquote>In the classic “World Domination RISK®” game of military strategy, you are battling to conquer the world. To win, you must launch daring attacks, defend yourself on all fronts, and sweep across vast continents with boldness and cunning. But remember, the dangers, as well as the rewards, are high. Just when the world is within your grasp, your opponent might strike and take it all away!</blockquote><p>Risk is a turn-based game centered around a world map —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*ytSDjxVLmdsC6EGd.png" /><figcaption>(Image from Wiki Commons <a href="https://commons.wikimedia.org/wiki/File:Risk_board.svg">here</a>)</figcaption></figure><p>The world map is split into continents (each one colored differently in the figure above), which is further split into individual territories.</p><p>In one common variant, players place units one-by-one on an unoccupied territory (thus owning that territory). Once all territories are occupied, players will begin to “reinforce” their existing territories by adding more units to their existing territories until all players have placed their initial allocated number of units.</p><p>At the start of each player’s turn, the player will receive some units to reinforce current territories (this is based on the number of territories occupied, continents fully under their control and trading in of cards).</p><p>Subsequently, the player will be able to choose to attack neighbouring territories owned by other players. This attack phase will be a key component of the analysis that we will explore in this article and the next subsequent few articles of this series!</p><p><em>To find out more details about the official rules, feel free to check out Hasbro’s official rule book </em><a href="https://www.hasbro.com/common/instruct/risk.pdf"><em>here</em></a><em>!</em></p><h4>The Attack Phase</h4><p>A couple basic rules about attacking -</p><ul><li>One can only attack adjacent territories</li><li>The territory you are attacking from must retain at least 1 “garrison unit” — i.e. if you have 2 units in a territory and intend to attack an opposing territory, 1 unit must remain as “garrison” for the current territory and only 1 unit can attack.</li><li>A maximum of 3 units can participate in an “attack” (represented by dice rolls) and a maximum of 2 units can “defend” (defender dice rolls) in a particular battle.</li><li>To resolve a battle, line up the dice rolls in descending order — the highest “attacker” vs “defender” pair of dice will be compared and likewise for the 2nd pair. In the case of a tie — the defender would win (a so-called “defenders’ advantage”)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*-Y9zmNjPYPA6UnQCjgaY2A.png" /><figcaption>(Image taken from Hasbro’s official <a href="https://www.hasbro.com/common/instruct/risk.pdf">rule book</a>)</figcaption></figure><p><em>To find out more details about the official rules, feel free to check out Hasbro’s official rule book </em><a href="https://www.hasbro.com/common/instruct/risk.pdf"><em>here</em></a><em>!</em></p><h3>Let’s jump into analysing this with Python!</h3><p>The first order of business then would be to understand the various probabilities involved in different kind of “battle scenarios”! In Risk, there would be a total of 6 attacker-defender scenarios - (1v1, 1v2, 2v1, 2v2, 3v1, 3v2).</p><p>To decide whether one should attack another territory, we will want to know the probabilities associated with each of these scenarios.</p><p>One way of doing this would be to iterate through all the possibilities of dice rolls for each scenario, and determining through comparison the result of the battle (how many units does the attacker and defender lose).</p><p>Here is a sample Python code that does this for the 3v2 scenario:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87cae4796445b4a5e57351f595be1099/href">https://medium.com/media/87cae4796445b4a5e57351f595be1099/href</a></iframe><p>There are quite a few things going on here:</p><h4>Generating all the possible combinations of dice rolls</h4><p>Firstly, I used the product function from the Python itertools library to generate all the possible dice rolls in a 3v2 scenarios (line 30–37). As the 3v2 scenario would have a total of 5 dice, and each die roll can take on a value between 1–6, the product function, when taking in this range of numbers (1-6) and the repeat keyword set to 5, all possible dice rolls possible will be enumerated.</p><h4>Simulating the outcome of the battle from the dice rolls</h4><p>For each of the possible combinations, I call a function determine_win_of_attacker_3v2 to… determine the number of units of the attacker which will win.</p><p>Within the determine_win_of_attacker_3v2 function, I use the in-built Pythonsorted function to sort the dice combinations in descending order.</p><p>And subsequently check what the battle outcome is for each pair of dice rolls.</p><p>I store the result of each dice roll in the dictionary probabilities_3v2 (to be exact these are not probability measures, but rather the number of dice roll combinations which result in a particular battle outcome).</p><p>Printing out the output dictionary we get:</p><pre>{&#39;win2&#39;: 2890, &#39;win1&#39;: 2611, &#39;win0&#39;: 2275}</pre><p>This means that out of the 6⁵ possible dice states, there are 2890 states in which 2 attackers will win, 2611 states in which 1 attacker will win, and 2275 states in which the 0 attackers will win. To get the sense of the probabilities — we can use a normalize function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dacbba257f5b2bd7db42fbe420794a46/href">https://medium.com/media/dacbba257f5b2bd7db42fbe420794a46/href</a></iframe><p>This function divides each of the possible end states by the total number of states possible — giving us the probabilities associated with the outcome of the battle.</p><p>We obtain the normalized probabilities as such:</p><pre>{&#39;win2&#39;: 0.37165637860082307, &#39;win1&#39;: 0.3357767489711934, &#39;win0&#39;: 0.2925668724279835}</pre><p>We can do this analysis for all possible battle scenarios and obtain the respective results:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3e865f3b504d1dd765a169c0a93488d/href">https://medium.com/media/e3e865f3b504d1dd765a169c0a93488d/href</a></iframe><p>A couple things that clearly stand out — in 3v1, 2v1, 1v2, 1v1 battles, as there is only ultimately 1 “skirmish” (die vs die comparison) there is no way for the attacker to win 2 skirmishes.</p><p>We have also shown the underlying intuition for individual battle scenarios — the more dice we have rolling, the better the odds are. Compare the 3v1 battle, where there is almost a 1/3 chance of winning outright against the 1v2 battle — where there is a 3/4 chance of losing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a552f032bc2f15ea95332aefc136363/href">https://medium.com/media/1a552f032bc2f15ea95332aefc136363/href</a></iframe><h3>Conclusion</h3><p>So that’s our first rule-of-thumb for World Domination - <strong>attack with as many (3) units if you can, local superiority is key!* </strong>— next, we will be exploring the probabilities of extended battles (when we have much more than 3 attackers against much more than 2 attackers)</p><p>I’ve also shown how to use the itertools Python library, as well as some basic data structures (dictionaries)that we used to store information in. On the programming side of things, we’ll be exploring some recursion and memoization moving on and a bit more probability/statistics.</p><p><em>*Unless… For reasons we will explore further down the road</em></p><h3><strong>Resources and further readings</strong></h3><ol><li>Can’t wait for the next article of the series to find out more? — <a href="http://datagenetics.com/blog/november22011/index.html">DataGenetics</a> has quite an in-depth analysis on Risk (in the course of this series, I intend to go much deeper in the analysis though). Also, I feel honor-bound to shout out to them as well! I used the published results from DataGenetics to make sure my computed probabilities are not incorrect. Because bugs.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40d17d39d1e2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techfront/winning-at-risk-with-python-part-1-40d17d39d1e2">Winning at Risk with Python — Part 1</a> was originally published in <a href="https://medium.com/techfront">Tech Front</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Callback Hell: Simply Explained]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/techfront/javascript-callback-hell-simply-explained-93c3cf4be884?source=rss----e5f458f93849---4"><img src="https://cdn-images-1.medium.com/max/2600/1*piQB9fJ-VJf27TUIc8MT2w.png" width="3000"></a></p><p class="medium-feed-snippet">The simplest explanation and examples to understand Javascript Callback Hell</p><p class="medium-feed-link"><a href="https://medium.com/techfront/javascript-callback-hell-simply-explained-93c3cf4be884?source=rss----e5f458f93849---4">Continue reading on Tech Front »</a></p></div>]]></description>
            <link>https://medium.com/techfront/javascript-callback-hell-simply-explained-93c3cf4be884?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/93c3cf4be884</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[techfront-web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Le Van Tuan Long]]></dc:creator>
            <pubDate>Mon, 18 May 2020 22:26:58 GMT</pubDate>
            <atom:updated>2020-05-19T10:37:59.653Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Javascript Callback Function: Simply Explained]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/techfront/javascript-callback-function-simply-explained-365b16fe83d9?source=rss----e5f458f93849---4"><img src="https://cdn-images-1.medium.com/max/2600/1*lNM7-mho95IS4auhoKfTzA.png" width="3000"></a></p><p class="medium-feed-snippet">The simplest explanation and example to understand Javascript Callback Function</p><p class="medium-feed-link"><a href="https://medium.com/techfront/javascript-callback-function-simply-explained-365b16fe83d9?source=rss----e5f458f93849---4">Continue reading on Tech Front »</a></p></div>]]></description>
            <link>https://medium.com/techfront/javascript-callback-function-simply-explained-365b16fe83d9?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/365b16fe83d9</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[techfront-web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Le Van Tuan Long]]></dc:creator>
            <pubDate>Mon, 18 May 2020 17:01:46 GMT</pubDate>
            <atom:updated>2020-05-19T10:37:05.030Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How I built an AI to beat Flappy.. Cat?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/techfront/how-i-built-an-ai-to-beat-flappy-cat-97899d66e4d1?source=rss----e5f458f93849---4"><img src="https://cdn-images-1.medium.com/max/1245/1*XFB1NY11OAtYvAk6jGb85Q.png" width="1245"></a></p><p class="medium-feed-snippet">Using JavaScript and Neural Networks to beat the extremely annoying game.</p><p class="medium-feed-link"><a href="https://medium.com/techfront/how-i-built-an-ai-to-beat-flappy-cat-97899d66e4d1?source=rss----e5f458f93849---4">Continue reading on Tech Front »</a></p></div>]]></description>
            <link>https://medium.com/techfront/how-i-built-an-ai-to-beat-flappy-cat-97899d66e4d1?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/97899d66e4d1</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[techfront-web-development]]></category>
            <category><![CDATA[techfront-data-science]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kim Huiyeon]]></dc:creator>
            <pubDate>Mon, 18 May 2020 02:31:01 GMT</pubDate>
            <atom:updated>2020-05-18T02:31:01.466Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Javascript Closure: Simply Explained]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/techfront/javascript-closure-simply-explained-a735ba546537?source=rss----e5f458f93849---4"><img src="https://cdn-images-1.medium.com/max/2600/1*clx5zpoWZfcqd28tby-Qzw.png" width="3000"></a></p><p class="medium-feed-snippet">The simplest explanation and examples to understand Javascript Closure</p><p class="medium-feed-link"><a href="https://medium.com/techfront/javascript-closure-simply-explained-a735ba546537?source=rss----e5f458f93849---4">Continue reading on Tech Front »</a></p></div>]]></description>
            <link>https://medium.com/techfront/javascript-closure-simply-explained-a735ba546537?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/a735ba546537</guid>
            <category><![CDATA[techfront-web-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Le Van Tuan Long]]></dc:creator>
            <pubDate>Fri, 15 May 2020 04:54:58 GMT</pubDate>
            <atom:updated>2020-05-19T10:38:43.295Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[You cannot get bored with Boyer Moore Algorithm!]]></title>
            <link>https://medium.com/techfront/you-cannot-get-bored-with-boyer-moore-algorithm-b98d564ecd55?source=rss----e5f458f93849---4</link>
            <guid isPermaLink="false">https://medium.com/p/b98d564ecd55</guid>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Kim Huiyeon]]></dc:creator>
            <pubDate>Wed, 13 May 2020 13:45:44 GMT</pubDate>
            <atom:updated>2020-05-13T13:45:28.514Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mqubjdtD5pRchMP5-sUjuA.jpeg" /></figure><p>I am back with another algorithm which sort of blew my mind. Its solves a problem which is seen many times and has a lot of different solutions to it. Boyer-Moore’s Majority Vote algorithm is really something. Lets dive into the problem statement of what this algorithms solves!</p><p><strong>Problem:</strong> Given an array of n numbers, find the <strong>Majority Element</strong>. (Majority element is basically a number which appears more than n/2 times). Basically we just need to find the element which appears in more than half of the positions of the array. Here are some sample input/outputs.</p><p><strong>Input 1:</strong> [1,3,4,3,3,3,3]</p><p><strong>Output 1:</strong> 3</p><p>This is because there are 7 items in total and “3” appears 5 times in the array which is of course more than 7/2 which is 3. (3.5 to be exact but we take as 3).</p><p><strong>Input 2:</strong> [5,5,3,5,4,4,4,3,3,3,3,3,3]</p><p><strong>Output 2:</strong> 3</p><p>Again, there are 13 elements in the list and “3” appears 7 times which is more than 13/2 = 6.</p><p>Sounds pretty simple? Well it isnt a hard question to solve as there are many solutions to it. In this article, I will go through 3 different solutions and show why Boyer-Moore’s algorithm is so cool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*CUt5dv7_zrrCQDHM.gif" /></figure><h3>Method 1: Sort it out.</h3><p>This method is the shortest one out of the three methods and possibly the easiest to understand. Since the problem states that the majority element appears more than half times in the array, we can just sort the array and take the middle element! No matter what the value of the item is, the middle item will always be the majority element. It is sort of like finding the median of the array.</p><p>Lets take a look at the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afc308bac8a902458d44dd362157e8a8/href">https://medium.com/media/afc308bac8a902458d44dd362157e8a8/href</a></iframe><p>So short and so simple right? It is! And it is pretty fast too. With time complexity of O(nlogn) and Space of O(1). Great!!</p><p>But of course we can do better than this. Introducing Hashing (Hashing usually makes everything better).</p><h3>Method 2: Hash it out.</h3><p>We can keep a HashMap of the item as the key and the frequency of the item as the value. Once the value of any key becomes greater than half of the length of the array, the key is the answer. Its pretty simple, lets check it out in code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ede25b0e528d3c6cbcd5affec0ef555/href">https://medium.com/media/0ede25b0e528d3c6cbcd5affec0ef555/href</a></iframe><p>Well its pretty simple too. The map will store the frequency of all the items in the array and when the frequency becomes higher than the length/2, we return the key! Its easy, it faster than the previous one! With time complexity of O(n), this is a good enough solution right? Well yeah it is but retrieving from a map is an amortized O(1). So its not always O(1). Moreover, time taken to hash might be quite long, depending on the algorithm to hash. Finally, it takes O(n) space.</p><p>We can do better than this right? Lets bring in Boyer Moore’s Algorithm and see how to make this better.</p><h3>Method 3: Boyer-Moore’s Algorithm</h3><p>This algorithm is actually pretty simple. There is no need of any special data structure nor does it require an insane algorithm. With just two variables, we can get the same output.</p><p>As I mentioned, we need 2 variables: counter and result. When the counter variable is zero, we set the result to the current element in the list and increment the counter variable. For subsequent elements, if the subsequent element is equal to the element stored in result, we increment the counter variable. When the subsequent element is not equal to the result then we decrement the counter variable. Of course, when the counter variable reaches zero, then the result variable will be set to the element in the current position.</p><p>The result variable will store the final answer, the majority element. Lets see it through code to see how it works again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/241f77e8059890edc5de0ecfff493a51/href">https://medium.com/media/241f77e8059890edc5de0ecfff493a51/href</a></iframe><p>As you can see, the first if statement checks if the counter is zero and updates the result variable if it is. Then in the next loop, since the counter variable is not zero, it will check if the current element i is equal to result and if it is, it will increment counter and if not it will decrement counter. In the end, we just return result and voila!! That’s it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*t8skzCg6Cu8qUBgT.png" /></figure><p>This image from <a href="https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm">Wikipedia</a> visualizes this algorithm perfectly.</p><p>And that’s it! Congrats on making it this far and I hope to see y’all soon!</p><p><em>Originally published at </em><a href="https://learnwars.com/cannot-get-bored-with-boyer-moore-algorithm"><em>https://learnwars.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b98d564ecd55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techfront/you-cannot-get-bored-with-boyer-moore-algorithm-b98d564ecd55">You cannot get bored with Boyer Moore Algorithm!</a> was originally published in <a href="https://medium.com/techfront">Tech Front</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>