<?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"><channel><title><![CDATA[Sandesh Daundkar | Blogs]]></title><description><![CDATA[Sandesh Daundkar | Blogs]]></description><link>https://sandeshdaundkar.com</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 08:50:22 GMT</lastBuildDate><atom:link href="https://sandeshdaundkar.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[It's about the Bash!]]></title><description><![CDATA[Introduction
A bash script is a file having instructions that can also be executed individually on a command line. With scripting, you can automate repetitive tasks. To create a bash script, you can start by creating a file in your favorite editor & ...]]></description><link>https://sandeshdaundkar.com/bash-refresher</link><guid isPermaLink="true">https://sandeshdaundkar.com/bash-refresher</guid><category><![CDATA[Bash]]></category><category><![CDATA[Linux]]></category><category><![CDATA[bash script]]></category><category><![CDATA[Script]]></category><dc:creator><![CDATA[Sandesh Daundkar]]></dc:creator><pubDate>Tue, 07 Mar 2023 09:02:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/0gkw_9fy0eQ/upload/af4c8e51f1ddf185a4a499eaf58bb041.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction</h3>
<p>A bash script is a file having instructions that can also be executed individually on a command line. With scripting, you can automate repetitive tasks. To create a bash script, you can start by creating a file in your favorite editor &amp; saving it with the <code>.sh</code> extension. </p>
<h4 id="heading-shebang">Shebang (#!)</h4>
<p>The first line in every bash script is the shebang character, which tells the operating system which interpreter to use to parse the rest of the code. The shebang takes the following form,</p>
<pre><code class="lang-bash"><span class="hljs-comment">#!interpreter [arguments]</span>
</code></pre>
<p>Where, the interpreter is full path to the binary file &amp; arguments are optional.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Interpreter</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>#!/bin/bash</td><td>uses <code>bash</code> to parse the file</td></tr>
<tr>
<td>#!/usr/bin/env bash</td><td>uses <code>env</code> to find the path of the <code>bash</code> executable</td></tr>
</tbody>
</table>
</div><h4 id="heading-executing-the-script">Executing the Script</h4>
<p>The bash script can be executed in two ways, either by sourcing the file itself or by creating an executable. The way we can do it is as mentioned below, </p>
<ul>
<li>By sourcing the file itself</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> bash-refresher.sh
</code></pre>
<ul>
<li>By creating an executable</li>
</ul>
<pre><code class="lang-bash">chmod +x bash-refresher.sh
./bash-refresher.sh
</code></pre>
<h3 id="heading-printing">Printing</h3>
<p>Bash uses <code>echo</code> to display the argument passed to the command.  </p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, World!"</span>
</code></pre>
<h3 id="heading-commenting">Commenting</h3>
<p>Commenting plays very important role in programming, so that the developer can understand why a particular block of code was written. In bash, there are multiple ways in which you can comment your code. </p>
<h4 id="heading-single-line-comments">Single line comments</h4>
<p>Anything followed by the <code>#</code> hash character is skipped, this way you can write single line comment in bash.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># This is one-liner comment</span>
</code></pre>
<h4 id="heading-multi-line-comments">Multi line comments</h4>
<p>Multi-line comments in bash can be written using the <code>:</code> bash operator and <code>' '</code> pair of single quotes.</p>
<pre><code class="lang-bash">: <span class="hljs-string">'
This is a way one can write multi line comments in bash.
echo "This will not be echoed!"
'</span>
</code></pre>
<h3 id="heading-variables">Variables</h3>
<p>You can define variables in bash using the <code>=</code> assignment operator and can use the <code>$</code> dollar sign to access the variable. </p>
<blockquote>
<p>The assignment operator should not have space on either side while defining a variable</p>
</blockquote>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

language=<span class="hljs-string">"bash"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"This article is about the <span class="hljs-variable">$language</span> scripting"</span>
</code></pre>
<h3 id="heading-reading-input">Reading Input</h3>
<p>To read input from the user you can use <code>read</code>  command. To add a prompt , one can use the <code>-p</code> flag followed by the prompt string, whereas to read the input silently one can use the <code>-s</code> flag. The value entered by the user, is assigned to the variable name following the read command.</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

<span class="hljs-built_in">read</span> -p <span class="hljs-string">"Enter username:    "</span> username
<span class="hljs-built_in">read</span> -sp <span class="hljs-string">"Enter password:     "</span> password
</code></pre>
<p>You can access the value entered by the user, using the <code>$</code> dollar sign followed by the variable name.</p>
<h3 id="heading-conditional-statements">Conditional Statements</h3>
<p>You can define conditional statements in bash using <code>if-elif-else-fi</code> block. The conditions must be passed in single <code>[ ]</code> or double square<code>[[ ]]</code> brackets. In bash we use the <code>then</code> keyword to execute the block of code, when a certain condition returns <code>True</code>.</p>
<blockquote>
<p>Leave a space before and after the end of the condition statement inside the square brackets [[ condition ]]</p>
</blockquote>
<ul>
<li>Syntax for if-statements</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-keyword">if</span> [[ condition ]]
<span class="hljs-keyword">then</span>
    &lt;statements&gt;
<span class="hljs-keyword">elif</span> [[ condition ]]
<span class="hljs-keyword">then</span>
    &lt;statements&gt;
<span class="hljs-keyword">else</span>
    &lt;statements&gt;
<span class="hljs-keyword">fi</span>
</code></pre>
<ul>
<li>Example for if-statements</li>
</ul>
<p>The condition flags are listed at the end of the article, also you can use the logical operators to chain conditions.</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

<span class="hljs-built_in">echo</span> -p <span class="hljs-string">"Please enter your age: "</span> age

<span class="hljs-keyword">if</span> [[ -z <span class="hljs-variable">$age</span> || <span class="hljs-variable">$age</span> -le 0 ]]; 
<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"Please enter valid age! o_O"</span>

<span class="hljs-keyword">elif</span> [[ <span class="hljs-variable">$age</span> -gt 0 &amp;&amp; <span class="hljs-variable">$age</span> -lt 21 ]]; 
<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"we're young! \o/"</span>

<span class="hljs-keyword">elif</span> [[ <span class="hljs-variable">$age</span> -eq 21 ]]; 
<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"we're forever 21! :D"</span>

<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"we're just not 21 anymore! :("</span>

<span class="hljs-keyword">fi</span>
</code></pre>
<h3 id="heading-case-statements">Case Statements</h3>
<p>Case statements in bash are similar to switch cases in other programming languages. If a particular condition is matched, the statement block for that case is executed. If no case is matched the default statement  is executed.</p>
<ul>
<li>Syntax for case-statements</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-keyword">case</span> variable <span class="hljs-keyword">in</span> 
    case_condition )
         statements
    ;;
