<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Mebo</title>
    <description>The latest articles on DEV Community by David Mebo (@meistens).</description>
    <link>https://dev.to/meistens</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F975799%2F0675ec41-7162-4fc2-8ecf-574c622937f6.jpg</url>
      <title>DEV Community: David Mebo</title>
      <link>https://dev.to/meistens</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meistens"/>
    <language>en</language>
    <item>
      <title>Eliminating Duplicates: A Guide to Removing Duplicates from a Sorted Array</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Sun, 12 Mar 2023 19:34:06 +0000</pubDate>
      <link>https://dev.to/meistens/eliminating-duplicates-a-guide-to-removing-duplicates-from-a-sorted-array-3p29</link>
      <guid>https://dev.to/meistens/eliminating-duplicates-a-guide-to-removing-duplicates-from-a-sorted-array-3p29</guid>
      <description>&lt;p&gt;A new week, new challenges, and new algorithms to tackle and most importantly, understand. The algorithm we will be looking at, is an algorithm that allows one to delete duplicate elements from an already sorted array.&lt;br&gt;
The algorithm in question is considered to be an easy one compared to the Dutch Flag Partitioning, which looked easy with the detailed explanation, but the reason for the difficulty being during whiteboard sessions (will update the entries with them soon) where you mix the pointer values in terms of greater, lesser and equality.&lt;br&gt;
Anyhoo, let's get started with this one.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Question
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/"&gt;See leetcode question 26&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Naive/Brute-force Approach
&lt;/h2&gt;

&lt;p&gt;Obviously, there is a naive approach for this, and the plus side is its &lt;strong&gt;Space complexity&lt;/strong&gt; is O(1).&lt;/p&gt;
&lt;h3&gt;
  
  
  Explanation/Thought Process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;assign a variable to represent the length of the array&lt;/li&gt;
&lt;li&gt;iterate through the array

&lt;ul&gt;
&lt;li&gt;use another loop to iterate from the next index to the end of the array

&lt;ul&gt;
&lt;li&gt;if the element from the first loop matches the element of the second loop&lt;/li&gt;
&lt;li&gt;delete the matching element found&lt;/li&gt;
&lt;li&gt;decrement the length of the array by one (we do this to cover for the missing element or we will get errors or wrong values)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;print the array (for visuals) &lt;/li&gt;
&lt;li&gt;return the length of the array &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;delDups.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delDups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;test_delDup.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;delDups&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;delDups&lt;/span&gt;

&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;dups&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;delDups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;dups&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;5
&lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem With This Solution
&lt;/h3&gt;

&lt;p&gt;While it fulfills the &lt;strong&gt;Space complexity&lt;/strong&gt; of O(1), the &lt;strong&gt;Time complexity&lt;/strong&gt; is O(n^2), which is the result of using two nested loops which take O(n) time to iterate through the array and O(n) time for comparisons for each iteration. And by this Big O notation graph I got, it is terrible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better Approach and Optimal Solution
&lt;/h2&gt;

&lt;p&gt;There is a better approach that takes O(n) time to sort the array, removes duplicates, and still maintains the same O(1) space as seen in the brute-force approach. We just need to make some modifications to the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explanation/Thought Process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;assign the length of the array to a variable and another (we'll call it idxOne) to represent the array index to be 1&lt;/li&gt;
&lt;li&gt;check if the array is empty, if it is, stop the operation&lt;/li&gt;
&lt;li&gt;if not, iterate through the array starting from the first index of the array

&lt;ul&gt;
&lt;li&gt;if idxOne - 1 is not equal to the current index of the array

&lt;ul&gt;
&lt;li&gt;write the said index to an array, where the array is idxOne&lt;/li&gt;
&lt;li&gt;idxOne is incremented by one&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;return idxOne&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;delDupsBetter.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;del_dups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;idxOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idxOne&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idxOne&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;idxOne&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;idxOne&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;test_delDupsBetter.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;delDupsBetter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;del_dups&lt;/span&gt;

&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;dups&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;del_dups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dups&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;dups&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;5
&lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem With This Solution
&lt;/h3&gt;

&lt;p&gt;None I could find. Plus, this is a bit cleaner than having to use nested loops.&lt;/p&gt;

&lt;h2&gt;
  
  
  LeetCode Solution
&lt;/h2&gt;

&lt;p&gt;Unfortunately, not going to get creative with this one, reason being its unnecessary for this solution, so here it is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;leetcode_26.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;removeDuplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

        &lt;span class="n"&gt;idxOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idxOne&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idxOne&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;idxOne&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;idxOne&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, I'll see you some time next week with more array-related questions, or maybe something new.&lt;br&gt;
Remember to practice in your own free time though...&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Optimizing Dutch Flag Partitioning for Unsorted Arrays</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Tue, 07 Mar 2023 21:35:49 +0000</pubDate>
      <link>https://dev.to/meistens/optimizing-dutch-flag-partitioning-for-unsorted-arrays-4nej</link>
      <guid>https://dev.to/meistens/optimizing-dutch-flag-partitioning-for-unsorted-arrays-4nej</guid>
      <description>&lt;h2&gt;
  
  
  Sort Colors
&lt;/h2&gt;

&lt;p&gt;Been a while since my last entry, been busy with life and trying to practice cracking algorithms, whiteboard, read some algorithm-based book... Its been wild. Anyways, this question was taken from a leetcode medium level exercise, and though it gave me some trouble trying to implement the needed code given, it was kinda worth it at the end.&lt;br&gt;
Also, why medium level?&lt;br&gt;
That is because I'm still covering arrays, and not gonna go crazy blazing through the whole algorithm materials I'm studying. My goal's to understand most of the algorithms with useful use-case in the real world (like this one), not some algo-junkie...&lt;br&gt;
Anyways, let's get down to business.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Question
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/sort-colors/"&gt;See leetcode question 75&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Naive/Brute-force Approach
&lt;/h2&gt;

&lt;p&gt;This approach - I like to call it the Caveman Approach - will have us loop through the array with each successful sort. More in the section below (will be using this approach in future posts)&lt;/p&gt;
&lt;h3&gt;
  
  
  Explanation/Thought Process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;a variable to represent the pivot is defined&lt;/li&gt;
&lt;li&gt;a list is iterated from left to right

&lt;ul&gt;
&lt;li&gt;with each iteration, elements smaller than the pivot are sorted to the left of the array via swaps&lt;/li&gt;
&lt;li&gt;the loop is exited&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;iterate through the list again, but in reverse

&lt;ul&gt;
&lt;li&gt;with each iteration, elements larger than the pivot are moved to the right side of the array&lt;/li&gt;
&lt;li&gt;elements less than the pivot are not sorted (exit the loop if that is the case)&lt;/li&gt;
&lt;li&gt;the loop is exited&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code below looks like some medium-level problem alright:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dutch_flag.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dutch_flag_partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pivot_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivot_idx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# First pass: group elements smaller than the pivot, loop restarts with each sort
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;

    &lt;span class="c1"&gt;# Second pas: group elements larger than the pivot, same as above loop
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;))):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;1, 0, 1, 0, 0, 2, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem With This Solution
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Space complexity&lt;/strong&gt; is O(1), but the time complexity is O(n^2). This is because in the first pass, while searching for elements to swap, the array starts sorting from the beginning after each successful swap.&lt;/p&gt;

