<?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 Shadow on Medium]]></title>
        <description><![CDATA[Stories by Shadow on Medium]]></description>
        <link>https://medium.com/@shadow_07?source=rss-fdf58ac949c9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LC-9q-TMebNXq0pAB0CnpA.jpeg</url>
            <title>Stories by Shadow on Medium</title>
            <link>https://medium.com/@shadow_07?source=rss-fdf58ac949c9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 16:00:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shadow_07/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[Customising oh-my-zsh in MacOS]]></title>
            <link>https://medium.com/crypticcrazeforcs/customising-oh-my-zsh-in-macos-6d4e24ef983?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d4e24ef983</guid>
            <category><![CDATA[powerlevel10k]]></category>
            <category><![CDATA[zsh]]></category>
            <category><![CDATA[oh-my-zsh]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Mon, 26 Oct 2020 11:32:50 GMT</pubDate>
            <atom:updated>2020-10-26T11:32:50.347Z</atom:updated>
            <content:encoded><![CDATA[<h3>Current View</h3><p>This is how my terminal looks as of 25th October, 2020</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DPAupxdIDo7lrrWJ3vhy1g.png" /><figcaption>current iterm configuration</figcaption></figure><p>I use <a href="https://www.iterm2.com/features.html">iTerm</a> whose features are listed in the link. You can download iTerm from the given link, and use it as the default terminal.</p><h3>Installing zsh</h3><p>zsh is the default shell I use. Install zsh using brew. Steps involved are:</p><ul><li>If brew is not installed, install it using:</li></ul><pre>/usr/bin/ruby -e &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&quot;</pre><ul><li>Now that brew is installed, install zsh using</li></ul><pre>brew install zsh</pre><h3>Setting up zsh as default shell</h3><p>Check thatzsh has been installed (check it using zsh --version, if this outputs the version of zsh, that means that installation is complete).</p><p>Now, change the default shell to zsh using the following steps:</p><ul><li>Check where zsh actually is. which zsh should output something like /usr/local/bin/zsh or /bin/zsh.</li><li>chsh is the command to change the shell. However:</li></ul><blockquote>chsh will accept the full pathname of any executable file on the system. However, it will issue a warning if the shell is not listed in the /etc/shells file.</blockquote><p>To do away with the above warning, add zsh to /etc/shells using:</p><pre>sudo sh -c &quot;echo $(which zsh) &gt;&gt; /etc/shells&quot;</pre><ul><li>Change the default shell to zsh using</li></ul><pre>sudo chsh -s $(which zsh)</pre><p>Now, your /etc/shells should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qPGhM-hhWCfnA_gHuBKu6A.png" /><figcaption>content of /etc/shells</figcaption></figure><p>Restart your terminal (some versions of Yosemite, you may need to restart the system as well) and zsh should be set as your default shell. You can check it using echo $SHELL.</p><h3>Oh-My-Zsh</h3><p><a href="https://github.com/ohmyzsh/ohmyzsh">Oh My Zsh</a> is an open source, community-driven framework for managing your <a href="https://www.zsh.org/">zsh</a> configuration.</p><h4>Installating oh-my-zsh</h4><p>The following command should install oh-my-zsh.</p><pre>sh -c &quot;$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)&quot;</pre><h4>Configuring oh-my-zsh</h4><p>Open the ~/.zshrc file, and you’ll see a lot of options there. You can go ahead and play with some of those options. To reflect the changes on the session, use the command source ~/.zshrc.</p><p>We’ll now add the plugins and themes required.</p><h3>Plugins and themes</h3><p><a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins">Here</a> you’ll find most of the plugins for oh-my-zsh.</p><p>I found the following plugins and themes to be quite useful:</p><ul><li><a href="https://github.com/zsh-users/zsh-autosuggestions">zsh-autosuggestions</a></li><li><a href="https://github.com/romkatv/powerlevel10k">powerlevel10k</a></li><li><a href="https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git">git</a></li><li><a href="https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/osx">osx</a></li><li><a href="https://github.com/zsh-users/zsh-syntax-highlighting">zsh-syntax-highlighting</a></li><li><a href="https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/thefuck">thefuck</a></li></ul><h3>Installing plugins</h3><h4>zsh-autosuggestions</h4><p>Run the following command to clone the zsh-autosuggestions library (the following is one line of code):</p><pre>git clone <a href="https://github.com/zsh-users/zsh-autosuggestions">https://github.com/zsh-users/zsh-autosuggestions</a> ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions</pre><p>Next add zsh-autosuggestions as plugins in ~/.zshrc file</p><pre>plugins=(zsh-autosuggestions)</pre><h4>zsh-syntax-highlighting</h4><p>Clone the zsh-syntax-highlighting library</p><pre>git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting</pre><p>Activate the plugin in ~/.zshrc file</p><pre>plugins=(zsh-syntax-highlighting) </pre><p>Now run source ~/.zshrc to check how the changes appear.</p><h3>Powerlevel10k</h3><p>This is the theme which we are going to use.</p><h4>Installation</h4><p>To install, simply clone:</p><pre>git clone https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k</pre><p>and add the theme in ~/.zshrc</p><pre>ZSH_THEME=&quot;powerlevel10k/powerlevel10k&quot;</pre><h4>Fonts</h4><p>Download these files:</p><ol><li><a href="https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Regular.ttf">MesloLGS NF Regular.ttf</a></li><li><a href="https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Bold.ttf">MesloLGS NF Bold.ttf</a></li><li><a href="https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Italic.ttf">MesloLGS NF Italic.ttf</a></li><li><a href="https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Bold%20Italic.ttf">MesloLGS NF Bold Italic.ttf</a></li></ol><p>Now double click on each file to install them. Once installed, Open <em>iTerm2 → Preferences → Profiles → Text</em> and set <em>Font</em> to MesloLGS NF.</p><h4>Configuration</h4><p>Simply run p10k configure and select the options you like.</p><p>Once this is done, you should see a really good terminal.</p><h4>Manually Configuring the colors and prompts</h4><p>Open the ~/.p10k.zsh file, and you’ll find options to edit the prompt elements:</p><pre>typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(</pre><pre>status                  # exit code of the last command</pre><pre>command_execution_time  # duration of the last command</pre><pre>....<br>....<br>....</pre><pre>battery               # internal battery</pre><pre># wifi                  # wifi speed</pre><pre># example               # example user-defined segment (see prompt_example function below)</pre><pre>)</pre><p>Uncomment the ones you want to keep.</p><p>The color options are present below that:</p><pre># Current directory background color.</pre><pre>typeset -g POWERLEVEL9K_DIR_BACKGROUND=24</pre><pre># Default current directory foreground color.</pre><pre>typeset -g POWERLEVEL9K_DIR_FOREGROUND=254</pre><pre># If directory is too long, shorten some of its segments to the shortest possible unique</pre><pre># prefix. The shortened directory can be tab-completed to the original.</pre><pre>typeset -g POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique</pre><pre># Replace removed segment suffixes with this symbol.</pre><pre>typeset -g POWERLEVEL9K_SHORTEN_DELIMITER=</pre><pre># Color of the shortened directory segments.</pre><pre>typeset -g POWERLEVEL9K_DIR_SHORTENED_FOREGROUND=250</pre><pre># Color of the anchor directory segments. Anchor segments are never shortened. The first</pre><pre># segment is always an anchor.</pre><pre>typeset -g POWERLEVEL9K_DIR_ANCHOR_FOREGROUND=255</pre><pre># Display anchor directory segments in bold.</pre><p>Since the colors are numbers, you’ll need a mapping of numbers to colors to display the color you want.</p><p>To display all the colors your terminal supports along with the number, type the following in iTerm:</p><pre>for i in {0..255}; do print -Pn &quot;%K{$i}  %k%F{$i}${(l:3::0:)i}%f &quot; ${${(M)$((i%6)):#3}:+$&#39;\n&#39;}; done</pre><p>That should print something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*Inm8MM8QommuRvr7dLuOvw.png" /><figcaption>numbers against the colors they represent</figcaption></figure><p>Choose the background and foreground colors you like.</p><p>Congratulations! Your terminal has all been setup.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d4e24ef983" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/customising-oh-my-zsh-in-macos-6d4e24ef983">Customising oh-my-zsh in MacOS</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Majority Element]]></title>
            <link>https://medium.com/crypticcrazeforcs/majority-element-afcbd4cd80e0?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/afcbd4cd80e0</guid>
            <category><![CDATA[majority-element]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Mon, 28 Sep 2020 10:13:44 GMT</pubDate>
            <atom:updated>2020-09-29T15:23:48.499Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose there is an election to be held, with the following criteria:<br>1. There are m candidates.<br>2. There are n voters.<br>3. A candidate wins if (s)he gets strictly &gt; 50% of the votes.</p><p>So, how do you decide if there is a winner? And if there exists one, how do you find him/her?</p><h3>Sorting</h3><p>We sort all the votes (in either increasing or decreasing order). Thereafter, we count all the votes for each candidate. If any candidate gets &gt; 50% of the vote, we declare him/her the winner.</p><p>The pseudo code looks like:</p><pre>sort(votes)</pre><pre>n = length(votes)<br>start_index = 0<br>end_index = 0</pre><pre>while (end_index &lt;= n):<br>    while (end_index&lt;n and votes[end_index]==votes[start_index]):<br>         end_index++</pre><pre>    votes_for_this_candidate = end_index-start_index<br>    if votes_for_this_candidate &gt; 50% of n:<br>        declare this_candidate as winner<br>        break<br>    else:<br>        continue</pre><p>The above takes O(n*log(n)) time. However, it runs in O(n) space (the recursion stack takes O(n) space. Thanks to <a href="https://medium.com/u/ab5427fbb355">Piyush Bansal</a> for pointing that out.</p><h3>Mapping</h3><p>Another way would be to create a map of each element and its count. The pseudo code would look like this:</p><pre>map M<br>n = number_of_votes</pre><pre>for vote in votes:<br>    if vote present in M.keys:<br>         M[vote]++<br>    else:<br>         insert vote into M with M[vote] = 1</pre><pre>for key in M:<br>    if M[key] &gt; 50% of n:<br>         declare key as winner</pre><p>This runs in O(n) time (insertions and updates in map). However, this takes O(m) space, where m is the number of candidates in the election.</p><h3>Boyer-Moore Majority Vote Algorithm</h3><p>This algorithm solves the majority problem in linear time and in constant space.</p><h4>Idea</h4><p>Suppose there exists a majority element. We break votes into two sets (mathematical sets), A and B such that A only contains the majority element. In that case, size of A will be greater than size of B (since majority element appears &gt; 50% times).</p><p>So it would be possible to map each element of B to a different element in A, and some elements in A would remain unmapped.</p><h4>Better explained with an example:</h4><p>Say that number of votes is 21, and majority element is ‘m’. Then, we can definitely say that m occurs &gt; 10 times.</p><p>So consider set A consisting only of m’s, and set B consisting of all other elements. Size of A ≥ 11. Size of B ≤ 10.</p><p>Hence, each element of B could be mapped to a different m, and we would still be left with some unmapped m .</p><h4>An O(1) space and O(n) time solution</h4><p>The above idea can be used as follows:</p><ol><li>Take an empty stack</li><li>For each vote, do the following:</li></ol><ul><li>If the stack is empty, add the vote to the stack.</li><li>If the top of stack is same as vote, add the vote to the stack.</li><li>If the top of the stack is different from vote, remove the top of the stack.</li></ul><p>The end can have two possibilities:</p><ul><li>The stack is empty → there is no majority element in the array</li><li>The stack is not-empty. If the stack is not empty, it is guaranteed that all elements in the stack will be the same (think why). In this case, the <em>element present in the stack is a </em><strong><em>candidate</em></strong><em> for majority element</em>.</li></ul><h4>Intuition</h4><p>The above algorithm can be thought of as making pairs of different elements (vote and top of the stack) and removing them. As we saw above, if there is a majority element, it wouldn’t be possible to map all it’s occurrences to a different element. Hence, <strong>the elements which are not present in the stack are definitely not majority elements.</strong></p><h3>Going from a Stack to a simple counter</h3><h4>Pseudo Code</h4><p>The pseudo code looks like:</p><pre>candidate_for_majority_element = None<br>count = 0</pre><pre><strong><em>#part-1 finding out the candidate</em></strong></pre><pre>for vote in votes:<br>    if count==0:  <em>#no candidate for majority element</em><br>        candidate_for_majority_element = vote<br>        count++<br>    else if vote == candidate_for_majority_element:<br>         count++<br>    else<br>         count--</pre><pre><strong><em>#part-2 finding if the candidate is indeed a majority element</em></strong></pre><pre>count = 0<br>for vote in votes:<br>    if vote == candidate_for_majority_element:<br>        count++</pre><pre>if count &gt; 50% of length(votes):<br>    return candidate_for_majority_element</pre><h3>Follow Up</h3><p>Given a list of votes, find all candidates who got &gt;33% of the votes. Again, the solution should run in O(n) time and in constant space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afcbd4cd80e0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/majority-element-afcbd4cd80e0">Majority Element</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python — Design Pattern for Classes -II]]></title>
            <link>https://medium.com/crypticcrazeforcs/python-design-pattern-for-classes-ii-6c1df5488d0a?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c1df5488d0a</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 13:35:25 GMT</pubDate>
            <atom:updated>2020-07-28T13:35:25.388Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python — Design Pattern for Classes -II</h3><p>Please read the <a href="https://medium.com/crypticcrazeforcs/python-design-pattern-for-classes-i-88182e2936ff">first part</a> as we’ll be using the previously derived results and build on the previous part’s code.</p><h3>A New Requirement</h3><p>Our customers wanted a new function, angle_to_grade which will convert a return the gradient given an angle. So we implement the following function in our class:</p><pre>def angle_to_grade(self, angle):<br>    return math.tan(math.radians(angle))*100</pre><p>Is there a problem with the above? Not as such. But suppose our client wants to calculate gradient for <em>30 degrees. </em>Need they have a Circle object for doing so? No, right. So this indicates that angle_to_grade should be a static function.</p><p>But if it’s a static function, why is it placed in a <strong>class</strong>? The reason is, to ease finding the function. Else, our clients won’t know where to look for. Hence the reason for static functions. Our code thus becomes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n7Bf88aEiKFmAbhsE3xKEQ.png" /><figcaption>version-0.5 with a static method</figcaption></figure><h3>A New Request</h3><p>One of our customers wants that area be calculated using perimeter. No other customer has an issue with this, so we go forward to implement it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qm2dL_MCi8akOQLe3AfPCA.png" /><figcaption>beta-version with new formula for area calculation</figcaption></figure><p>Is there an issue with this implementation? Think over it before moving on.</p><p>Let’s go back to the previous version 0.5, and create a Tire, and print its perimeter and area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*1uwmJK44d-2mG3Oz20jMzw.png" /><figcaption>Tires in version 0.5</figcaption></figure><p>Let’s see what happens to Tires in the new beta version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*VWrJ53ribrH7Gb-98dYf-A.png" /><figcaption>Tires in version 0.6b</figcaption></figure><p>We broke the code for tires. This is because self.perimeter() calls tire.perimeter() for Tires, and this is the adjusted perimeter. So we tweak our code in a way the self.perimeter() refers to Circle.perimeter() only.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TcFoXbU17A_f5Rxzew4gXg.png" /></figure><p>However, since this is a good idea for Circles, this is a good idea for Tires too. So they go and implement one for themselves.</p><pre>class Tire(Circle):<br>    def perimeter(self):<br>        return Circle.perimeter(self)*1.25<br>    _perimeter = perimeter</pre><p>It would be better if we had someway of clarifying who the perimeter call belongs to. It would become very clear if we had a prefix, saying Circle.perimeter or Tire.perimeter. Turns out, Python has a feature for this. That is by using the __ before a variable name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*70CvuESQkB2nbtMNcoprDg.png" /></figure><p>Internally, __perimeter would resolve to _Circle__perimeter. So if the Tire people implement it too, their __perimeter would resolve to _Tire__perimeter. And thus there remains no ambiguity.</p><p>So say Tire people change their code to the following:</p><pre>class Tire(Circle):<br>    def perimeter(self):<br>        return self.__perimeter()<br>    def __perimeter(self):<br>        return Circle.perimeter(self)*1.25</pre><p>As expected, the code works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k0O_J03oQOqOGe5dJAu_Dg.png" /></figure><h3>Fifth Customer</h3><p>Our fifth customer is a very important customer, and a very wealthy one too. They are offering to pay almost as much as all the four customers combined, but they have only one ask. <strong>We should not store the radius, but the diameter.</strong></p><p>We cannot simply do that, as all our previous code would fail. And we’ll lose all our previous customers. If only we had some kind of ‘<em>getters and setters</em>’, and we had that in our code, we would have been able to save ourselves.</p><p>Turns out, Python does have something for this. Let’s see the code first, the explanation will follow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4aebmRirSvQ6CIaYwyYBBg.png" /><figcaption>version-1.0 for all our customers</figcaption></figure><p>@property converts dotted access to method calls, i.e., c.radius will call the function radius in the Circle class. But, this means that self.radius should be a function call to radius, which returns the radius. How does then self.radius = radius work?</p><blockquote>self.radius = radius is resolved to calling the function with@radius.setter decorator.</blockquote><p>Hence, all places where we either used radius or assigned radius continue to work.</p><h3>The Final Touches</h3><p>Remember our first customer, who were calculating the average area of circles. Turns out, they want to find average of 10 million circles. So their use case is:</p><pre>n = 10000000<br>circles = [Circle(random()) for i in range(n)]</pre><p>Let’s see how much memory we are using.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z4YY3geKX15jFyhWNiGsGg.png" /></figure><p>Doesn’t it feel weird to use so much memory for something which essentially stores a list of numbers? <strong>This amount of memory is being used because there is a corresponding dictionary stored against every object.</strong></p><p>So how do we prevent that from happening. We declare slots.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qaT6Fwfc91Szfvw9_GXwlg.png" /><figcaption>version-2.0 (memory optimised with slots)</figcaption></figure><p>And the memory being used changes accordingly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcaTKzCWy3Jzxjoj_9LDNw.png" /></figure><p>Remember that this is the very last step in our class design.</p><p>But wait. Doesn’t this break the Tire class?</p><p>No. Because, if somebody inherits your code, the slots doesn’t inherit. Dictionary for each instance of Tire is still intact.</p><h3>Conclusion</h3><p>The following points should summarise stuff up:</p><ul><li>Inheriting from objects to get the functionalities like slots</li><li><strong>Instance Variables </strong>unique to the instance</li><li><strong>Class Variables </strong>common across classes</li><li><strong>Regular Methods </strong>need “self” to operate on instance data</li><li><strong>Class Methods </strong>implement alternate constructors — a ‘cls’ parameter is passed to them for creating subclasses instances as well</li><li><strong>Static Methods </strong>attach functions to classes — they neither need “self”, nor “cls”, and improve discoverability of the function</li><li><strong>Property() </strong>lets getter and setter methods be invoked automatically by attribute access</li><li>The <strong>__slots__ </strong>variable implements flyweight design patterns by suppressing instance dictionaries</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c1df5488d0a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/python-design-pattern-for-classes-ii-6c1df5488d0a">Python — Design Pattern for Classes -II</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python — Design Pattern for Classes -I]]></title>
            <link>https://medium.com/crypticcrazeforcs/python-design-pattern-for-classes-i-88182e2936ff?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/88182e2936ff</guid>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 12:17:55 GMT</pubDate>
            <atom:updated>2020-07-22T12:17:55.225Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python — Design Pattern for Classes -I</h3><p>An important thing when one moves from college to corporate is the need to understand others’ code, as well as to write code in a way that others find it easy to comprehend.</p><p>Another thing is that once a class is written, other developers might want to extend it to avoid effort duplication. However, many-a-times, we forget that the classes we are writing are not just for us, but for our children as well (ah, I like the puns).</p><p>Here goes an article based on a famous presentation by Raymond Hettinger.</p><h3>The Base Class</h3><p>We want to build a module that would provide some geometrical calculations on a Circle. The following is our basic design:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*unKPHcLCf1vVscSY6TQY_g.png" /><figcaption>Initial design</figcaption></figure><p>Despite being very basic, the above encapsulates some very important concepts:</p><ul><li>We inherit from <strong>object </strong>to get some functionalities. What functionalities exactly, would be discussed in the second part.</li><li>Contrary to popular opinion, <strong>__init__ </strong>is not a constructor. <strong>__init__ </strong>is called after an instance is already created, and is used to populate the existing instance.</li><li>Class is a namespace. Unlike cpp and Java, we can write print statements, open and close files inside class declarations, and these would run when the class is run. The following snapshot should make it more clear.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9oGXyVU6XzTs1BQyaLEhMg.png" /><figcaption>‘Hello’ is printed after the class declaration itself</figcaption></figure><ul><li>self refers to the instance calling the function. We may name self as this or s or anything we like, but again, we want to convey our ideas to others who use our code. And the general practice is to use self.</li></ul><h4>Some Minor Modifications?</h4><h4>π</h4><p>We are using 3.14 instead of π. A better idea would be to import math and use math.pi instead.</p><p>What if I specify π with a 32-bit precision and use it? That should be the same as ‘importing math’, right? No. Suppose your code is run on a 64-bit machine. Using math.pi will automatically take care to convert it π to 64-bit precision. Hence, if there’s a library, understand that people have spent time building and optimising it. So use it.</p><h4>version</h4><p>It’s a good practice to have versions. Since this is the very first try, let’s keep the version as 0.1. A couple of questions before we include that in our code:</p><ul><li>Is the version instance specific? → No, it’s the same across all instances. <br>So it makes sense to keep the version as a c<strong>lass variable.</strong></li><li>Which should we use, version = 0.1 or version = &#39;0.1&#39;? <br>Firstly, is version 1.0 + version 1.1 = version 2.1? No.<br>Secondly, it would have a very bad impact if someone printed the version, and it gave the result as 0.1000000000000000001. Makes the code look very bad. So, the better idea is to keep version as a <strong>string</strong>.</li></ul><p>With the above two modifications, here is our initial code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S0m7wc3MCGKnGPKhQOlobA.png" /><figcaption>Initial Design with Minor Modifications</figcaption></figure><p>Shouldn’t we include a function for perimeter? A function for area of inscribed equilateral triangle? Another for area of circumscribed equilateral triangle? NO.</p><p>Do not put in features which may not be helpful. Unless the client wants it, there’s no use implementing it and wasting the resources. It’s better to have less features which are more often used than bombarding the class with features which clients have a tough time figuring out.</p><blockquote>With the Minimum Viable Product in hand, we ship it.</blockquote><h3>First Customer</h3><p>Our first customers want to calculate average area of circles, so they do the following:</p><pre>from random import random<br>n = 10<br>circles = [Circle(random()) for i in range(10)]<br>area = [c.area() for c in circles]<br>print(sum(area)/len(area))</pre><p>Simple as this may be, this shows that our customer will create multiple instances of the class. And this is true for most use cases. But no worries now, our base class works good.</p><h3>Second Customer</h3><p>The second customer wants to include a function for perimeter as well. Now would be a good time to write a function for perimeter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q1FmLIKElzyq_sCm7KLfmA.png" /><figcaption>Circle with perimeter function</figcaption></figure><p>The same customer uses the class as follows:</p><pre>radii = [1, 2, 3]<br>circles = [Circle(r) for r in radii]<br>for c in circles:<br>    print(&#39;Area before expansion: &#39;, c.area())<br>    c.radius *= 1.1<br>    print(&#39;Area after expansion: &#39;, c.area())</pre><p>But wait a second. The customer changed the radius of the circle. This was an instance variable. Are we comfortable ‘exposing’ this?</p><blockquote>For Python, there’s nothing such as private and protected variables. Python leaves all doors unlocked.</blockquote><p>Writing c.set_radius(c.get_radius()*1.1) would be tiresome, right? People might be wondering wouldn’t exposing create problems? We’ll look into this later on in the article.</p><h3>Third Customer</h3><p>Our third customer is a <strong>Tire</strong> company, so they want to extend the <strong>Circle</strong> class. However, their demand is that the perimeter be multiplied by 1.25 to account for some adjustments.</p><p>So they implement the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jFjwKKewQQ5Xaw8L2D1iFg.png" /><figcaption>The ‘Tire’ class</figcaption></figure><p>This again brings forth an important point: <br><strong>Expect modifications to your functions in subclasses.</strong></p><h3>Fourth Customer</h3><p>The fourth customer wants to create a circle given a bounding box diagonal. A bounding box is a square box which encloses the circle, and touches it at diametrically opposite points.</p><p>Given the diagonal of a bounding box, and a function bbd_to_radius, our customer uses the following to create Circles:</p><p>c = Circle(bbd_to_radius(diagonal))</p><p>Doing the above every-time is tiresome, so a better way would be to provide an alternate constructor. So we modify our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kJF6g7HEg6AYztRlpNrbDA.png" /><figcaption>version-’0.3&#39; with an alternate constructor</figcaption></figure><p>Using the above code enables the following use case for customer 4:</p><pre>c = Circle.from_bbd(25.1)<br>print(c.perimeter())<br>print(c.area())</pre><h3>Something’s Not Right</h3><p>Yes, it would be good to ponder over what could be wrong before moving on. What breaks? Why? Think…</p><p>Our customer 4 ran the following, and got the desired output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t6mZyiHVGgAUAxdhr8yLwA.png" /><figcaption>Customer 4 use case with output</figcaption></figure><p>However, we have other customers too. Our Customer 3 (The Tire people) saw the new from_bbd function and tried to use it. So they did the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e76NJK3UvSdj6XACma3Vnw.png" /><figcaption>Tire declared using `from_bbd`</figcaption></figure><p>They got the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VGHNAiJ_I87IUU7YBcOsWw.png" /><figcaption>Output for the Tire class</figcaption></figure><p>Shouldn’t their perimeter be adjusted? All they wanted was that their perimeter be multiplied by 1.25, but it’s not happening. Why not? Think before reading on ahead.</p><blockquote>The reason is that we hardcoded from_bbd to return a Circle and forgot all about the subclasses which might inherit Circle.</blockquote><p>Remember that the <strong>class</strong> is always passed as the first parameter to @classmethod functions. Hence, we use that to our advantage, and modify the from_bbd function as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gIN903JvHaodS9W1qJC_2g.png" /><figcaption>modified from_bbd function</figcaption></figure><p>Now doing the same operation on the Tire class gives the corrected perimeter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g6emabNZRjytepZ1oxW-_w.png" /></figure><p>Our customers are satisfied now, but as popularity increases, so does use case, and so does number of customers. The next part will deal with some more customers, some more demands and trying to implement stuff without breaking the existing code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88182e2936ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/python-design-pattern-for-classes-i-88182e2936ff">Python — Design Pattern for Classes -I</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Step-by-Step Guide to Building Your Own Website Using Jekyll — II]]></title>
            <link>https://medium.com/crypticcrazeforcs/a-step-by-step-guide-to-building-your-own-website-using-jekyll-ii-1da6749dcc20?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/1da6749dcc20</guid>
            <category><![CDATA[website-development]]></category>
            <category><![CDATA[website]]></category>
            <category><![CDATA[jekyll-themes]]></category>
            <category><![CDATA[website-design]]></category>
            <category><![CDATA[jekyll]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Thu, 04 Jun 2020 15:03:44 GMT</pubDate>
            <atom:updated>2020-06-04T15:03:44.229Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Step-by-Step Guide to Building Your Own Website Using Jekyll — II</h3><p>Part I available <a href="https://medium.com/crypticcrazeforcs/a-step-by-step-guide-to-building-your-own-website-using-jekyll-i-201ecf2b3547">here</a>.</p><p>We’ll look at what happens when we build a website using Jekyll, what the folder structure of Jekyll is, and finally, how to take full control of your website</p><h3>Understanding what Jekyll does (in a nutshell)</h3><p>Whenever you run bundle exec jekyll serve, what happens is:</p><ul><li>Jekyll generates the corresponding .html files from the .md files.</li><li>It generates the corresponding .css files from the .scss files (these are located in the _sass folder).</li><li>It also resolves all the parameters (like {post.title},{relative_url}), accordingly rendering them.</li></ul><h3>Directory Structure of Jekyll</h3><p>The official website describing the Jekyll directory structure can be found <a href="https://jekyllrb.com/docs/structure/">here</a>. I’ll be discussing only the most relevant of them:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R_XZPvO4BLgRs7fDRgPvZg.png" /><figcaption>Directory structure of Jekyll</figcaption></figure><h4>index.html (index.md)</h4><p>This is the default page that will open up when someone visits your site.</p><h4>_sass</h4><p>Contains all the scss files (which are later converted to css files by Jekyll)</p><h4>_layouts</h4><p>Contains the different layouts. Suppose that you want your home page to have a different layout, and your posts to have a different layout. What you can do is create a post.html (or any .html, the name does not matter). Then while writing a new post, use this at the top of the .md file:</p><pre>---<br>layout: post<br>---</pre><p>Or, more specifically, if you created a mylayout.html in _layouts, and wanted to use that, just change layout: post to layout: mylayout.</p><h4>_posts</h4><p>This folder contains all your blogs. Please note that the name of the post should be in the format: YYYY-MM-DD-title_of_post.md.</p><h4>_config.yml</h4><p>This specifies most of the configuration data, like <strong>title, description, show_downloads, </strong>etc. (try changing show_downloads: true to show_downloads: false, and see what changes happen in the homepage).</p><h4>_site</h4><p>All the files generated by <strong>Jekyll </strong>are placed in this folder. This is the one which is used to render the website.</p><h4>assets</h4><p>Suppose you want to use some custom css or javascript files. Where to store them? Turns out, <strong>assets</strong> is the folder for that. Jekyll keeps this folder as it is in the _site folder. This folder is best used for storing images, fonts, custom css and js files.</p><h3>Taking Full Control of Your Website</h3><p>Once you are comfortable with the previous two topics (understanding Jekyll and its directory structure), you’re good to go customizing your website according to your requirements.</p><p>To change the default layout, play around with _layouts/default.html. To add a new div, create it in default.html, and change the style of the div either in assets/style.css, or in _sass/jekyll_theme_whatever_theme_you_use.scss, or create new customstyle.css in assets folder.</p><p>You’ll see the following line in your _layout/default.html :</p><pre>&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ &#39;/assets/css/style.css?v=&#39; | append: site.github.build_revision | relative_url }}&quot;&gt;</pre><p>relative_url is useful so that you can add relative links of your pages, and <strong>Jekyll </strong>will be able to build it both locally and on github-pages.</p><p>You can add any css or js files to the assets folder, and accordingly link them to your default.html or any post.md that you like.</p><h4>Hands on Experimenting — An example</h4><p>In this example, I’ll add a div called lefter (header: head :: lefter: left :p ). This is how my website is presently:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2XGcK-A0irdCyIxoq8HcJw.png" /><figcaption>The current view of my website</figcaption></figure><p>Add the following to the default.html:</p><pre>&lt;div id=&quot;lefter&quot;&gt;<br>      &lt;h1 class=&quot;project-title&quot;&gt;{{ site.title }}&lt;/h1&gt;<br>      &lt;h2 class=&quot;project-description&quot;&gt;{{ site.description }}&lt;/h2&gt;<br>&lt;/div&gt;</pre><p>Add the following to assets/style.scss:</p><pre>#lefter{<br> background-color: #171717;<br> top: 0px;<br> left: 0px;<br> position: absolute;<br> width: 200px;<br> height: 100%;<br> z-index: 200;</pre><pre>.project-title{<br>  background-color: white;<br> }<br> h1{<br>  color: red;<br> }<br> .project-description{<br>  background-color: white;<br> }<br> h2{<br>  color: red;<br> }<br>}</pre><p>Now, the website appears as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gzz4JnIr9M6J7AT36wdh1w.png" /><figcaption>See the `lefter` on the left side :p</figcaption></figure><p>You can similarly add js files to the assets, and add references to them in your html/md file.</p><p>That’s it. You have your personal website, with full control. Hardly takes a couple of days to setup. And once setup, all you need to focus on is blogs and their content. No more worrying about the layout.</p><blockquote>Happy Blogging.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1da6749dcc20" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/a-step-by-step-guide-to-building-your-own-website-using-jekyll-ii-1da6749dcc20">A Step-by-Step Guide to Building Your Own Website Using Jekyll — II</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Step-by-Step Guide to Building Your Own Website Using Jekyll — I]]></title>
            <link>https://medium.com/crypticcrazeforcs/a-step-by-step-guide-to-building-your-own-website-using-jekyll-i-201ecf2b3547?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/201ecf2b3547</guid>
            <category><![CDATA[website-design]]></category>
            <category><![CDATA[jekyll]]></category>
            <category><![CDATA[jekyll-themes]]></category>
            <category><![CDATA[website-development]]></category>
            <category><![CDATA[website]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Thu, 04 Jun 2020 15:00:54 GMT</pubDate>
            <atom:updated>2020-06-15T07:26:31.423Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Step-by-Step Guide to Building Your Own Website Using Jekyll — I</h3><p>I had often thought of building my own website. But since I had little knowledge of web-development and almost zero imagination in front end, I could never put that thought to execution.</p><p>However, <strong>Jekyll</strong> came to my rescue. Don’t worry if you don’t know the concepts of web-development. This guide will help you set up your own website and give you full control over it too, i.e.</p><blockquote>You can change the themes as per your need. If you don’t like a feature, you can remove it or modify it.</blockquote><p>The official documentation of Jekyll is available <a href="https://jekyllrb.com/">here</a>. But personally, I could not gather much from it, and had a tough time figuring out stuff.</p><h3>Outline</h3><p>We’ll setup a website on github. Since ‘github-pages’ come with inbuilt support for Jekyll, hosting your webpage on github becomes very easy.</p><h3>Requirements</h3><ul><li>A github account (don’t have one, sign up <a href="https://github.com/">here</a>)</li><li>Linux / Mac OS</li><li>Familiarity with git concepts like push, pull, clone and commit</li></ul><h3>Installation</h3><p>Let’s decide the theme first. <a href="https://pages.github.com/themes/">Here</a> is a list of Jekyll themes which are supported by github pages. Decide which theme you want to use. For this guide, I’ll be working with the <a href="https://pages-themes.github.io/cayman/">Cayman</a> theme.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dll-xFtgsgShisS1nrgCWg.png" /><figcaption>The Cayman Theme. Every theme will have a `View on Github` option.</figcaption></figure><p>Go to the theme which you like. Click on View on Github.</p><p>That should open a page like this. Either click on fork or Clone or download.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xy9qVNaHAybzu5a0Q5oEBw.png" /></figure><p>For this guide, I’ll be cloning the directory into my local. When you click on Clone, you’ll get something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*P50hFsY-_lH4H9NecgJxaw.png" /><figcaption>Copy the link (in this case <a href="https://github.com/pages-themes/cayman.git">https://github.com/pages-themes/cayman.git</a>)</figcaption></figure><p>Open the terminal (on either Mac or Ubuntu), cd to the folder you want to save the code in, and run the following:</p><pre>$ git clone whatever_you_copied</pre><p>So supposing you wanted the code in your Documents folder, and you were using the Cayman theme, run:</p><pre>$ cd Documents<br>$ git clone <a href="https://github.com/pages-themes/cayman.git">https://github.com/pages-themes/cayman.git</a></pre><p>The above clones the repository to your local. Now you’ll find a folder with the theme name (for those using Cayman, the folder name would be Cayman).</p><p>Next do the following in your terminal:</p><pre>$ cd Cayman ##replace this with your theme name<br>$ script/bootstrap</pre><h3>Viewing what you installed</h3><p>Next run the following command:</p><pre>$ bundle exec jekyll serve</pre><p>If everything goes well, you should see the following on your terminal:</p><pre>Server address: <a href="http://127.0.0.1:4000/">http://127.0.0.1:4000</a><br>  Server running... press ctrl-c to stop.</pre><p>Open any browser of your choice, and go to either of the following links:</p><pre><a href="http://localhost:4000/">http://localhost:4000</a><br><a href="http://127.0.0.1:4000/">http://127.0.0.1:4000</a></pre><p>You should see a webpage corresponding to your theme.</p><h3>Changing what you view</h3><p>Now we come to the part where we take control over our website.</p><h4>Step 1 (Changing the title) :</h4><p>Navigate to the theme folder (in our case, Documents/Cayman), and open the file _config.yml on a text-editor of your choice. The contents would look like this:</p><pre>title: Cayman theme<br>description: Cayman is a clean, responsive theme for GitHub Pages.<br>show_downloads: true<br>google_analytics:<br>theme: jekyll-theme-cayman</pre><p>Supply an appropriate title and description. For example:</p><pre>title: Shadow<br>description: Apparently this user prefers to keep an air of mystery<br>show_downloads: true<br>google_analytics:<br>theme: jekyll-theme-cayman</pre><p>And the webpage changes accordingly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GthPeOp6y5sjltEDeusl2g.png" /></figure><blockquote>Note that you may need to perform a hard refresh for the changes to reappear, as the browser may use cache to render the webpage. So press CTRL+SHIFT+R (or on Mac, do a CMD+SHIFT+R).</blockquote><h4>Step 2 (Changing the Content) :</h4><p>Navigate to the theme folder (in our case, Documents/Cayman), and open index.md on a text-editor of your choice.</p><p>Notice how the contents on index.md are rendered on your webpage. This is the beauty of <strong>Jekyll</strong>. It automatically generates html file from the md file, and thus our work is reduced to a mere writing of plaintext.</p><p>Play around with index.md, and notice how the webpage changes. Once you’re done with that, your basic webpage has been set up.</p><h4>Step 3 (Start Blogging):</h4><p>The primary use case of Jekyll is to generate static sites without worrying about HTML and CSS specifications. So, it finds great use for bloggers, where once the initial set up is done, you can start blogging — focusing only on the content of the blog and not having to worry about the layout.</p><p>Blogs go under the _posts folder. Navigate to the Documents/theme folder, and create a _posts folder.</p><p>You can create blogs in the _posts folder, but note that the names of blog files follow a prescribed format. The name of blogs should be in the format:</p><blockquote>YYYY-MM-DD-Title_of_blog.md</blockquote><p>Create a file named 2020-06-03-first-blog.md. Put the following inside the file:</p><pre>---<br>layout: default<br>description: This is my first blog<br>---</pre><pre># This should be the top heading</pre><pre>## This should be a subheading</pre><p>The first blog is ready. You can see it on:</p><p><a href="http://localhost:4000/2020/05/27/first-blog.html">http://localhost:4000/2020/06/03/first-blog.html</a>.</p><p>It will appear as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vnco6dnztMOME5Uz-5Losg.png" /></figure><p>Notice how the title (<strong>First Blog</strong>) is picked up from the file name itself, and the description (<strong>This is my first blog</strong>) is picked up from description: This is my first blog .</p><p>Now you can create your blogs as well. So most of the work is done. The only thing which remains is to add links to these blogs on your home page, so that others can reach your blogs from the home page itself.</p><h3>Listing Your Blogs</h3><p>Wouldn’t it be great if you had a page, on which all the blogs were automatically listed. Turns out, we can have this functionality using Jekyll.</p><p>Let us design a page for listing our blogs. Create a folder named blog on the Documents/Cayman (or wherever your theme is). Create a file called index.md in it. Put the following into index.md :</p><pre>---<br>layout: default<br>---</pre><pre># Blogs</pre><pre>{% for post in site.posts %}<br> <br>&lt;ul&gt;<br> <br>&lt;li&gt;&lt;h3&gt;&lt;a href=&quot;{{ post.url | relative_url }}&quot;&gt;{{ post.title }}&lt;/a&gt;&lt;/h3&gt;&lt;/li&gt;<br> <br>&lt;/ul&gt;</pre><pre>{% endfor %}</pre><p>Now go to the site: localhost:4000/blog , and you’ll be able to see your blogs listed there. You might see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kqrNVaRbXAroz_eme_rwmw.png" /></figure><p>This is the power of <strong>Jekyll</strong>. You needn’t worry about listing your blogs on the page. All you have to do is create blogs, and <strong>Jekyll</strong> would take care of the rest.</p><h4>Explanation of what the code means</h4><p>The first part tells which <strong>layout</strong> to use to render the page. layout:default means use the default layout to render the page. If you want to see how the default layout is configured, look at the default.html file in _layout folder.</p><p>You can change the default.html file, or add another html file in _layout and use that to render some page. More on this to follow later.</p><p>Another thing to note is the index.md file in blog folder. Try and understand how HTML syntax works with the markdown syntax. Notice that # is not an HTML tag, but is converted to a header tag internally by <strong>Jekyll.</strong></p><p>However, &lt;ul&gt; , &lt;li&gt; , &lt;a&gt; are all HTML tags and can be used as are in the md file.</p><p>The following is <strong>Jekyll </strong>syntax:<br> {% for post in site.posts %}.</p><p>This is basically a <strong>for-loop</strong> over all the posts in the site. Note that {{ post.title }} and {{ post.url }} are internally compiled by <strong>Jekyll </strong>to their respective values.</p><h3>Adding a link to blog homepage</h3><p>Go to your index.md file in theme folder (in our case, Documents/cayman). Add the following line after the layout specification (or something similar — start controlling your website now):</p><p>You can find my blogs [here](./blog/)</p><p>Now go to localhost:4000, and you should see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2XGcK-A0irdCyIxoq8HcJw.png" /><figcaption>Notice the top line below the title and description. Note how the word `here` is highlighted</figcaption></figure><p>If you click on here, you’ll be redirected to the blogs page, where all your blogs are listed.</p><h3>Going Live</h3><p>With the repo in place, you can now push all this code to your github account (create an appropriate repository, like shadow.github.io).</p><p>Please go through <a href="https://help.github.com/en/github/working-with-github-pages/configuring-a-publishing-source-for-your-github-pages-site">this link</a> to configure the github-pages publishing source for your repository.</p><p>If everything goes well, your site should be live at: https://repo_name.github.io, or in my case, https://shadow.github.io.</p><h3>Taking Full Control</h3><p>The <a href="https://medium.com/crypticcrazeforcs/a-step-by-step-guide-to-building-your-own-website-using-jekyll-ii-1da6749dcc20">part 2</a> of this blog assumes a working knowledge of HTML, CSS and JavaScript. You may ignore this if you are not familiar with the above, or if you are content with the website you have.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=201ecf2b3547" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/a-step-by-step-guide-to-building-your-own-website-using-jekyll-i-201ecf2b3547">A Step-by-Step Guide to Building Your Own Website Using Jekyll — I</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Random Point in a Circle]]></title>
            <link>https://medium.com/discrete-love-for-maths/random-point-in-a-circle-45559d3e9665?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/45559d3e9665</guid>
            <category><![CDATA[randomness]]></category>
            <category><![CDATA[probability]]></category>
            <category><![CDATA[circle]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Wed, 13 May 2020 15:50:17 GMT</pubDate>
            <atom:updated>2020-05-13T15:50:17.253Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose you’re given the centre of the circle C (xc,yc). You are also given its radius R. How do you return a random point in a circle (each point in the circle should have the same probability of being returned)?</p><h4>Tools/Functions Provided</h4><p>Assume that a function f is provided which returns a random value between 0 and 1.</p><h3>Approach</h3><p>It is difficult to think in terms of cartesian coordinates, because the range of x is dependent on y and vice versa. So we think in terms of polar coordinates.</p><p>First, we choose a random distance r from the centre C. That is returned by f*R.</p><p>Now, we choose a random angle θ. This is returned by f*2π.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*iiBchLWyAecgYisLeZqytw.gif" /><figcaption>0≤r≤R, 0≤θ≤2*π</figcaption></figure><p>So, the random point (x,y) is given by (xc+r*cos(θ), yc+r*sin(θ)).</p><h3>Problems?</h3><p>The above approach seems good to go. However, does there seem to be a problem with it?</p><p>Suppose that distance r can take only two values, R/3 and 2*R/3. So what is the probability that the point is selected from the outer circle (with radius 2*R/3) ? It’s exactly 1/2, because distance is randomly chosen from the two values with equal probability. But this doesn’t seem right, does it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*Fe0ES1EItaSEM-jf2yDL1A.png" /><figcaption>Inner circle has radius R/3, while outer circle has radius 2*R/3.</figcaption></figure><p>If the outer circle has twice the radius, it also has twice the circumference. So, probability of selecting a point from the outer circle(r = 2*R/3) should be double than that of selecting a point from the inner circle(r = R/3).</p><p>This shows that the probability distribution function should not be constant with distance, but should be directly proportional to distance.</p><h4>The New PDF</h4><p>So, our new probability density function is proportional to distance. Let’s call this probability density function p(r). Then we have:</p><blockquote>p(r) = k*r<br>∫k*r*dr = 1<br>k = 2/R²</blockquote><p>So, our pdf becomes 2*r/R², where r is the radial distance of the point. Our cumulative distribution function now becomes ∫p(r)*dr = r²/R².</p><p>Using <strong>sqrt(f)*r</strong> gives a point at distance x with probability 2*x/R². The proof of this will be covered in the next blog.</p><p>Thus, the final desired generation would be:</p><blockquote>θ = 2*π*f<br>r = sqrt(f)*R<br>x = xc + r*cos(θ)<br>y = yc + r*sin(θ)</blockquote><p>Next article coming soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45559d3e9665" width="1" height="1" alt=""><hr><p><a href="https://medium.com/discrete-love-for-maths/random-point-in-a-circle-45559d3e9665">Random Point in a Circle</a> was originally published in <a href="https://medium.com/discrete-love-for-maths">Discrete Love For Maths</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[So You Think You Know Your Spaces?]]></title>
            <link>https://medium.com/discrete-love-for-maths/so-you-think-you-know-your-spaces-56b7f98159d3?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/56b7f98159d3</guid>
            <category><![CDATA[probability]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Mon, 11 May 2020 13:05:57 GMT</pubDate>
            <atom:updated>2020-05-11T13:05:57.418Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Question</h3><p>Let C be a circle, with an equilateral triangle inscribed into it. If we select a chord at random, what is the probability that length of the chord is greater than the side of the equilateral triangle? An image is added for illustration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*2XrLiqhyvT0Fv8CLiAKS5A.png" /><figcaption>A circle with an inscribed equilateral triangle</figcaption></figure><h3>The Answer(s)?</h3><p>Firstly, note that length of one side of equilateral triangle = r√3.</p><h4>Method 1</h4><p>Suppose we select a point M in the circle. There is only one chord that has M as its midpoint.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*DCQ8dEmTyteOyD57GCZPNg.png" /><figcaption>chord AB with M as midpoint</figcaption></figure><p>Proof: Given a point M. If M is the midpoint of some chord, then CM will be the perpendicular bisector of the chord. So there is a unique chord perpendicular to CM and passing through M.</p><p>Now, we need to find when AB &gt; r√3.</p><blockquote><em>(CM)² = r²- ((AB)²/4). Also, AB &gt; r√3.<br>⇒ (CM)² &lt; r²/4<br>⇒ M &lt; r/2</em></blockquote><p>So, M may lie anywhere within a circle of radius r/2. Hence, probability of choosing such a point is 1/4 <em>(π*(r/2)² / π*r²).</em></p><blockquote>So, probability of choosing a chord whose length is greater than the side of an inscribed equilateral triangle = 1/4.</blockquote><h4>Method 2</h4><p>Suppose we fix one of the end points of the chord. Suppose that this end point coincides with one of the vertices. Then we would have something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*cx_vyxwlm441dad2ZgwIKw.png" /><figcaption>Fixing one end point of the chord on one of the vertices</figcaption></figure><p>The probability of the length of the chord being greater than the side of the triangle would be the same as probability of picking the second end point in the green region. The green region has length half as that of the red region.</p><blockquote>So, probability of choosing a chord whose length is greater than the side of an inscribed equilateral triangle = 1/3.</blockquote><h4>Method 3</h4><p>Consider a radius bisecting one of the sides of the triangle. Consider a point on the radius, and a chord passing through that point and perpendicular to the radius.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*ZdLSJfs5ZET7lPukjkhsQg.png" /><figcaption>Radius bisecting one of the sides of the inscribed triangle</figcaption></figure><p>Our chord will be greater than the side of the triangle if the point is chosen on the green part of the radius.</p><p>Length of the green part = r/2.</p><blockquote>So, probability of choosing a chord whose length is greater than the side of an inscribed equilateral triangle = 1/2.</blockquote><h3>Which Answer is Correct</h3><p>And the answer is: <strong>All of them</strong>.</p><p>The above question is famous as Bertrand’s Paradox.</p><blockquote>And so we get 3 perfectly correct but different answers to the question. This example illustrates the very important fact that there may be equally valid but different ways of defining ‘<strong><em>randomness’. </em></strong>In the same situations it is not at all clear which is the “truly random<strong>”</strong> method.</blockquote><p>So, do you know your underlying spaces?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56b7f98159d3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/discrete-love-for-maths/so-you-think-you-know-your-spaces-56b7f98159d3">So You Think You Know Your Spaces?</a> was originally published in <a href="https://medium.com/discrete-love-for-maths">Discrete Love For Maths</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[VPN, Proxies and Hotspot]]></title>
            <link>https://medium.com/crypticcrazeforcs/vpn-proxies-and-hotspot-9a07ae1a6769?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a07ae1a6769</guid>
            <category><![CDATA[proxy]]></category>
            <category><![CDATA[vpn]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[tethering]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Sat, 02 May 2020 16:40:15 GMT</pubDate>
            <atom:updated>2020-05-04T07:21:33.532Z</atom:updated>
            <content:encoded><![CDATA[<p>I had always wondered how proxies and VPNs worked behind the hood, but never really looked into them. With the WorkFromHome going on and the constant need to use hotspots, and VPNs over them, I got quite intrigued by the two working together. So I read about them, and here go my findings.</p><h3>VPNs</h3><p>Suppose a company has two offices, distantly located. Let’s call the locations, location A and location B. How do they share data b/w them? One way would be to lay wires connecting both these offices. This would be an effective, but costly method. And suppose the company is expanding, this would pose challenges to add a new data centre (would have to lay down wires to the new data centre too).</p><p>Another way would be to use the Internet to connect the two offices. But of course, the company’s data is sensitive. And one wouldn’t want sniffers and attackers to get a hand on the data. So a solution is proposed. Gateways are installed at locations A and B. These gateways are connected to both the local network and the Internet. When the gateway at A receives a data packet to be sent to B, it first encrypts the data, and sends it to the gateway at B. The gateway at B decrypts the data, and passes it on to the local network there.</p><p>With regards to other data packets (pertaining to the Internet and not directed from A to B or vice versa), they are handled like at any other gateway — forwarded to the corresponding server, and then the gateway receives the desired data. No encryption involved. The above is an example of <strong>Site to Site VPN.</strong></p><p>This also inspired <strong>Remote Access VPNs, </strong>where a piece of software called <strong>VPN client</strong> is run on the client machine (like the machine of an employee). This software encrypts all the requests, and forwards them to the gateway at the company. Now, the gateway decrypts them, and takes the corresponding action (either fetch the data from the local network, encrypt it, and send it back to the client machine, or fetch the data from the Internet, encrypt it, and send it back to the client machine, or respond back saying the client is trying to access a blocked site).</p><h3>Proxies</h3><p>In the literal sense, proxy means <em>pretending to be someone else</em>. The same is true for computer proxies as well. A proxy is a middle-man b/w the client and the server. When a request is made from a client, it first goes to the proxy server. The proxy server then forwards the request <em>on behalf of the client</em>, effectively hiding the ip address of the client.</p><p>After receiving the request from the client, the proxy server may forward the request to the desired location and then forward the response to the local machine, or may immediately respond back to the client saying that the client is trying to access a blocked site.</p><p>A proxy is far less secure than a VPN. Firstly, in most cases, there’s no encryption of data from the local machine to the proxy server, so sniffing is possible. Also, the proxy server might be compromised, and usernames and passwords might be leaked, if sent in plaintext.</p><p>An example of working with proxy is to use <a href="https://translate.google.com/">Google Translate</a>. A website being blocked for a client entails requests to the server hosting that website being blocked. Suppose website <strong>w </strong>is blocked. However, requests to google servers are allowed. So a workaround could be to ask google to translate the response on request to the website <strong>w.</strong></p><p>The above can be achieved by adding the website link after <a href="http://translate.google.com/translate?sl=ja&amp;tl=en&amp;u=">http://translate.google.com/translate?sl=ja&amp;tl=en&amp;u=</a>. If <strong>w </strong>is <a href="https://www.facebook.com/">https://www.facebook.com</a>, then the above request would look like <a href="http://translate.google.com/translate?sl=ja&amp;tl=en&amp;u=">http://translate.google.com/translate?sl=ja&amp;tl=en&amp;u=</a><a href="https://www.facebook.com/">https://www.facebook.com</a>.</p><blockquote>The above request essentially means asking google to receive the response from <a href="https://www.facebook.com/">https://www.facebook.com</a> assuming the source language is Japanese, and translate it to English. Effectively, we are using Google Translate as a proxy server.</blockquote><h3>Hotspots</h3><p>Tethering is basically using a mobile device as a modem to connect to another device. When hotspot is enabled on a mobile device, it serves as a miniature router, forwarding all requests to the desired servers, and forwarding all responses to the desired clients.</p><h3>VPNs with Hotspots</h3><blockquote>If a laptop is connected to a smartphone hotspot, what happens when VPN client is installed on smartphone only or laptop only?</blockquote><h4>VPN client on laptop only</h4><p>Any requests coming from the laptop will be encrypted. And the mobile device would act as any other router would — forward those requests to the desired server (which in our case is the gateway). Then the gateway will do whatever was described in <a href="#b780">VPNs</a>, and send the encrypted response to the mobile device. The mobile device would forward the encrypted response to the laptop, and then the VPN client on the laptop would decrypt it.</p><h4>VPN client on smartphone only</h4><p>This was the thing that most intrigued me. I connected my laptop to my mobile device and switched on the VPN client. The target location, <strong>t, </strong>was set to a location different from my current location, <strong>c</strong>. Now, when I opened the website <a href="https://whatismyipaddress.com/">what’s my ip</a>, I could see the location as <strong>t</strong>. All good.</p><p>However, when I opened the same link on my laptop, I could see <strong>c. </strong>I was completely confused. What I had assumed was that my device would receive requests from the laptop, and then the VPN client would encrypt the request and forward it. But apparently this was not happening.</p><p>So after some digging around, I found the following explanation.</p><blockquote>Android doesn’t redirect hotspot traffic through VPN network.</blockquote><p>The reason given was that the VPN service API redirects traffic to VPN’s interface using the UIDs of apps. While tethering works in native code, <em>dnsmasq </em>is the DNS server, while the rest of the traffic is handled by the Kernel directly.</p><p>So the way around this would be to modify the <em>routing table</em>. This would require adding an entry in the routing table database to redirect all traffic coming to the hotspot to the interface used by the VPN app, and then add an entry for the reverse traffic too, i.e., send traffic from VPN to the connected devices.</p><h4>Sources:</h4><ol><li><a href="https://android.stackexchange.com/questions/216033/how-to-share-vpn-connection-with-devices-on-hotspot?newreg=c0fe1c68dee04c8ab8849c26c020ad87">https://android.stackexchange.com/questions/216033/how-to-share-vpn-connection-with-devices-on-hotspot?newreg=c0fe1c68dee04c8ab8849c26c020ad87</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a07ae1a6769" width="1" height="1" alt=""><hr><p><a href="https://medium.com/crypticcrazeforcs/vpn-proxies-and-hotspot-9a07ae1a6769">VPN, Proxies and Hotspot</a> was originally published in <a href="https://medium.com/crypticcrazeforcs">CrypticCrazeForCS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Some P(h)unny One Liners]]></title>
            <link>https://medium.com/@shadow_07/some-p-h-unny-one-liners-5812d1dcc12a?source=rss-fdf58ac949c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/5812d1dcc12a</guid>
            <category><![CDATA[puns]]></category>
            <dc:creator><![CDATA[Shadow]]></dc:creator>
            <pubDate>Tue, 28 Apr 2020 13:48:10 GMT</pubDate>
            <atom:updated>2020-04-28T13:48:10.433Z</atom:updated>
            <content:encoded><![CDATA[<h3>(Title’s original, content — I’m afraid not)</h3><ul><li>Most people are shocked when they find out how incompetent I am as an electrician.</li><li>The first computer dates back to Adam and Eve. It was an Apple with limited memory, just one byte. And then everything crashed.</li><li>Q: What do you call the security outside of a Samsung Store? A: Guardians of the Galaxy.</li><li>I named my hard drive “dat ass” so once a month my computer asks if I want to ‘back dat ass up’.</li><li>Can’t see an end. I have no control and I don’t think there’s an escape. I don’t even have a home anymore. Think it’s time for a new keyboard.</li><li>A Mexican magician was doing a magic trick. He said, Uno, Dose, and he disappeared without a trace.</li><li>Atheism is a non-prophet organisation.</li><li>To the mathematicians who thought of the idea of zero, thanks for nothing!</li><li>I’m changing my name to ‘Benefits’ on Facebook. Next time someone adds me, It will say “you are now friends with Benefits.”</li><li>I saw an ad for burial plots, and thought to myself this is the last thing I need.</li><li>I’m glad I know sign language, it’s pretty handy.</li><li>I’m emotionally constipated. I haven’t given a shit in days.</li><li>Claustrophobic people are more productive thinking out of the box.</li><li>When I get naked in the bathroom, the shower usually gets turned on.</li><li>I couldn’t quite remember how to throw a boomerang, but eventually, it came back to me.</li><li>My wife likes it when I blow air on her when she’s hot, but honestly… I’m not a fan.</li><li>Cleaning mirrors is a job I could really see myself doing.</li><li>My math teacher called me average. How mean!</li><li>A book just fell on my head. I’ve only got myshelf to blame.</li><li>I don’t engage in mental combat with the unarmed.</li><li>Is your ass jealous of the amount of shit that just came out of your mouth?</li><li>As a wizard, I enjoy turning objects into a glass. Just wanted to make that clear.</li><li>I relish the fact that you’ve mustard the strength to ketchup to me.</li><li>Why don’t aliens visit our planet? Terrible ratings. One star.</li><li>What’s the difference b/w ignorance and apathy? Don’t know and don’t care.</li><li>Justice is a dish best served cold because if it were served warm, it would be just water.</li><li>Sign at the Urologist’s office: URINE good hands.</li><li>Having sex in an elevator is wrong on so many levels.</li><li>How can you spot the blind guy at the nudist colony? It’s not hard.</li><li>When I was young, I always felt like a male trapped in a females body. Then I was born.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5812d1dcc12a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>