*)
    default_statements
<span class="hljs-keyword">esac</span>
</code></pre>
<blockquote>
<p>semi-colon <code>;</code> indicates the end of the command &amp; beginning of a new one. The asterisk <code>*</code> defines the default case</p>
</blockquote>
<ul>
<li>Example for case-statements</li>
</ul>
<pre><code class="lang-bas">#!/usr/bin/env bash

read -p "Joey doesn't share food! [True|False] ?" input

case $input in
  "True")
  echo "Right! \o/"
  ;;
  "False")
  echo "Oh my God! o_O"
  ;;
  * )
  echo "Invalid Choice!"
esac
</code></pre>
<h3 id="heading-loops">Loops</h3>
<p>Similar to other languages, bash also supports the <code>while</code> and the <code>for</code> loops. The statements to be executed must be enclosed within the <code>do-done</code> block. </p>
<h4 id="heading-for-loop">For Loop</h4>
<ul>
<li>Syntax - for loop</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-keyword">for</span> iterable <span class="hljs-keyword">in</span> iterables
    <span class="hljs-keyword">do</span>
        &lt;statements&gt;
    <span class="hljs-keyword">done</span>
</code></pre>
<ul>
<li>Example - for loop</li>
</ul>
<p>In this example, we are iterating over an array of strings, to access the element we use <code>"${array[@]}"</code> as iterables.</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

friends=(<span class="hljs-string">"Joey"</span> <span class="hljs-string">"Chandler"</span> <span class="hljs-string">"Ross"</span>)
<span class="hljs-keyword">for</span> friend <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">${friends[@]}</span>"</span>
<span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hi I'm <span class="hljs-variable">$friend</span>"</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h4 id="heading-while-loop">While Loop</h4>
<ul>
<li>Syntax - while loop</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-keyword">while</span> condition
    <span class="hljs-keyword">do</span>
        &lt;statements&gt;
    <span class="hljs-keyword">done</span>
</code></pre>
<ul>
<li>Example - while loop</li>
</ul>
<p>In this example, we have a while loop that would be executed until a valid password is entered. </p>
<blockquote>
<p>​    The <code>-z</code> flag checks if the value is empty</p>
</blockquote>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

