<![CDATA[Code Stories]]>https://tech.abhimanyutimes.comhttps://cdn.hashnode.com/res/hashnode/image/upload/v1637933916139/-Jsc0NFUx.pngCode Storieshttps://tech.abhimanyutimes.comRSS for NodeSat, 22 Jun 2024 22:50:48 GMT60<![CDATA[Code Stories: An Introduction]]>https://tech.abhimanyutimes.com/code-stories-an-introductionhttps://tech.abhimanyutimes.com/code-stories-an-introductionSat, 26 Dec 2020 20:26:46 GMT<![CDATA[<p><span class="s"></span></p><h1 id="code-stories-an-introduction">Code Stories: An Introduction</h1><img alt="Image for post" src="https://miro.medium.com/max/3600/1*zQXjtQ-Hv-4UiQk0Sd8ykg.png" /><p>Making Computer Science simple bit by bit</p><blockquote><p>If you cant explain it simply, you dont understand it well.</p></blockquote><p>Computer Science is often seen as an esoteric discipline. Most of the learners approach it in a convoluted fashion. You hear complex mathematical jargon thrown around in corridors.On top of that Pop culture left no stone unturned to depict Programming as a niche skill possessed by socially anxious (awkward) individuals.</p><p>However, I cant blame them entirely. The complexity of a subject depends on a variety of things like:</p><ul><li><em>Are you exposed to similar branches of knowledge?</em></li><li><em>What do you want to achieve by studying it?</em></li><li><em>How much time & interest do you have?</em></li><li><em>How do you plan to approach the subject?</em></li></ul><p>Often learners skip the part where they honestly answer these questions.This leads to all kinds of problems.It ultimately results in their dry outlook towards a rather succulent and creative branch of knowledge.</p><p>Being a Computer Science graduate, I can assure you, things are not as difficult as they look like. Sure, they require novel thinking and good problem-solving skills. But like I said, these are skills and can be developed.</p><p>Therefore today, I am starting a series of articles that will take a programming problem and deconstruct it into intuitive puzzles.</p><p>You will see, how typical problems can be solved without any complex mumbo-jumbo if we ask ourselves the right questions and approach them systematically.</p><p><em>In the coming series of essays, we will follow a consistent structure:</em></p><ul><li><strong>The Problem</strong> </li><li><strong>The Strategy</strong> π‘</li><li><strong>The Code π</strong></li></ul><p>We will start with a detailed problem statement and gradually proceed towards the solution by building upon the <em>existing knowledge</em> and <em>intuition</em>. Towards the end, we will code that solution away.</p><p>I am a big fan of <strong><em>community learning</em></strong>.When you learn together, it leads to an understanding that is beyond the realm of an isolated study session.The best way to achieve this is to share your learnings with the community that helped you grow.</p><p>I strongly feel that these essays will rekindle your curiosity and soon you will see how fun problem solving is.</p><blockquote><p>The journey of <strong>thousand</strong> commits begins with a single <strong>init</strong> π</p></blockquote><p><span class="iv ee fo iw ix iy"></span><span class="iv ee fo iw ix iy"></span><span class="iv ee fo iw ix"></span></p><p><em>Want more of this?</em></p><p><em>You can connect on Twitter here:</em><a target="_blank" href="https://twitter.com/abshekha"><strong><em>abshekha</em></strong></a></p>]]><![CDATA[What is SGID in Linux?]]>https://tech.abhimanyutimes.com/what-is-sgid-in-linuxhttps://tech.abhimanyutimes.com/what-is-sgid-in-linuxThu, 12 Nov 2020 18:16:49 GMT<![CDATA[<p>A few weeks ago, I stumbled upon an interesting problem.<br />In Linux, if you check the file permissions you will notice the name of the file owner along with the group which owns the file. Note that, It is <strong>not</strong> necessary for the owner to be a part of the group but usually that is the case.</p><p><strong>Imagine this scenario:</strong></p><blockquote><p>There are 3 processes operating in a system:</p><ul><li><strong>P1</strong>: Runs as <em>root</em></li><li><strong>P2</strong>: Creates the directories during the install.</li><li><strong>P3</strong>: It access files created by P1.</li></ul></blockquote><p>Further, P2 & P3 are the part of the same group, say <em>user_grp</em> and both run as owner <em>user</em>.<br />Now consider a directory <strong><em>docs</em></strong> which is created by our installer process P2 and its permission looks like this:</p><pre><code><span class="hljs-attribute">drwxr</span>-x--- <span class="hljs-number">5</span> user user_grp <span class="hljs-number">4096</span> Jul <span class="hljs-number">31</span> <span class="hljs-number">19</span>:<span class="hljs-number">56</span> docs</code></pre><p>P1 gets into the directory and wants to create a file called <strong><em>readme.txt</em></strong> for process P3 to read.<br />Please note that there is an additional constraint here, the read, write & execute access for OTHERS is prohibited.<br />Nevertheless, P1 goes ahead and creates the file whose permission looks like this:</p><pre><code> <span class="hljs-string">-rwxr-----</span> <span class="hljs-number">1</span> <span class="hljs-string">root</span> <span class="hljs-string">root</span> <span class="hljs-number">4096 </span><span class="hljs-string">Jul</span> <span class="hljs-number">31</span> <span class="hljs-number">19</span><span class="hljs-string">:56</span> <span class="hljs-string">readme.txt</span></code></pre><p><strong><em>Can you spot the problem here?</em></strong><br />After carefully examining the situation, it is not difficult to judge that Process P3 will fail in reading the file <em>readme.txt</em> because it is neither the owner of file <em>readme.txt</em> nor part of the same group.<br />There are a lot of ways one can go about solving this access limitation. For example, you can just issue <em>chgrp</em> for the file to change its group to <em>user_grp</em> so that process P3 can access, as it will then belong to the same group. But if there are a lot of files like this in the docs directory and you want P3 to read all of them, rather than firing <em>chgrp</em> for each file, you can opt for a more elegant approach.</p><p>Let's issue this command on <strong><em>docs</em></strong> directory to set <strong><em>GID bit</em></strong>, we will come back to what it actually means in a moment.</p><pre><code>$ <span class="hljs-keyword">chmod</span> g+<span class="hljs-keyword">s</span> docs</code></pre><p>Look at the file permission of the <strong><em>docs</em></strong> directory:</p><pre><code><span class="hljs-attribute">drwxr</span>-s--- <span class="hljs-number">5</span> user user_grp <span class="hljs-number">4096</span> Jul <span class="hljs-number">31</span> <span class="hljs-number">19</span>:<span class="hljs-number">56</span> docs</code></pre><p>Notice there is an <strong><em>'s'</em></strong> in place of <strong><em>'x'</em></strong> in the directory permissions. Now let's allow process P1 to create a file <strong>readme2.txt</strong> in the docs directory.</p><pre><code><span class="hljs-string">-rwxr-----</span> <span class="hljs-number">1</span> <span class="hljs-string">root</span> <span class="hljs-string">user_grp</span> <span class="hljs-number">4096 </span><span class="hljs-string">Jul</span> <span class="hljs-number">31</span> <span class="hljs-number">19</span><span class="hljs-string">:56</span> <span class="hljs-string">readme2.txt</span></code></pre><p>This is the magic of SGID!<br />When you set the SGID on a directory, all the files and directories inside it will inherit the parent directory's group.<br />So even though <strong><em>root</em></strong> is creating the file <strong><em>readme2.txt</em></strong> inside <strong><em>docs</em></strong>, the process P3 can easily read it because the file now has the same group as process P3, i.e. <strong>user_grp</strong> instead of <strong>root</strong>. Hence, SGID bit can be very useful for dealing with situations like the one above. </p><blockquote><p><strong><em>Linux holds deeper truths, we just have to MAN [1] up to get there!</em></strong></p></blockquote><h3 id="notes">Notes</h3><p>[1] I am not trying to label courage as a masculine trait. It is just that Linux manual is called MAN pages. So a <em>puny</em> pun π</p><hr /><blockquote><p>If you liked this post, you can find more by:</p><p>Following me on <strong>Twitter</strong>: <a target="_blank" href="https://twitter.com/abshekha">@abshekha</a></p><p>Connecting on <strong>LinkedIn</strong>: <a target="_blank" href="https://www.linkedin.com/in/abhimanyubitsgoa/">abhimanyubitsgoa</a></p><p>Visiting my <strong>website</strong>: <a target="_blank" href="https://abhimanyutimes.com">https://abhimanyutimes.com</a></p><hr /></blockquote><p>Thanks for reading!</p>]]>https://cdn.hashnode.com/res/hashnode/image/upload/v1605681529498/CjjXmpHG-.png<![CDATA[Newton & Square Roots]]>https://tech.abhimanyutimes.com/newton-and-square-rootshttps://tech.abhimanyutimes.com/newton-and-square-rootsSun, 08 Nov 2020 14:43:50 GMT<![CDATA[<p>The web is an interesting place, it humbles you down and shows how little you actually know. While hitchhiking through the hyperlinks, I had one such realization recently.</p><p>What if I ask you to calculate the square root of a number? Depending upon your background you are likely to come up with a variety of solutions. Let me talk about that specific fraction of you who will be interested in churning out code for doing so. <em>[No, sqrt()]</em></p><p><em>Binary Search</em> seems to be a valid and intuitive approach, after all, we can easily discard half of the search space in each iteration. But I want you to hold that thought and glance over this small snippet below.</p><pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">sq</span><span class="hljs-params">(<span class="hljs-keyword">double</span> num)</span></span>{ <span class="hljs-keyword">double</span> xn = num/<span class="hljs-number">2</span>; <span class="hljs-keyword">while</span>(<span class="hljs-built_in">abs</span>((xn*xn)-num)>eps) <span class="hljs-comment">//eps is precision</span> { xn = (xn + num/xn)/<span class="hljs-number">2</span>; } <span class="hljs-keyword">return</span> xn;}</code></pre><p>Magical isn't it? When I first looked at this, I was really startled by the elegance of this solution. So what is it and why does it work?</p><h2 id="sweet-mathematics">Sweet Mathematics</h2><p>You just witnessed the glamour of <strong><em>Newton Raphson</em></strong> method.<br />In simple terms, it is a way to quickly find a good approximation for the roots of a real-valued function <strong>f(x) = 0</strong>.</p><p>This is our cue, square root for a number can be minimally represented by solving for the roots of <strong>f(x) = x^2- N</strong>, where <em>N</em> is the number we have to find the root of.<br />That is all cool! But how does it actually work? Let's look at the simple math behind this:-</p><ol><li><p>Consider a general function: <strong>y = f(x)</strong><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1604846773594/B6w6l1ZcC.png" alt />We start by choosing an arbitrary value <strong>x1</strong> as our starting point[1]. For our specific case, we can choose <strong>N/2</strong> as our starting point and can safely discard values greater than that from the candidature of <strong>N</strong>. </p></li><li><p>Now let's draw a tangent (Differential) to the curve at that point and mark its intersection with the x-axis. Let's call this <strong>x2</strong>.<br /><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1604846774809/YmDc6IqYJ.png" alt /> </p></li><li><p>Repeat <em>step 2</em> for point <strong>x2</strong> to obtain <strong>x3</strong>. You must have noticed that with each iteration we are nearing the roots of the quadratic equation. Slow and steady!<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1604846776063/WMvVeaX6X.png" alt /><em>Depending upon the precision, you can decide upon the number of iterations required.</em> </p></li></ol><h2 id="formal-expression">Formal Expression</h2><p>Let's try to workout an elegant formula for <strong>f(x) = x^2 - N</strong> which will get us the compact code we saw in the beginning. Please note that we are aiming to get a relation between <strong>N</strong> and <strong>(N+1)</strong> iteration.</p><ul><li>For any point <strong>xn</strong>, by using the equation of line we can get:<br /><strong>f'(x[n]) = (f(x[n+1]) - f(x[n]))/(x[n+1]- x[n])</strong><br />[<em>f'(x)</em> being the slope at point <em>x</em>] </li></ul><ul><li>Putting <strong>0</strong> for y coordinate at point <strong>x[n+1]</strong> where tangent crosses x-axis we get:<br /><strong>f'(x[n]) = (- f(x[n]))/(x[n+1]- x[n])</strong> </li></ul><ul><li>Rearranging the terms & putting <strong>f'(x) = 2x[n]</strong> we have our final expression:<br /><strong>x[n+1] = (x[n]+ N/x[n])/2</strong></li></ul><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1604846777283/IpOx2-UyD.png" alt /></p><blockquote><p><strong>Visualising Newton Raphson method for N = 10000</strong></p></blockquote><h2 id="notes">Notes</h2><p>[1] For the sake of simplicity, I have omitted the details about choosing the initial value which plays a crucial role in more complex functions.</p><p><em>Special thanks to <a target="_blank" href="https://amsi.org.au/">AMSI</a> for facilitating illustrations</em></p><hr /><blockquote><p>If you liked this post, you can find more by:</p><p>Following me on <strong>Twitter</strong>: <a target="_blank" href="https://twitter.com/abshekha">@abshekha</a></p><p>Connecting on <strong>LinkedIn</strong>: <a target="_blank" href="https://www.linkedin.com/in/abhimanyubitsgoa/">abhimanyubitsgoa</a></p><p>Visiting my <strong>website</strong>: <a target="_blank" href="https://abhimanyutimes.com">https://abhimanyutimes.com</a></p><hr /></blockquote><p>Thanks for reading!</p>]]>https://cdn.hashnode.com/res/hashnode/image/upload/v1604846880468/lad4Cr8Aw.jpeg