<?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 swagatshinde on Medium]]></title>
        <description><![CDATA[Stories by swagatshinde on Medium]]></description>
        <link>https://medium.com/@htcswagatshinde?source=rss-3fea1e749bec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9lb4YhpSNPUrNuBEb7sdKg.jpeg</url>
            <title>Stories by swagatshinde on Medium</title>
            <link>https://medium.com/@htcswagatshinde?source=rss-3fea1e749bec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:56:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@htcswagatshinde/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[Time vs Space Complexity: Which Matters More?]]></title>
            <link>https://medium.com/@htcswagatshinde/time-and-space-complexity-282311321f51?source=rss-3fea1e749bec------2</link>
            <guid isPermaLink="false">https://medium.com/p/282311321f51</guid>
            <dc:creator><![CDATA[swagatshinde]]></dc:creator>
            <pubDate>Wed, 19 Aug 2026 16:48:50 GMT</pubDate>
            <atom:updated>2026-08-19T17:03:17.616Z</atom:updated>
            <content:encoded><![CDATA[<p>Time and Space Complexity measure the efficiency of an algorithm by analyzing how execution time and memory usage grow as the input size (n) increases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WVj2eWbc_oKsH7U-aNdcXQ.png" /></figure><p><strong>Key Notations (Asymptotics)</strong><br><strong>Big-O (O):</strong> Upper bound. Represents the worst-case scenario.<br><strong>Big-Omega (Ω)</strong>: Lower bound. Represents the best-case scenario.<br><strong>Big-Theta (Θ)</strong>: Tight bound. Represents the average or exact rate of growth.</p><p><strong>Time Complexity vs Space Complexity</strong><br>Time Complexity measures the total time an algorithm takes to run as a function of the input size (n). It focuses on computational speed and operation count.<br>Space Complexity measures the total memory (RAM) an algorithm consumes during execution, including both input space and auxiliary (temporary) space.</p><p><strong>Why Time Complexity Usually Wins</strong><br><strong><em>Hardware Economics</em>:</strong> RAM is relatively cheap, scalable, and abundant. Processing power (CPU cycles) and user time, however, are limited resource bottlenecks.<br><strong><em>User Experience (Latency):</em></strong> Users notice slow performance immediately. An algorithm with a poor time complexity like O(n²) instead of O(n log n) can cause a web app or API to freeze, time out, or feel sluggish.<br><strong><em>The Time-Space Trade-off:</em></strong> Developers intentionally sacrifice extra memory to make programs run faster. Caching, Hash Tables O(1) lookup using O(n) memory, and Dynamic Programming Memoization are all based on buying time with memory.</p><p><strong>Real-World Example: Finding a Name in a 1,000-Page Phonebook</strong><br><strong>Time Complexity:</strong><br>Flipping page-by-page from the start takes up to 1,000 steps O(n)- slow.<br>Opening to the middle and cutting the remaining pages in half each time takes at most 10 steps O(log n) — fast.<br><strong>Space Complexity:</strong><br>Remembering the page number in your head uses negligible memory O(1)space.<br>Writing down every name on a separate notepad to organize them uses extra memory O(n) space.</p><p><strong>Applications of Time Complexity</strong><br>-System Design &amp; Scalability<br>-Choosing Data Structures<br>-Cost &amp; Hardware Optimization<br>-Real-Time Systems<br>-Performance Bottleneck Analysis<br>-Selecting Optimal Algorithms</p><p><strong>Conclusion:</strong><br>Time Complexity is the single most critical tool for building fast, scalable software.It helps developers pick the right algorithms, avoid sluggish performance, and keep server costs down as user traffic grows. While memory limits matter, optimizing execution time is what keeps modern applications responsive and reliable</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=282311321f51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Recursion in Python?]]></title>
            <link>https://medium.com/@htcswagatshinde/recursion-in-python-is-when-a-function-calls-itself-to-solve-a-smaller-piece-of-the-same-problem-71ae39da52b6?source=rss-3fea1e749bec------2</link>
            <guid isPermaLink="false">https://medium.com/p/71ae39da52b6</guid>
            <dc:creator><![CDATA[swagatshinde]]></dc:creator>
            <pubDate>Fri, 14 Aug 2026 05:09:12 GMT</pubDate>
            <atom:updated>2026-08-18T16:22:09.173Z</atom:updated>
            <content:encoded><![CDATA[<p>Recursion in Python is when a function calls itself to solve a smaller piece of the same problem until it reaches a stopping condition.</p><h3>What is Recursive Functions?</h3><ul><li>A recursive function is a specific implementation of recursion. Instead of solving a complex problem directly, recursive functions break it down into smaller, more manageable instances of the same problem.</li><li>Each time the function is called, it operates on a smaller or simpler part of the problem until it reaches a base case — a condition where the function returns a specific result without making any further recursive calls.</li><li>Recursive functions are commonly used in various programming languages, including Python, to solve problems that exhibit repetitive or self-similar structures.</li></ul><h3>Types of Recursion in Python</h3><p>Recursion can be categorized into two main types: direct recursion and indirect recursion.</p><h3>1. Direct Recursion</h3><p>Direct recursion occurs when a function calls itself directly. It can be categorized into four subtypes: tail recursion, head recursion, tree recursion, and nested recursion.</p><h4>1.1. Tail Recursion</h4><p>This type of recursion is a type of direct recursion in which the recursive call is the last operation in the function. This allows the compiler or interpreter to optimize the recursion, as it doesn’t need to maintain a stack of function calls</p><p>Code:</p><pre>def factorial_tail(n, result=1):<br>    if n == 0:<br>        return result<br>    else:<br>        return factorial_tail(n - 1, n * result)<br>result = factorial_tail(5)<br>print(result) </pre><p>Output:</p><p>120</p><h4>1.2. Head Recursion</h4><p>Head recursion is the opposite of tail recursion. Here, the recursive call is the first operation performed in a function. This type is less common and doesn’t benefit from the same optimization as tail recursion.</p><p>Code:</p><pre>def factorial_head(n):<br>    if n == 0:<br>        return 1<br>    else:<br>        return n * factorial_head(n - 1)<br>result = factorial_head(5)<br>print(result)</pre><p>Output:</p><p>120</p><h4>1.3. Tree Recursion</h4><p>Tree recursion occurs when a function makes multiple recursive calls, branching into a tree-like structure of recursive calls. This is often seen in problems related to trees or hierarchical structures.</p><p>Code:</p><pre>def fibonacci_tree(n):<br>    if n &lt;= 1:<br>        return n<br>    else:<br>        return fibonacci_tree(n - 1) + fibonacci_tree(n - 2)<br>result = fibonacci_tree(6)<br>print(result)</pre><p>Output:</p><p>8</p><h4>1.4. Nested Recursion</h4><p>Nested recursion involves a function calling itself with a recursive call as one of its arguments.</p><p>Code:</p><pre>def nested_recursion(n):<br>    if n &lt;= 0:<br>        return 1<br>    else:<br>        return nested_recursion(n - nested_recursion(n - 1))<br>result = nested_recursion(3)<br>print(result)</pre><p>Output:</p><p>1</p><h3>2. Indirect Recursion:</h3><p>Indirect recursion occurs when two or more functions call each other in a circular manner.</p><p>Code:</p><pre>def is_even(n):<br>  if n == 0:<br>    return True<br>  else:<br>    return is_odd(n - 1)<br>def is_odd(n):<br>  if n == 0:<br>    return False<br>  else:<br>    return is_even(n - 1)<br>print(is_even(10))<br>print(is_odd(10))</pre><p>Output:</p><p>True</p><p>False</p><p><strong><em>The Two Critical Rules </em></strong><br>Every recursive function must contain two parts to execute correctly and avoid infinite loops:<br>1.Base Case: The stopping condition that returns a direct answer without making further recursive calls.<br>2.Recursive Step: The logic where the function reduces the input size and calls itself.</p><p><strong>Q.Why do we use recursion in Python?</strong><br>-&gt;We use recursion because it turns complex, deeply nested problems into short, simple code.<br>Key Reasons:<br>-<em>Cleaner Code</em>: Solves multi-level problems in fewer lines than complex loops.<br><strong>-</strong><em>Handles Tree Structures</em>: Ideal for nested data like folders, file systems, a<em>nd family trees.<br>-Automatic Memory</em><strong>:</strong> Python tracks where you are automatically, so you don’t have to manage extra variables manually.</p><p><strong>Advantages of Recursion in Python</strong><br><em>-Shorter, Cleaner Code</em>: Transforms complex, multi-step logic into fewer, highly readable lines of code compared to long, nested loops.<br>-<em>Simplifies Dynamic Data</em>: Effortlessly navigates non-linear, tree-like structures such as file system directories, JSON trees, or HTML DOM elements.<br>-<em>Automatic State Tracking</em>: Python’s execution stack automatically tracks function calls and variables, removing the need to manage manual stack or queue data structures.<br>-<em>Natural Algorithm Design</em>: Fits divide-and-conquer strategy perfectly, making algorithms like Merge Sort, Quick Sort, and Binary Search simple to write and maintain.</p><p><strong>ex.1)What will be the output of the following Python code?</strong></p><pre>def count_down(n):<br> if n &lt;= 0: # Base case (stopping condition)<br> print(&quot;Done!&quot;)<br> return<br> print(n)<br> count_down(n - 1) # Recursive call (function calling itself)<br>count_down(3)</pre><p>//op<br>3<br>2<br>1<br>Done!</p><p><strong>The Standard Recursion Algorithm When designing a recursive algorithm, follow this structured pattern:</strong></p><ol><li>Start with the input parameter (N).</li><li>Check Base Case: Is (N) at its simplest, terminating state?<br>Yes: Return the base value directly and exit the current frame.<br>No: Proceed to the recursive step.</li><li>Execute Recursive Step:<br>Reduce or transform (N) into a smaller version (N’).<br>Call the function again with (N’).</li><li>Combine &amp; Return: Perform any pending operation with the returned sub-result and send it up the call stack.</li></ol><pre>5.def factorial(n):<br> # Step 2: Base Case Check<br> if n &lt;= 1:<br> return 1 # Returns value up the stack<br> <br> # Step 3 &amp; 4: Recursive Step + Reduction<br> return n * factorial(n - 1)</pre><p># Example Execution<br>result = factorial(4)<br>print(result) # Output: 24</p><p><strong>Trace of Call Stack Operations</strong><br>For factorial(4):<br>Push Stack (Calling down):<br>factorial(4) -&gt; waits for 4*factorial(3)<br>factorial(3) -&gt; waits for 3*factorial(2)<br>factorial(2) -&gt; waits for 2*factorial(1)<br>factorial(1) -&gt; hits Base Case! Returns 1</p><p><strong><em>Pop Stack (Unwinding up):</em></strong><br>factorial(2) receives 1 -&gt; returns 2 * 1 = 2<br>factorial(3) receives 2 -&gt; returns 3 * 2 = 6<br>factorial(4) receives 6 -&gt; returns 4 * 6 = 24</p><p><strong>ex.2) Greatest Common Divisor (GCD)The classic algorithm to find the GCD of two numbers is Euclid’s Algorithm.</strong><br>Logic: The GCD of two numbers a and b is the same as the GCD of b and a(modb). <br>When b becomes 0, the base case is reached and a is the GCD.</p><pre>def gcd(a, b):<br> # Base Case: When the remainder becomes 0, return &#39;a&#39;<br> if b == 0:<br> return a <br> # Recursive Step: Call gcd with &#39;b&#39; and the remainder (a % b)<br> return gcd(b, a % b)<br>print(gcd(48, 18)) # Output: 6</pre><p><strong>ex.3) Least Common Multiple (LCM)</strong><br>The simplest way to calculate the LCM recursively uses the mathematical relationship:</p><pre>def gcd(a, b):<br> if b == 0:<br> return a<br> return gcd(b, a % b)<br>def lcm(a, b):<br> # Base Case: Prevent division by zero if either input is 0<br> if a == 0 or b == 0:<br> return 0<br> # Calculate LCM using the recursive GCD<br> return abs(a * b) // gcd(a, b)<br>print(lcm(12, 18)) # Output: 36 We can write a recursive helper function to find the multiplier, or simply leverage our recursive gcd function.</pre><p>Operation Base Case Recursive Formula<br>GCD b==0 -&gt;returns a GCD(a, b) = GCD(b,a(mod b))<br>LCM a==0 or b==0-&gt; LCM(a, b) = a*b/GCD(a,b)<br> returns 0</p><h3>Conclusion</h3><p>Recursion in Python is a powerful technique that involves defining a function in terms of itself. We’ve explored its fundamentals, from understanding recursive functions to diving into various types of recursion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71ae39da52b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>