<span class="hljs-keyword">while</span> [ -z <span class="hljs-variable">$INPUT</span> ]
<span class="hljs-keyword">do</span>
  <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Enter new password.Password should not be blank"</span>
  <span class="hljs-built_in">read</span> -s INPUT
<span class="hljs-keyword">done</span>
</code></pre>
<h3 id="heading-functions">Functions</h3>
<p>Functions are reusable blocks of code. In bash, you can define a function in two ways, by typing <code>function_name()</code> or using the <code>function</code> keyword.</p>
<p>A function can take no arguments or can have multiple arguments passed as command-line arguments. Command line arguments can be accessed using the <code>$</code> dollar sign and indexes starting at 1,  like <code>$1</code> for the first argument, <code>$2</code> for the second, and so on.</p>
<p>Unlike other languages, one does not have to call the function in bash using empty parenthesis, simply typing the <code>function_name</code> works.</p>
<ul>
<li>Syntax for functions</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">function_name</span></span>() {
    &lt;statements&gt;
}

<span class="hljs-comment"># calling a function</span>
function_name
</code></pre>
<ul>
<li>Example of functions</li>
</ul>
<p>In the example, we have two functions, one that takes no arguments and second one that takes two command line arguments &amp; display it as a conversation.</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>

<span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">joey_tribbiani</span></span>(){
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"How you doin'?"</span>
}

<span class="hljs-function"><span class="hljs-title">trained_for_nothing</span></span>(){
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$1</span> : You got a job?"</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$2</span> : Are you kidding? I'm trained for nothing!"</span>
}