&lt;h2&gt;
  
  
  Better Approach
&lt;/h2&gt;

&lt;p&gt;There is another way to approach this problem that saves us time. This approach will have us continue searching for elements to swap after one has been done by resuming operations from the next index after a swap is carried out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explanation/Thought Process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;two variables are initialized to represent the first and last index of the array and act as pointers&lt;/li&gt;
&lt;li&gt;a variable is initialized to represent the pivot&lt;/li&gt;
&lt;li&gt;the list is iterated from left to right

&lt;ul&gt;
&lt;li&gt;from the loop, we check if the elements are less than the pivot element. If so, the element found is swapped with the pointer that represents the first index of the array, and the pointer is incremented. This goes on until all elements are swapped appropriately.&lt;/li&gt;
&lt;li&gt;loop exited, process moves to the next one&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;the list is iterated again, but in reverse

&lt;ul&gt;
&lt;li&gt;if elements found are less than the pivot, the loop is broken out of&lt;/li&gt;
&lt;li&gt;else if elements found are greater than the pivot, the same process carried out on the first loop is repeated, but the elements found are swapped with the pointer that represents the last index of the array, and that is decremented instead.&lt;/li&gt;
&lt;li&gt;loop exited, you have your sorted list/array.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Problem With The Solution
&lt;/h3&gt;

&lt;p&gt;None actually, except maybe white-boarding this might give newbies a headache for days, especially if you got the case of OCD or it's your first time, and still got OCD...&lt;br&gt;
Anyways, &lt;strong&gt;Space complexity&lt;/strong&gt; is O(1) like above, no additional memory created, only swaps, but &lt;strong&gt;Time complexity&lt;/strong&gt; is O(n), because we are sorting the array elements in one iteration through the loop compared to the Brute-force approach.&lt;/p&gt;

&lt;p&gt;Code below:&lt;br&gt;
&lt;code&gt;dutch_flag_On_improved.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dutch_flag_partitioning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pivot_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pivot_idx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;smaller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="c1"&gt;# First pass: groups elements smaller than the pivot
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# swap the elements between each other
&lt;/span&gt;            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="c1"&gt;# increment smaller variable/ptr
&lt;/span&gt;            &lt;span class="n"&gt;smaller&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="c1"&gt;# Second pass: groups elements larger than the pivot
&lt;/span&gt;    &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;))):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;0, 2, 1, 0, 1, 0, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optimal Solution
&lt;/h2&gt;

&lt;p&gt;This one looks like how a &lt;strong&gt;Dutch Flag Partitioning&lt;/strong&gt; should look like, given the pseudocode seen on Wikipedia when Dutch Flag Partition is looked up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explanation/Thought Process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Three variables (we will call them smaller, equal, and larger) are created to represent the first and last indexes of the array, with two out of the three variables initialized to the beginning of the array/list. These will be the pointers&lt;/li&gt;
&lt;li&gt;a loop is created, which executes if the equal pointer is less than the larger pointer

&lt;ul&gt;
&lt;li&gt;if the array element is equal to zero, the smaller and equal pointers elements are swapped, and the smaller and equal pointer is incremented by one.&lt;/li&gt;
&lt;li&gt;else if the array element is equal to two, the equal and larger elements represented by their respective pointers are swapped, and the pointers are incremented by one.&lt;/li&gt;
&lt;li&gt;else, we increment the equal pointer.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Problem With The Solution
&lt;/h3&gt;

