<?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 Saisankar Gochhayat on Medium]]></title>
        <description><![CDATA[Stories by Saisankar Gochhayat on Medium]]></description>
        <link>https://medium.com/@saisankargochhayat?source=rss-51318d4e67c8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Odng6YBKp9CTc_1p.</url>
            <title>Stories by Saisankar Gochhayat on Medium</title>
            <link>https://medium.com/@saisankargochhayat?source=rss-51318d4e67c8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 17:41:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saisankargochhayat/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[Dealing with mutable arguments in Unit Tests]]></title>
            <link>https://saisankargochhayat.medium.com/dealing-with-mutable-arguments-in-unit-tests-b0a847160b?source=rss-51318d4e67c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0a847160b</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[mutable-objects]]></category>
            <category><![CDATA[pytest]]></category>
            <category><![CDATA[unittest]]></category>
            <dc:creator><![CDATA[Saisankar Gochhayat]]></dc:creator>
            <pubDate>Thu, 16 Mar 2023 02:12:59 GMT</pubDate>
            <atom:updated>2023-03-16T02:12:59.753Z</atom:updated>
            <content:encoded><![CDATA[<p>If you pass a mutable argument and then try to unittest it with .assert_called_with(), you might run into problems. This issue can be confusing since it might seem like there is a bug with the testing framework (such as pytest), and many people have been led down that path.</p><p>After digging deeper, I found the following article that explains the problem: <a href="https://docs.python.org/3/library/unittest.mock-examples.html#coping-with-mutable-arguments">https://docs.python.org/3/library/unittest.mock-examples.html#coping-with-mutable-arguments</a></p><blockquote>Another rare but potential issue arises when a mock is called with mutable arguments. The call_args and call_args_list variables store references to the arguments. If the code under test mutates the arguments, it can be challenging to make assertions about their values when the mock was called.</blockquote><p>To address this issue, you can copy the arguments. This approach ensures that even if the arguments are mutated later on in the function, you can still safely and sensibly unit test them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0a847160b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Find the intersection of two lists in Python with Duplicates.]]></title>
            <link>https://saisankargochhayat.medium.com/find-the-intersection-of-two-lists-in-python-with-duplicates-9fc4e3b0fc8?source=rss-51318d4e67c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fc4e3b0fc8</guid>
            <category><![CDATA[intersections]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Saisankar Gochhayat]]></dc:creator>
            <pubDate>Sat, 31 Oct 2020 19:26:17 GMT</pubDate>
            <atom:updated>2020-10-31T19:26:17.411Z</atom:updated>
            <content:encoded><![CDATA[<pre>&gt;&gt;&gt; from collections import Counter<br>&gt;&gt;&gt; a = Counter([1,2,3,4,5])<br>&gt;&gt;&gt; b = Counter([1,3,5,6])<br>&gt;&gt;&gt; a &amp;= b<br>&gt;&gt;&gt; list(a.elements())<br>[1, 3, 5]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fc4e3b0fc8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 34]]></title>
            <link>https://saisankargochhayat.medium.com/leetcode-34-e967d3c67776?source=rss-51318d4e67c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/e967d3c67776</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Saisankar Gochhayat]]></dc:creator>
            <pubDate>Sat, 14 Mar 2020 23:29:38 GMT</pubDate>
            <atom:updated>2020-03-14T23:29:38.609Z</atom:updated>
            <content:encoded><![CDATA[<p>This is an interesting problem involving Binary search.<br>Here is the link to the problem — <a href="https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/">https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/</a></p><pre>class Solution:<br>    def searchRange(self, nums: List[int], target: int) -&gt; List[int]:<br>        gLow, gHigh = 0, 0<br>        low, high = 0, len(nums) - 1<br>        while low &lt;= high:<br>            mid = (low + high)//2<br>            #print(low, high, mid)<br>            if target &lt; nums[mid]:<br>                high = mid - 1<br>            else: <br>                low = mid + 1<br>        gHigh = high<br>        # re-initialized for finding low. <br>        low, high = 0, len(nums) - 1<br>        while low &lt;= high:<br>            mid = (low + high)//2<br>            # Narrow down till we find low.<br>            if target &lt;= nums[mid]:<br>                high = mid - 1<br>            else:<br>                low = mid + 1<br>        gLow = low<br>        return [gLow, gHigh] if gLow &lt;= gHigh else [-1, -1]</pre><p>The beauty lies in how we reduce the range with ≤ to find the lowest target value. And just the &lt; operator and find the highest target value.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e967d3c67776" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let's talk about Union Find.]]></title>
            <link>https://saisankargochhayat.medium.com/lets-talk-about-union-find-4ff09d6b0f50?source=rss-51318d4e67c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ff09d6b0f50</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[union-find]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Saisankar Gochhayat]]></dc:creator>
            <pubDate>Fri, 28 Feb 2020 20:58:40 GMT</pubDate>
            <atom:updated>2020-02-28T21:11:46.506Z</atom:updated>
            <content:encoded><![CDATA[<h4>Union Find is a very interesting data-structure that makes a lot of graph problems easy to solve.</h4><p><strong>Let’s say, you have a set of N elements that are partitioned into further subsets, and you have to keep track of connectivity of each element in a particular subset or connectivity of subsets with each other. To do this operation efficiently, you can use the Union-Find Data Structure.</strong></p><p>There is a naive version of this which can be done in O(N²) time complexity and O(N) space complexity. However, I am excited to jump into the cooler version, and we are going to more optimizations to reduce the union-find to O(log *N) time complexity. Which is an extremely slow-growing function i.e. log*n is log(log(……log(n))) (recursive log that many times till the number becomes 1 ). it’s an extremely slowly growing function you can notice if n = 2⁶⁵⁵³⁶ ,log*(n) = 5.</p><p>Two important optimizations to achieve this are -<br>1. Weighted Union — This is nothing but during the join of two graph sets, we join the smaller one to the bigger one.<br>2. Path Compression — Here we try bringing nodes up to the root node, to keep the tree flat.</p><p>Here is the Python implementation of the same —</p><pre># Goal is to implemented weighted union find with path compression.<br>class UnionFind:<br>    def __init__(self, N):<br>        # Stores the set it belongs to.<br>        self.arr = [i for i in range(N)]<br>        # Stores the size of set, needed for weighted union.<br>        self.size = [1 for i in range(N)]</pre><pre>def _root(self, i):<br>        # The idea is to set the root of i to its grandparent, hence compressing the path.<br>        while self.arr[i] != i:<br>            self.arr[i] = self.arr[self.arr[i]]<br>            i = self.arr[i]<br>        return i</pre><pre>def find(self, x):<br>        # find the parent set of the element.<br>        return self._root(x)</pre><pre>def union(self, x, y):<br>        # find the union of two dsijoint sets given.<br>        x_root = self._root(x)<br>        y_root = self._root(y)<br>        <br>        # this the weighted union step.<br>        if self.size[x_root] &lt; self.size[y_root]:<br>            # add x to y tree.<br>            self.arr[x_root] = self.arr[y_root]<br>            self.size[y_root] += self.size[x_root]<br>        else:<br>            # add y to x tree.<br>            self.arr[y_root] = self.arr[x_root]<br>            self.size[x_root] += self.size[y_root]</pre><pre>u = UnionFind(10)<br>connections = [(0, 1), (1, 2), (0, 9), (5, 6), (6, 4), (5, 9)]<br># union<br>for i, j in connections:<br>    u.union(i, j)</pre><pre># find<br>for i in range(10):<br>    print(&#39;item&#39;, i, &#39;-&gt; component&#39;, u.find(i))</pre><p>Gist — <a href="https://gist.github.com/saisankargochhayat/f381002f39a1263bdfa31b4e8899f332">https://gist.github.com/saisankargochhayat/f381002f39a1263bdfa31b4e8899f332</a></p><p>Here is a good reference video discussing the same —</p><p>References — <a href="https://www.youtube.com/watch?v=VnKtI_WF-Og">https://www.youtube.com/watch?v=VnKtI_WF-Og</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ff09d6b0f50" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 934 Shortest Bridge]]></title>
            <link>https://saisankargochhayat.medium.com/leetcode-934-shortest-bridge-fea1483b9c8f?source=rss-51318d4e67c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/fea1483b9c8f</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Saisankar Gochhayat]]></dc:creator>
            <pubDate>Thu, 10 Oct 2019 04:58:53 GMT</pubDate>
            <atom:updated>2019-10-10T04:58:53.861Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/shortest-bridge/">Shortest Bridge - LeetCode</a></p><p>Here is one of the solutions I understood, I am putting it here for future reference. I have added comments to make it easier to understand.</p><pre># Start with picking the first 1 you can find. <br># Pick that point and start dfs to all all the edges for that island. <br># Now start bfs on the elements and exit when we find the second island, and quit when next 1 is found.<br>class Solution:<br>    rows = 0<br>    col = 0<br>    A = 0<br>    def shortestBridge(self, A: List[List[int]]) -&gt; int:<br>        self.col = len(A[0])<br>        self.rows = len(A)<br>        self.A = A<br>        bfs = [] <br>        self.dfs(bfs, *self.first(self.rows, self.col))<br>        <br>        # Final bfs logic to start expanding from each of the nodes of the first island.<br>        count = 0<br>        while bfs:<br>            newBfsStack = []<br>            for i,j in bfs:<br>                for x,y in ((i-1, j),(i+1, j),(i, j-1),(i, j+1)):<br>                    if 0&lt;=x&lt;self.rows and 0&lt;=y&lt;self.col and self.A[x][y] == 1:<br>                        return count<br>                    elif 0&lt;=x&lt;self.rows and 0&lt;=y&lt;self.col and self.A[x][y] == 0:<br>                        self.A[x][y] = -1                     <br>                        newBfsStack.append((x,y))<br>            count += 1<br>            bfs = newBfsStack<br>        return count<br>         <br>                    <br>    # Returns the first 1 encoutnered in the matrix.<br>    def first(self, rows, col):<br>        for i in range(rows):<br>            for j in range(col):<br>                if self.A[i][j]:<br>                    return i,j<br>                <br>    # Adds all the 1 nodes in from the first coordinate to the bfs list. <br>    def dfs(self, bfs, i, j):<br>        bfs.append((i,j))<br>        if self.A[i][j] == 1:<br>            self.A[i][j] = -1<br>            for i,j in ((i-1, j),(i+1, j),(i, j-1),(i, j+1)):<br>                if 0&lt;=i&lt;self.rows and 0&lt;=j&lt;self.col and self.A[i][j] == 1:<br>                    self.dfs(bfs,i,j)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fea1483b9c8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Changing the swap size on Ubuntu 18.04.]]></title>
            <link>https://saisankargochhayat.medium.com/changing-the-swap-size-on-ubuntu-18-04-3e4e2033fec6?source=rss-51318d4e67c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e4e2033fec6</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[swap]]></category>
            <dc:creator><![CDATA[Saisankar Gochhayat]]></dc:creator>
            <pubDate>Wed, 28 Nov 2018 14:07:27 GMT</pubDate>
            <atom:updated>2018-11-28T14:07:27.900Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*JLiXA7zQhk00-lLbKlbe0g.png" /><figcaption>Swappiness</figcaption></figure><p>So one fine evening I find my budget laptop freezing up due to Chrome hogging up all the memory. Upon checking my RAM usage I figured that out of my 6.8GB of usable ram barely 500mb was left unused.</p><p>By default ubuntu had set up a 2GB swap space, I wanted to extend it to the CentOS recommended swap space, that is your current ram size+2GB. So if you have say 6GB of ram space, allocating an 8GB swap space would be a good idea.</p><p>Starting with Ubuntu 17.04, the swap partition was replaced by a swap file. The main advantage of the swap file is easy resizing.</p><p>So to extend the swap space, we would be using the following step —</p><ol><li>Check if you have swap space running and turn them off -</li></ol><pre>sudo swapoff -a</pre><p>2. Resize the swap</p><pre>sudo dd if=/dev/zero of=/swapfile bs=1G count=8</pre><p>if = input file<br>of = output file<br>bs = block size<br>count = multiplier of blocks</p><p>3. Make the file usable as swap</p><pre>sudo mkswap /swapfile</pre><p>4. Activate the swap file</p><pre>sudo swapon /swapfile</pre><p>5. Check the amount of swap available</p><pre>free -h</pre><p>That should give your swap space a boost.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e4e2033fec6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>