joey_tribbiani
trained_for_nothing Ross Rachel
</code></pre>
<h3 id="heading-extras">Extras</h3>
<h4 id="heading-bash-expansions">Bash Expansions</h4>
<ul>
<li><p>Command Substitution <code>$(command)</code></p>
<p>This allows user to execute a command in subshell &amp; replace the command substitution with the output of the command enclosed. Another way to do this is by enclosing command within back ticks.</p>
<pre><code><span class="hljs-string">`command`</span>
</code></pre></li>
<li><p>Arithmetic Expansion <code>$(( expression ))</code></p>
<p>This allows user to evaluate the expression and replace it with the result of the expression enclosed.</p>
</li>
<li><p>Parameter Expansion <code>${parameter}</code></p>
<p>This allows user to access the value of the parameter and replace it.</p>
</li>
</ul>
<h4 id="heading-built-in-variables">Built-in Variables</h4>
<p>Bash has few builtin variables that are very useful while writing a bash script. The table below shows the variable and its description. Remember that the <code>$</code> dollar sign is used to access the variable in bash.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Variable</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td><code>$#</code></td><td>Total number of arguments passed</td></tr>
<tr>
<td><code>$@</code></td><td>Values of the arguments passed</td></tr>
<tr>
<td><code>$?</code></td><td>Execution status of the last command, 0 for success</td></tr>
<tr>
<td><code>$*</code></td><td>Values of the arguments passed</td></tr>
</tbody>
</table>
</div><blockquote>
<p>The difference between <code>$@</code> and <code>$*</code> is that, if you pass them inside quotes as a string, <code>*</code> takes all the positional arguments as a single parameter whereas <code>@</code> will pass each positional argument separately</p>
</blockquote>
<h4 id="heading-file-conditions">File Conditions</h4>
<p>Below table gives list of few operators that can be used to test a particular property of a file.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Flag</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>-d string</td><td>Returns True if the string is a directory</td></tr>
<tr>
<td>-f string</td><td>Returns True if the string is file</td></tr>
<tr>
<td>-g file</td><td>Returns True if the group-id is set on the file</td></tr>
<tr>
<td>-u file</td><td>Returns True if the user-id is set on the file</td></tr>
<tr>
<td>-r file</td><td>Returns True if the file is readable</td></tr>
<tr>
<td>-w file</td><td>Returns True if the file is writable</td></tr>
<tr>
<td>-x file</td><td>Returns True if the file is executable</td></tr>
<tr>
<td>-s file</td><td>Returns True if the size of the file is non-zero</td></tr>
</tbody>
</table>
</div><h4 id="heading-comparison-operators">Comparison Operators</h4>
<p>Below table gives list of few of the comparison operators available in bash.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operator</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>val1 -eq val2</td><td>Returns True if the values are equal</td></tr>
<tr>
<td>val1 -ne val2</td><td>Returns True if the values are not equal</td></tr>
<tr>
<td>val1 -gt val2</td><td>Returns True if the val1 is greater than val2</td></tr>
<tr>
<td>val1 -ge val2</td><td>Returns True if the val1 is greater than or equal to val2</td></tr>
<tr>
<td>val1 -lt val2</td><td>Returns True if the val1 is less than val2</td></tr>
<tr>
<td>val1 -le val2</td><td>Returns True if the val1 is less than or equal to val2</td></tr>
</tbody>
</table>
</div><h4 id="heading-exit-commands">Exit Commands</h4>
<p>There are two commands that work as exit commands with different purposes, the <code>exit</code> and the <code>return</code> command</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Command</td><td>Purpose</td></tr>
</thead>
<tbody>
<tr>
<td>exit</td><td>Exit from the bash script</td></tr>
<tr>
<td>return</td><td>Exit from the bash function</td></tr>
</tbody>
</table>
</div><h3 id="heading-conclusion">Conclusion</h3>
<p>Having bash knowledge plays a key role if you use Linux in your day-to-day work. I hope this article, helps you refresh your bash skills. </p>
<p>Thanks for reading 🙇🏻‍♂️</p>
<h3 id="heading-references">References</h3>
<ul>
<li><p><a target="_blank" href="https://ryanstutorials.net/bash-scripting-tutorial/">Ryan Tutorials</a></p>
</li>
<li><p><a target="_blank" href="https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html">Bash Reference Manual</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Switch Case in Python]]></title><description><![CDATA[Introduction
In python, there is no switch case, unlike other programming languages. So the question comes how do we write a switch case in python? 
Basically, there are two ways in which you can try to implement the switch case logic in python where...]]></description><link>https://sandeshdaundkar.com/switch-case-in-python</link><guid isPermaLink="true">https://sandeshdaundkar.com/switch-case-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Sandesh Daundkar]]></dc:creator><pubDate>Sat, 13 Aug 2022 11:19:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/pWlM5L6PFis/upload/v1660386601240/oRRPzVVHt.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In python, there is no switch case, unlike other programming languages. So the question comes how do we write a switch case in python? </p>
<p>Basically, there are two ways in which you can try to implement the switch case logic in python where you intend to execute certain statements when a particular case is matched.</p>
<ul>
<li>Using <code>if-elif-else</code> block</li>
<li>Using <code>python dictionary</code></li>
</ul>
<p>In this article, we will check both the ways by writing a function that performs arithmetic operations on the numbers passed to the function. The function would take the two numbers and operation as function arguments and return the result based on the operation passed to the function. </p>
<h3 id="heading-lets-begin">Let's begin</h3>
<h4 id="heading-function-to-write-switch-case-using-if-elif-else">Function to write switch-case using <code>if-elif-else</code></h4>
<p>The <code>if-elif-else</code> is used in python to write conditional statements as in other programming languages. You can write a switch case by checking every operation passed to function and based on the condition you can return the result.</p>
<p>This is how it is done,</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">switch_case_function</span>(<span class="hljs-params">x, y, op</span>):</span>
    <span class="hljs-keyword">if</span> op == <span class="hljs-string">"add"</span>:
        <span class="hljs-keyword">return</span> x + y
    <span class="hljs-keyword">elif</span> op == <span class="hljs-string">"sub"</span>:
        <span class="hljs-keyword">return</span> x - y
    <span class="hljs-keyword">elif</span> op == <span class="hljs-string">"div"</span>:
        <span class="hljs-keyword">return</span> x/y
    <span class="hljs-keyword">elif</span> op == <span class="hljs-string">"mul"</span>:
        <span class="hljs-keyword">return</span> x * y
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-string">"no valid operation"</span>