&lt;p&gt;None.&lt;/p&gt;

&lt;p&gt;Code below:&lt;br&gt;
&lt;code&gt;dutch_flag_On_improved2.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dutch_flag_partitioning2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pivot_idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# pivot = arr[pivot_idx]
&lt;/span&gt;
    &lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;smaller&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks pretty basic for a medium-level problem, but I assure you it is not basic, especially if you are white-boarding.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Solution
&lt;/h2&gt;

&lt;p&gt;Here's my own solution. Implementing the larger part was a pain. Code below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mine.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sortColors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="s"&gt;"""
        Do not return anything, modify nums in-place instead.
        """&lt;/span&gt;
        &lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="s"&gt;"""Iterate through the array"""&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="s"&gt;"""If the element in the array is on the smaller side based
            on the Dutch flag algorithm, swap and increment the smaller ptr"""&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;smaller&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                &lt;span class="s"&gt;"""Else if the element is on the larger side, decrement if
                it is on the larger side and also if the current array element
                is less than the larger element"""&lt;/span&gt;
                &lt;span class="s"&gt;"""Also check again for elements on the smaller side and
                move accordingly"""&lt;/span&gt;
            &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;larger&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;larger&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;smaller&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                    &lt;span class="n"&gt;smaller&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;result:&lt;br&gt;
An ordered list of 0's, 1's, and 2's.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Unlock the Power of Arrays: Tips and Tricks for Working with Arrays</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Wed, 01 Mar 2023 17:15:12 +0000</pubDate>
      <link>https://dev.to/meistens/unlock-the-power-of-arrays-tips-and-tricks-for-working-with-arrays-1pi7</link>
      <guid>https://dev.to/meistens/unlock-the-power-of-arrays-tips-and-tricks-for-working-with-arrays-1pi7</guid>
      <description>&lt;p&gt;It is no news that most wizards in the programming community, even accomplished polyglots do not like Python due to its weird(?) way of doing things. Still, hey, technology is all about innovation and change.&lt;/p&gt;

&lt;p&gt;Some things to note though when working with arrays in Python:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They are called &lt;em&gt;lists&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;There are other types of arrays that are similar, but different from &lt;em&gt;lists&lt;/em&gt;, like &lt;em&gt;tuples&lt;/em&gt;, &lt;em&gt;dictionaries&lt;/em&gt;,...&lt;/li&gt;
&lt;li&gt;Know the syntax for instantiating a list, e.g., [1, 2, 3, 4], [1] + [2] * [3].&lt;/li&gt;
&lt;li&gt;Know how to use the functions &lt;code&gt;insert&lt;/code&gt;, &lt;code&gt;append&lt;/code&gt;, and &lt;code&gt;remove&lt;/code&gt;, and do not be afraid to use them if you cannot do the operations manually. It's all about innovation and using the libraries and functions already provided instead of trying to reinvent the wheel, unless you are asked not to (looking at you, C).&lt;/li&gt;
&lt;li&gt;Know how to instantiate a 2D array, e.g., [[1, 2, 3], [4, 5, 6], [7, 8]].&lt;/li&gt;
&lt;li&gt;Checking if a value is present in an array is as simple as &lt;code&gt;i&lt;/code&gt; in &lt;code&gt;arr&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Understand how &lt;em&gt;copy&lt;/em&gt; and &lt;em&gt;deep copy&lt;/em&gt; work, the difference between the two, and when to use them. E.g., A = B and A = list(B), where &lt;code&gt;B(list)&lt;/code&gt; is a different copy of &lt;code&gt;B&lt;/code&gt; (more on that in the future).&lt;/li&gt;
&lt;li&gt;Slice is your best bud, use it wisely.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There may be some unmentioned here, but if you know any, I appreciate it if you could share.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Ace the Basics of Arrays: A Step-By-Step Guide</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Sun, 26 Feb 2023 09:48:42 +0000</pubDate>
      <link>https://dev.to/meistens/ace-the-basics-of-arrays-a-step-by-step-guide-1b81</link>
      <guid>https://dev.to/meistens/ace-the-basics-of-arrays-a-step-by-step-guide-1b81</guid>
      <description>&lt;h2&gt;
  
  
  Array Introduction
&lt;/h2&gt;

&lt;p&gt;An array is a contiguous block of memory, and the simplest data structure known so far. It is usually used to represent sequences.&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
Given an array A[i], A[i] denotes the (i + 1)th object stored in the array.&lt;br&gt;
With your typical use of arrays, the &lt;strong&gt;Time complexity&lt;/strong&gt; for retrieving and updating data from an array takes O(1) time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Array Actions
&lt;/h2&gt;

&lt;p&gt;Here, we will look at the &lt;strong&gt;Time complexities&lt;/strong&gt; of inserting and deleting an element of an array, and hopefully, the explanation will be clear enough for anyone to understand.&lt;br&gt;
Also in the future, other aspects of arrays, like modifying/changing an element in an array will be covered. But for now, we will stick to the basics.&lt;/p&gt;
&lt;h3&gt;
  
  
  Insertion of An Element In An Array
&lt;/h3&gt;

&lt;p&gt;Insertion of new data/element into a full array can be handled by resizing i.e. allocating a new array with additional memory, and copying over the entries from the original array increases the worst-case time of insertion.&lt;br&gt;
But, if the new array has a constant factor larger than the original array, the average &lt;strong&gt;Time complexity&lt;/strong&gt; for insertion is constant O(1), since resizing is infrequent.&lt;/p&gt;
&lt;h3&gt;
  
  
  Deletion of An Element In An Array
&lt;/h3&gt;

&lt;p&gt;Deletion of an element from an array involves moving all successive elements one over to the left to fill the space left from the deleted/removed element in the array.&lt;br&gt;
The &lt;strong&gt;Time complexity&lt;/strong&gt; to delete/remove an element at index &lt;em&gt;i&lt;/em&gt; from an array of length &lt;em&gt;n&lt;/em&gt; is O(n - i). The same applies when one is inserting a new element into the array using the same principle.&lt;br&gt;
&lt;em&gt;Examples&lt;/em&gt;:&lt;br&gt;
If we have an array/list of elements {1, 2, 3, 4, 5, 6, 7} and we want to remove the element at index 3, to the uninitiated and those who are familiar with languages like COBOL, FORTRAN or AWK (not THAT awk), the value they'd be expecting to get cut off is 3, but if we followed the example given at the intro, the value getting removed is 4. This is because we are following the principle that 4 in the array (we'll call it A) A[4] is the (i + 1)th object stored in the array, where &lt;code&gt;i&lt;/code&gt; is the index of the array.&lt;br&gt;
You might have a solid argument if the language(s) you use that follows this principle, but I'm not gonna indulge you on that, take it to the stack overflow overzealous wizards!&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem(s)
&lt;/h2&gt;

&lt;p&gt;This problem statement gives us an insight of working with arrays (or lists if you are using Python), but for the sake of avoiding confusion on my part, arrays/lists are arrays, unless stated otherwise.&lt;br&gt;
For the question, they'd be structured in an unordered list. Sure looks weird for some leetcode warriors out there, but for us normies, it should make it easier to understand each requirement, whiteboard and explain the complex parts where necessary, which leads to you thinking up a good solution or ask the interviewer the right question(s) for hints that could lead to a potential answer.&lt;/p&gt;
&lt;h3&gt;
  
  
  Problem 01
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The question&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are given an array of integers&lt;/li&gt;
&lt;li&gt;You have to reorder the entries so that even entries appears first&lt;/li&gt;
&lt;li&gt;You are required to solve this without adding additional space&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the last option was not an option, we'd simply do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;split the array into two sub-arrays called &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;write a &lt;code&gt;for&lt;/code&gt; loop that traverses the array, with a parameter, most likely &lt;code&gt;i&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;inside the &lt;code&gt;for&lt;/code&gt; loop, we write a conditional to check if the elements (or numbers if you must) is a modulus of 2, i.e. the remainder is 0&lt;/li&gt;
&lt;li&gt;if it is, we add the number to the array (how? append the even number to the &lt;code&gt;left&lt;/code&gt; sub-array)&lt;/li&gt;
&lt;li&gt;if it is not a modulus of 2, i.e., there is a remainder of 1+, append to the &lt;code&gt;right&lt;/code&gt; sub array&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;return the values of the two sub-arrays combined.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;Time complexity&lt;/strong&gt; may be O(n), in which congratulations are in order, but the instructions does not want you to allocate any space to the array.&lt;/p&gt;

&lt;p&gt;If you were to follow the instructions to get the &lt;strong&gt;Space complexity&lt;/strong&gt; to be O(1), a third sub-array is needed. The third sub-array will be the original array you are sorting. So, from a point in your original thought process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the third sub-array (lets call it &lt;code&gt;original&lt;/code&gt;) is iterated&lt;/li&gt;
&lt;li&gt;from the point where you check for even and odd values, swap the values from &lt;code&gt;original&lt;/code&gt; to &lt;code&gt;left&lt;/code&gt; or &lt;code&gt;right&lt;/code&gt; sub-arrays. This expands the two sub-arrays, and shrinks the &lt;code&gt;original&lt;/code&gt; array. No additional space created for the new arrays, just a swap of sorts happening.
&lt;code&gt;arrayBasic01.py&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;O(n) space array i.e. allocated space to the array&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;even_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;000-testArray.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;arrayBasic01&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;even_left&lt;/span&gt;

&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;even_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above will output &lt;code&gt;[8, 2, 6, 4, 5, 7, 3, 1]&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: If you got OCD (feel free to modify) or think this is wrong, stop depending 100% on chatGPT. Its solutions resulted in this output despite it trying to force its 'correct' answer on me (its output was [2, 4, 6, 8, 1, 5, 7, 9] if you're wondering).&lt;/p&gt;

&lt;h4&gt;
  
  
  What's Going On
&lt;/h4&gt;

&lt;p&gt;What made the &lt;strong&gt;Space complexity&lt;/strong&gt; to be O(1) (chatGPT adamantly says it is impossible despite giving me an answer with O(1) space), is there is a variable that holds the indices (indexes), and a temporary variable for performing swaps.&lt;br&gt;
For &lt;strong&gt;Time complexity&lt;/strong&gt; analysis, since a constant amount of processes is done per entry to the sub-arrays, it is O(n), where &lt;code&gt;n&lt;/code&gt; is the length of the array.&lt;/p&gt;

&lt;p&gt;To close this entry, I'll drop some tips I read up;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Be comfortable writing code that utilizes subarrays&lt;/li&gt;
&lt;li&gt;When operating an array, do not forget that indexes starts from 0&lt;/li&gt;
&lt;li&gt;When operating 2D arrays, use &lt;strong&gt;parallel logic&lt;/strong&gt; for rows and columns&lt;/li&gt;
&lt;li&gt;Don't worry about order of an array, i.e. they must be equal, or see the note of the problem statement above&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more Array problems, approach and solutions, I'm building a repo of sorts for that, will contain leetcode questions and solutions (basically interview questions). Anyways, till next time.&lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>mentorship</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Relationships in Object-Oriented Programming</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Tue, 21 Feb 2023 22:11:48 +0000</pubDate>
      <link>https://dev.to/meistens/relationships-in-object-oriented-programming-2k2j</link>
      <guid>https://dev.to/meistens/relationships-in-object-oriented-programming-2k2j</guid>
      <description>&lt;p&gt;So far so good, I talked about &lt;a href="https://dev.to/meistens/public-and-private-interfaces-in-oop-1fpn"&gt;abstraction and encapsulation&lt;/a&gt; in my previous post, but we don't know yet how to use it to create proper levels of abstractions. There are ways to go about this, and they are called relationships. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Association&lt;/li&gt;
&lt;li&gt;Composition&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Aggregation&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Association
&lt;/h2&gt;

&lt;p&gt;Association is a type of relationship that has that "in a relationship with" relationship. It establishes a relationship between two classes using their objects.&lt;br&gt;
Take this UML diagram below, it shows two classes connected by a line. That line in UML is called an &lt;strong&gt;Association&lt;/strong&gt;, and it fits the use case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://postimg.cc/dk4gxsqF" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffs8onxcmx46jf6l7dkwt.png" alt="UMLClass-Of-Objects-excalidraw.png" width="441" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Composition
&lt;/h2&gt;

&lt;p&gt;Composition is the process of collecting several objects together for the purpose of creating a new object.&lt;br&gt;
An example of composition can be seen in the car example of abstraction &lt;a href="https://dev.to/meistens/public-and-private-interfaces-in-oop-1fpn"&gt;posted last week&lt;/a&gt;. The car object provides the interface needed for the driver, while also providing an interface to the mechanic to "fix" broken parts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregation
&lt;/h2&gt;

&lt;p&gt;Aggregation is almost similar to composition. The difference between them is that aggregate objects can exist independently of each other.&lt;br&gt;
Also, keep in mind that composition is the same as aggregation and aggregation is a more general form of of composition. Also, any composite relationship is also an aggregate one, but never the other way around.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://postimg.cc/rDVYRdx9" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4v4iu165f3af3yyhk3bd.png" alt="UMLComposite-Aggregate-excalidraw.png" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The UML above is a chess set, which shows how composition relationship (represented with a solid diamond) and aggregate relationships (hollow diamonds) works.&lt;br&gt;
In the UML, we will notice that the board and pieces are stored as a part of the &lt;strong&gt;Chess set&lt;/strong&gt; in exactly the same way a reference to them is stored as an attribute on the chess set. This shows the distinction between aggregation and composition is sometimes irrelevant since they behave in the same way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Inheritance is a relationship of sorts, where a class can inherit from another class or classes that share the same set of attributes and methods with the supposed class inheriting.&lt;br&gt;
In some languages like Java, the class being inherited from, is either called a &lt;em&gt;superclass&lt;/em&gt; or &lt;em&gt;base class&lt;/em&gt;.&lt;br&gt;
The class that is inheriting from the base class is called the &lt;em&gt;derived class&lt;/em&gt; or &lt;em&gt;subclass&lt;/em&gt;. The derived class inherits all the properties and methods, or overrides the ones inherited from the base class.&lt;/p&gt;

&lt;p&gt;For example, consider a class called &lt;code&gt;Animal&lt;/code&gt; that has properties such as &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;age&lt;/code&gt;, and methods such as &lt;code&gt;eat()&lt;/code&gt; and &lt;code&gt;sleep()&lt;/code&gt;. A subclass called &lt;code&gt;Cat&lt;/code&gt; can be created that inherits all the properties and methods of &lt;code&gt;Animal&lt;/code&gt;, and also has additional properties and methods specific to a cat, such as &lt;code&gt;meow()&lt;/code&gt; and &lt;code&gt;climb()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Inheritance helps to reduce code duplication, increase code reuse, and promote a more organized and modular programming style.&lt;/p&gt;

&lt;p&gt;On that note, hope it made things clear.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Public and Private Interfaces In OOP</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Tue, 14 Feb 2023 13:09:10 +0000</pubDate>
      <link>https://dev.to/meistens/public-and-private-interfaces-in-oop-1fpn</link>
      <guid>https://dev.to/meistens/public-and-private-interfaces-in-oop-1fpn</guid>
      <description>&lt;p&gt;Welcome to another section of OOP with Python. If you read the last &lt;a href="https://dev.to/meistens/oop-ood-and-ooa-in-python-intro-4bfo"&gt;post&lt;/a&gt;, I talked about OOP and other object-oriented paradigms like OOA and OOD. This time, we'll look at encapsulation and abstraction, and their role in creating public and private interfaces. To further support this, I will be using UML to show them in detail (I will cover UMLs in the not-too-distant future but if you are impatient, you can pick up UML Distilled and read).&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface
&lt;/h2&gt;

&lt;p&gt;The fundamental purpose of modeling an object is to determine what the public &lt;strong&gt;Interface&lt;/strong&gt; of the object will be. An &lt;strong&gt;interface&lt;/strong&gt; is the collection of attributes/instance variables and methods that other objects can interact with, and have access to. Let's use the &lt;em&gt;television&lt;/em&gt; as an example. The TV is an object, and the interface to the TV is the remote control. Each button on the remote represents a method that can be called on the television. When the access these methods, we are not from where the TV gets its signal, the signals are sent from the remote to the TV and other functionalities that make the TV work (speakers). The TV internals hides these things - the workings/implementations that make the TV work - from us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation
&lt;/h2&gt;

&lt;p&gt;The process of hiding these implementations of an object is referred to as &lt;strong&gt;encapsulation&lt;/strong&gt;. But, encapsulation's meaning is completely different. In other terms, it means to hide something, but in programming, it means to create a wrapper on the attributes. The public interface, on the other hand, is essential. It needs to be properly designed as other classes depend on it, meaning a change in the interface will affect those classes using the interface. Other parts can be changed and it will not affect the public interface, but if the interface is altered, it will break an application. To prevent this mistake from happening, one might spot a python variable starting with &lt;code&gt;_&lt;/code&gt;. It means that it is not a part of the public interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstraction
&lt;/h2&gt;

&lt;p&gt;Abstraction is another object-oriented term related to encapsulation and information hiding. Abstraction is the process of encapsulating information with a separate public interface. Any private element can be subject to information hiding. Take a car as an example. A car driver (client) needs to interact with the steering, accelerator, and brakes. The workings of the brakes, engines and the entire work do not matter to the driver, but to a mechanic (programmer), it does. Here are two levels of abstraction for a car from the driver (client) and mechanic (programmer) perspectives.&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; a leading &lt;code&gt;_&lt;/code&gt; can be used to suggest that it is not a public interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://postimg.cc/KKJwzHQM"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6qpxzXW_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.postimg.cc/tTjjr0x2/UMLAbstractions-Car-excalidraw.png" alt="UMLAbstractions-Car-excalidraw.png" width="880" height="691"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Overall, the takeaway from this post is that one has to make each model understandable to others and also ourselves in the future whenever we need to revisit and change how things work and interact with other objects.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No use of obnoxious names, like if an attribute should be named &lt;em&gt;instance_id&lt;/em&gt; and &lt;em&gt;user_id&lt;/em&gt; for easy identification, one goes and use &lt;em&gt;id&lt;/em&gt; for everything (because it's an object and it's encapsulated in an object/class won't cut it.)&lt;/li&gt;
&lt;li&gt;To make things somewhat easy, you should make sure your objects are nouns, methods verbs, and attributes as nouns or better yet, adjectives. It might be hard to do if you suddenly become conscious of it, but it'll naturally stick with you - like muscle memory - if you practice.&lt;/li&gt;
&lt;li&gt;When you design an interface, imagine what you want to be publicly accessible, and what is not. Even for private matters, you decide who you in your circle should know or have access to them privy information. In other words, treat it like it's a real-life scenario. You'd want everyone to know you got a new car but will not let a car painter service its engine. Ah well, till next time. The different relationships in OOP will be covered.
In the meantime, keep practicing!&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>oop</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>OOP, OOD, and OOA in Python Intro</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Tue, 14 Feb 2023 07:32:58 +0000</pubDate>
      <link>https://dev.to/meistens/oop-ood-and-ooa-in-python-intro-4bfo</link>
      <guid>https://dev.to/meistens/oop-ood-and-ooa-in-python-intro-4bfo</guid>
      <description>&lt;p&gt;The definition of an object is no different from an object in real life: a tangible thing one can touch, feel, sense, and manipulate.&lt;br&gt;
Things are different in programming. Objects might not be tangible, and cannot be sensed or felt, but they are models that can do certain things and vice-versa.&lt;br&gt;
Short of it, &lt;strong&gt;An object is a collection of data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now that we know what an object is, what about &lt;em&gt;object-oriented?&lt;/em&gt;&lt;br&gt;
Breaking it down (because that's what we mostly do), &lt;em&gt;oriented&lt;/em&gt;, according to the dictionary means &lt;em&gt;direct towards&lt;/em&gt;. Piecing both the definitions of an &lt;em&gt;object&lt;/em&gt; and &lt;em&gt;oriented&lt;/em&gt;, we can say that &lt;strong&gt;Object-oriented programming means writing code directed towards moving objects&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;&lt;a href="https://gfycat.com/discover/confused-gifs" rel="noopener noreferrer"&gt;&lt;/a&gt; &lt;a href="https://gfycat.com/ellipticalfluidlemur-angry-birds-2-hatchlings-confused-observe" rel="noopener noreferrer"&gt;via Gfycat&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yeah... doesn't make sense, I get it.&lt;br&gt;
The less confusing definition is below;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object-oriented programming (OOP) is seen as the process of converting a design into a&lt;br&gt;
working program or application that does what it is told to do.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are more concepts to Object-oriented, like &lt;em&gt;object-oriented design&lt;/em&gt; and &lt;em&gt;object-oriented analysis&lt;/em&gt;. Don't worry, they are still object-oriented.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object-oriented Paradigms
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Object-oriented analysis(OOA)&lt;/strong&gt;:&lt;br&gt;
This is the process of looking at a problem and identifying the objects and the interactions between the said objects.&lt;br&gt;
This stage is about what needs to be done.&lt;/p&gt;

&lt;p&gt;Example;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As a food scientist, I need a website to help users know how much to eat without it becoming a health risk to themselves.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a visitor, here is what I would like or think is essential for visitors.&lt;br&gt;
Each necessity a visitor wants is highlighted in &lt;strong&gt;bold&lt;/strong&gt; text to signify that it is an &lt;strong&gt;object&lt;/strong&gt;, and the actions of the visitors are in &lt;em&gt;italics&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;browse&lt;/em&gt; &lt;strong&gt;food choice&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;upload&lt;/em&gt; &lt;strong&gt;food decisions for the day/week&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;see&lt;/em&gt; &lt;strong&gt;recommended foods&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You get the idea...&lt;br&gt;
Once analysis/research has been carried out, we move on to the next stage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object-oriented design (OOD)&lt;/strong&gt;:&lt;br&gt;
This is the process of taking the results of the analysis above and implementing them. It's at this point does the programmer comes up with wicked-looking names for the objects, defines their expected behavior, and specifies certain behaviors of the objects. Kinda like them team meetings I hear about, starts good, but never end well. Ah well, moving on.&lt;/p&gt;

&lt;p&gt;Last is &lt;strong&gt;Object-oriented Programming&lt;/strong&gt; (scroll up for the definition if you missed it)... aaand we are done (not) with this bit. It looks confusing (trust me, I got lost learning for the first time) but over time, you'll get over it. On that note, look out for the next part!&lt;/p&gt;

</description>
      <category>cicd</category>
      <category>deployment</category>
      <category>devops</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Operators</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Tue, 29 Nov 2022 11:11:10 +0000</pubDate>
      <link>https://dev.to/meistens/operators-1h6m</link>
      <guid>https://dev.to/meistens/operators-1h6m</guid>
      <description>&lt;p&gt;More challenges given by ALX, more stuff to read about and grasp. Honestly, its overwhelming. Sure, I do have some basic knowledge with JavaScript, should be easy right?&lt;br&gt;
Yeah, it should be, if the challenges did not have conditions I gotta follow. I mean, why do I need to use putchars, puts when I can use printf and call it a day right? Boy, was I Wrong. Enough rant, let's get to documenting what I've learnt.&lt;br&gt;
Reminder: Mostly me documenting stuff, not meant to be an alternative to what you learn (fact-checking maybe) and also, I am talking about C, not JavaScript (even if their syntax seems familiar).&lt;/p&gt;
&lt;h2&gt;
  
  
  Operators
&lt;/h2&gt;

&lt;p&gt;Operators are symbols that allows a program to perform specific and logical tasks/operations.&lt;br&gt;
There are several types of operators in C:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arithmetic operators&lt;/li&gt;
&lt;li&gt;Relational operators&lt;/li&gt;
&lt;li&gt;Logical/boolean operators&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Arithmetic operators
&lt;/h2&gt;

&lt;p&gt;Arithmetic operators are mathematical expressions. There are seven of em:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plus (+)&lt;/li&gt;
&lt;li&gt; Minus (-)&lt;/li&gt;
&lt;li&gt;Multiplication (*)&lt;/li&gt;
&lt;li&gt;Division (/)&lt;/li&gt;
&lt;li&gt;Modulus (%)&lt;/li&gt;
&lt;li&gt; Increment (++)&lt;/li&gt;
&lt;li&gt;Decrement (--)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Arithmetic operators are of two types. Unary and binary operators.&lt;br&gt;
Unary operators are operators that works with an operand. Using the increment operator is a perfect example: &lt;code&gt;foo++&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Binary operators on the other hand, works with two or more operands. We use em everyday and everywhere, from calculating the speeds to safely land a spacecraft at NASA to calculating how much tip to give the pizza guy (1 + 1 anyone?)&lt;/p&gt;
&lt;h3&gt;
  
  
  Relational Operators
&lt;/h3&gt;

&lt;p&gt;Relational operators according to wikipedia, it is a construct that defines some kind of relationship between two entities. Methinks they are right, but swap out entities with operands. Here's a list of em;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; greater than
&amp;lt; less than
&amp;gt;= greater than or equal to
&amp;lt;= less than or equal to
== equal to
!= not equal to
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Examples below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo == foo
bar &amp;gt; baz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Logical/Boolean Operators
&lt;/h3&gt;

&lt;p&gt;Logical operators are operators used to combine two or more relational operators. They are used with statements (more on that).&lt;br&gt;
Below is a list of them;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logical AND (&amp;amp;&amp;amp;)&lt;/li&gt;
&lt;li&gt;Logical OR (||)&lt;/li&gt;
&lt;li&gt;Logical NOT (!)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are more types of logical operators, like the XOR, AND and OR operators but for my sanity, lets talk about the ones that I end up seeing almost everyday.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use of The Listed Operators
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;AND (&amp;amp;&amp;amp;) checks if both conditions in a statement is true. If the first one is false, the entire statement is false, irrespective of if the second condition is true. Example;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo == foo &amp;amp;&amp;amp; baz == baz        /*true*/
foo == bar &amp;amp;&amp;amp; baz == baz        /*false*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;OR(||) checks if either conditions given is true, meaning if one of them is false, the entire statement is evaluated as true unless both are false. Example;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo == bar || baz == baz        /*true*/
foo == bar || bar == baz        /*false*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;NOT(!) checks the conditions passed and returns the opposite value of the argument.
Take these snippets;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!(1&amp;gt;2)
!(1&amp;lt;2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first snippet says that 1 is greater than 2, which is &lt;strong&gt;false&lt;/strong&gt;. But, there's the NOT operator, which will inverse the value of the argument and that will make it &lt;strong&gt;true&lt;/strong&gt; because 1 is &lt;strong&gt;NOT&lt;/strong&gt; greater than 2.&lt;br&gt;
Going by the same logic for the second snippet, 1 is not less than 2. The result will be false because it is not true.&lt;/p&gt;

&lt;p&gt;There are other types of operators, like assignment operators (+=, -=,...), tenary operators, sizeof operators...&lt;br&gt;
Thing is, every language got an operator(s) that's specific/works well for them. Depending on what language syntax you choose to learn, don't mix up their operators. In other words, RTFM or look up some other source if the docs looks like someone tried transferring his/her frustrations onto its readers.&lt;/p&gt;

&lt;p&gt;For book recommendation, check out K.N. King's C Programming: A Modern Approach (make sure its the second edition you got). Should be worth the read for both beginners and anyone looking for a refresher.&lt;br&gt;
If you are done with that, pick up K &amp;amp; R ANSI C book. I have heard people say one should start with this book but nah, rather have most of it explained instead of doing a "shoot first, ask questions later" approach (debugging on the first chapter was NOT fun, especially if the issue was obvious if I knew what I was doing).&lt;/p&gt;

&lt;p&gt;Ah well, back to real life. New episode of Chainsaw Man out. Need a bit of fun before doing those mock interviews (who in their right mind thought it was a good idea to have several rounds of interviews for entry level roles???)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Rough intro to GCC (Hello, World!)</title>
      <dc:creator>David Mebo</dc:creator>
      <pubDate>Wed, 23 Nov 2022 17:00:00 +0000</pubDate>
      <link>https://dev.to/meistens/rough-intro-to-gcc-46ap</link>
      <guid>https://dev.to/meistens/rough-intro-to-gcc-46ap</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Write a script that runs a C file through the preprocessor and save the result into another file.&lt;br&gt;
The C file name will be saved in the variable $CFILE&lt;br&gt;
The output should be saved in the file c&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the first task starting my journey learning C, aside setting up your environment and all. Seems pretty straightforward... till I keyed in &lt;em&gt;man gcc&lt;/em&gt; in the terminal. The output? Lets just say I read half-way and gave up. Ended up getting a &lt;a href="https://www.amazon.com/Introduction-GCC-GNU-Compilers/dp/0954161793" rel="noopener noreferrer"&gt;book on GCC&lt;/a&gt; which in my opinion did a better job breaking down the commands and flags eli5 style.&lt;/p&gt;

&lt;p&gt;Anyways, C programs go through 4 compilation stages;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preprocessor&lt;/li&gt;
&lt;li&gt;Compiler&lt;/li&gt;
&lt;li&gt;Assembler&lt;/li&gt;
&lt;li&gt;Linker&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Preprocessing
&lt;/h2&gt;

&lt;p&gt;In the preprocessing stage, macros and header files are expanded i.e. you see what you added to the files. Macros? Header files? You see their source codes.&lt;br&gt;
By the way, the preprocessing step creates a file with .i extension. To pass a c file through a preprocessor, the flag you are looking for is &lt;code&gt;-E&lt;/code&gt;, command;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gcc -E foo.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Compilation
&lt;/h2&gt;

&lt;p&gt;Next step is the compilation stage. Here, the preprocessed file is passed through a compiler, which in turn creates source file with an extension '.s', which contains assembly code. The flag here is &lt;code&gt;-S&lt;/code&gt;, same command used as the preprocessor but the flags are swapped. Do note though that using the flag will not create an object file, that's the job of the assembler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assembler
&lt;/h2&gt;

&lt;p&gt;Third stage, the assembler. This is where assembly code goes from barely understandable except by those who learnt about it before I was born to being unreadable by virtually anyone on this planet, unless you are a robot or someone who's dedicated their life following the ways of the machines.&lt;br&gt;
The assembler takes the source file and turns it to an object file which contains machine code. To invoke the assembler, use&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;as foo.s -o foo.o&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 This will generate the object file 'foo.o'.&lt;/p&gt;
&lt;h2&gt;
  
  
  Linker
&lt;/h2&gt;

&lt;p&gt;The final stage is the linker. There's a lot going on under the hood here but the short of it is you invoke gcc followed by the object file one wishes to compile, like this;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gcc foo.o&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The command generates an executable called a.out. To run the executable, point to the path where the executable is located, followed by the executable name on your terminal. Hit enter and the results of your C file should display on your terminal.&lt;/p&gt;

&lt;p&gt;Back to the question, following the instructions, you'll realize you need two commands/flags. One for passing the file through a preprocessor, another for naming the output of the file&lt;br&gt;
In summary, the answer's;&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;gcc -E $CFILE -o c&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 Hopefully the rest of the task makes perfect sense to you (except the advanced task maybe, had to check the man page and wikipedia to figure it out).&lt;/p&gt;

&lt;p&gt;P.s: Blog post(s) are kinda like a journal to me and I am not in any way looking to educate anyone. It's not in me. Besides, there are lots of materials out there (I will gladly share the ones I used, they'd be mostly books, the occasional diving into the man pages and maybe stackoverflow and stackexchange).&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