switch_case_function(<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-string">"add"</span>)
</code></pre>
<h4 id="heading-function-to-write-switch-case-using-python-dictionary">Function to write switch-case using <code>python dictionary</code></h4>
<p>Python dictionaries are used to store data in key-value pairs. The key of the dictionary must be an immutable datatype. The dictionary itself is mutable and it does not allow duplicate keys.</p>
<p>You can create a python dictionary in the following ways:</p>
<pre><code class="lang-python">
<span class="hljs-comment"># creating an empty dictionary in python</span>
mapping = {}        
mapping = dict() 

<span class="hljs-comment"># creating a dictionary with key-value pairs</span>
mapping = {<span class="hljs-string">"language"</span> : <span class="hljs-string">"python"</span>, <span class="hljs-string">"creator"</span> : <span class="hljs-string">"guido van rossum"</span>}
mapping = dict(language=<span class="hljs-string">"python"</span>, creator=<span class="hljs-string">"guido van rossum"</span>)
</code></pre>
<blockquote>
<p>The main thing to notice here is while you use <code>dict()</code> and pass the key-value pair, you can directly pass the key as the variable name instead of passing it as strings, as when creating a dictionary using curly braces. </p>
</blockquote>
<p>To get a value from the dictionary you can use the built-in <code>get()</code> method on the dictionary object. It returns the value of the key if a key exists else returns the default. </p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">switch_case_function</span>(<span class="hljs-params">x,y,op</span>):</span>
    operation_mapping = dict(add=x+y, sub=x-y, mul=x*y, div=x/y)
    <span class="hljs-comment"># returns no valid operation if key does not exists.</span>
    <span class="hljs-keyword">return</span> operation_mapping.get(op, <span class="hljs-string">"no valid operation"</span>) 

switch_case_function(<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-string">"add"</span>)
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Python does not have switch cases unlike other programming languages but you can implement the logic using the two methods shown above. The code is more readable when you write using python dictionaries. You can make your own choice.</p>
<p>Thanks for reading 🙇🏻‍♂️</p>
]]></content:encoded></item><item><title><![CDATA[Recursion]]></title><description><![CDATA[Hello Everyone 🙋🏻‍♂️, 
In this article, we will take a look at recursion in python. The purpose of this article is not about analyzing the complexity or which approach is better (iterative or recursive). As many searches, sort, and traversal progra...]]></description><link>https://sandeshdaundkar.com/recursion</link><guid isPermaLink="true">https://sandeshdaundkar.com/recursion</guid><category><![CDATA[Python]]></category><category><![CDATA[Recursion]]></category><dc:creator><![CDATA[Sandesh Daundkar]]></dc:creator><pubDate>Sat, 06 Aug 2022 21:35:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/kesOP4tet6Y/upload/v1659821781187/EG6rT0OkT.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello Everyone 🙋🏻‍♂️, </p>
<p>In this article, we will take a look at recursion in python. The purpose of this article is not about analyzing the complexity or which approach is better (iterative or recursive). As many searches, sort, and traversal programs use recursion as their solution, it becomes important to understand the basics of recursion prior to getting into the DSA.</p>
<p>Recursion as the name says is a way in which a function recursively calls itself directly or indirectly. While you intend to write recursion, you have to be very careful as the function calls can get into infinite loops. However, there is a default upper limit set for the number of recursion calls, beyond which the program runs into stack overflow or runtime error. A recursion algorithm has two cases, the recursive case, and the base case. Recursion is terminated once the base case is recognized. Generally, the base case output is already known so we can directly return the value when this case is encountered. </p>
<p>With recursion, you can break complex problems into similar smaller ones. Also, the code is more readable than the iterative approach. Recursion is slower than iteration as it has to maintain the stack and uses more memory.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1659813786070/GhDKuyyUh.png" alt="image.png" class="image--center mx-auto" /> </p>
<p>The <code>greet()</code> function calls itself <strong><em>recursively</em></strong>, if you call the function and run this code, it will result in an infinite loop that would print "hello" as output as there is no base case in the function to terminate the recursion. However, in python, you can encounter the <code>RecursionError</code> once the upper limit of the recursion call is reached.</p>
<p>You can get or set the recursion limit in python using the <code>sys</code> module, to do so execute the following code in your python terminal,</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> sys

<span class="hljs-comment"># fetch the recursion upper limit</span>
sys.getrecursionlimit()
<span class="hljs-comment"># ouput: 1000</span>

<span class="hljs-comment"># if you intend to change the upper limit, you can do so by executing</span>
sys.setrecursionlimit(<span class="hljs-number">200</span>)
</code></pre>
<p>That's it for the basic understanding of recursion.</p>
<hr />
<p><strong><em>Now, let's see an example for finding the factorial of a number using recursion.</em></strong></p>
<blockquote>
<p>Factorial of a number is a product of all positive numbers smaller than or equal to the number. It is represented by using exclamation after the number. Also, remember that the Factorial of 0 is 1 (0!=1).</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">factorial</span>(<span class="hljs-params">number</span>):</span>
    <span class="hljs-string">'''
    The base case here is when the number becomes 0, we already know 0! = 1
    we can directly return 1 once the base case is reached.
    '''</span>
    <span class="hljs-keyword">if</span> number == <span class="hljs-number">0</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> number * factorial(number<span class="hljs-number">-1</span>)

factorial(<span class="hljs-number">4</span>)
</code></pre>
<p><strong><em>Visualize the factorial recursion code below by clicking the next option</em></strong></p>
<iframe width="800" height="500" src="https://pythontutor.com/iframe-embed.html#code=def%20factorial%28number%29%3A%0A%20%20%20%20if%20number%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20return%20number%20*%20factorial%28number-1%29%0A%0Afactorial%284%29&amp;codeDivHeight=400&amp;codeDivWidth=350&amp;cumulative=false&amp;curInstr=0&amp;heapPrimitives=nevernest&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false"> </iframe>

<p>You can observe, the factorial function calls itself with a number decremented by 1, once it reaches the base-case the recursion stops and starts returning the value for each function call until it reaches the actual call <code>factorial(4)</code> and returns the value <code>24</code>.</p>
<p>I hope this article, helps you understand the basics of recursion. As an exercise, you can try implementing the <a target="_blank" href="https://www.cuemath.com/numbers/fibonacci-series/">fibonacci series</a> using python and visualize the code on <a target="_blank" href="https://pythontutor.com/visualize.html#mode=edit">pythontutor</a>.</p>
<p>Thanks for reading 🙇🏻‍♂️</p>
<p><strong>references</strong></p>
<p>https://www.hackerearth.com/practice/basic-programming/recursion/recursion-and-backtracking/tutorial/
https://everythingcomputerscience.com/discrete_mathematics/Recurssion.html</p>
]]></content:encoded></item><item><title><![CDATA[Binary Trees]]></title><description><![CDATA[Hello Everyone 🙋🏻‍♂️, 
In this article, we will try to understand what are binary trees, as we go along we will be exploring different tree data structures so we must have a basic understanding of the binary tree.
A binary tree is a tree comprising...]]></description><link>https://sandeshdaundkar.com/binary-trees</link><guid isPermaLink="true">https://sandeshdaundkar.com/binary-trees</guid><category><![CDATA[BinaryTrees]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Sandesh Daundkar]]></dc:creator><pubDate>Sat, 06 Aug 2022 13:19:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/3GZNPBLImWc/upload/v1659792637723/mGAu-nHwS.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello Everyone 🙋🏻‍♂️, </p>
<p>In this article, we will try to understand what are binary trees, as we go along we will be exploring different tree data structures so we must have a basic understanding of the binary tree.</p>
<p>A binary tree is a tree comprising nodes, with each node having at most two other nodes referred to as the left child and the right child of the node.</p>
<p>Each node contains three components data element, pointer to the left child and pointer to the right child, they are recursively defined. 
If there is no child the pointer points to null. </p>
<p>A data element can be any type of data, while the left and the right pointer point to <strong><em>binary trees</em></strong> on the left and the right side of the node respectively.</p>
<p>The image below represents a binary tree and the node structure if you extract a node element.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1659790009814/2EoZcX9XY.png" alt="image.png" /></p>
<h4 id="heading-binary-tree-in-python">Binary Tree in Python</h4>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BinaryTree</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, data</span>):</span>
        self.data = data
        self.left = <span class="hljs-literal">None</span>
        self.right = <span class="hljs-literal">None</span>

root_node = Node(<span class="hljs-number">1</span>)
</code></pre>
<h5 id="heading-some-important-definitions">Some important definitions</h5>
<ul>
<li>The topmost node is called the root node.</li>
<li>The nodes that are connected downwards and are next to each other are called children.</li>
<li>The node without any children is called a leaf node.</li>
<li>Node with at least one child is called an internal node.</li>
<li>Node without any child is called an external node.</li>
<li>Nodes with the same parent are called siblings.</li>
</ul>
<h5 id="heading-types-of-binary-trees">Types of Binary Trees</h5>
<ul>
<li>Full Binary Tree</li>
<li>Complete Binary Tree</li>
<li>Perfect Binary Tree</li>
<li>Degenerate Binary Tree</li>
<li>Balanced Binary Tree</li>
</ul>
<p>I hope this article gave you a basic understanding of the binary tree. We will be exploring each type of BT in the later articles.</p>
<p>Thanks for reading 🙇🏻‍♂️</p>
<p><strong>references</strong></p>
<p>https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/</p>
]]></content:encoded></item></channel></rss>