<?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: Simon Mei</title>
    <description>The latest articles on DEV Community by Simon Mei (@simon_mei_0de03b0b5a3299a).</description>
    <link>https://dev.to/simon_mei_0de03b0b5a3299a</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%2F1242011%2F4ee6856e-4667-4712-882b-35078ab5f5e9.jpeg</url>
      <title>DEV Community: Simon Mei</title>
      <link>https://dev.to/simon_mei_0de03b0b5a3299a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/simon_mei_0de03b0b5a3299a"/>
    <language>en</language>
    <item>
      <title>Boiled down: merge sort w/recursion</title>
      <dc:creator>Simon Mei</dc:creator>
      <pubDate>Thu, 30 May 2024 17:10:18 +0000</pubDate>
      <link>https://dev.to/simon_mei_0de03b0b5a3299a/boiled-down-merge-sort-wrecursion-fj0</link>
      <guid>https://dev.to/simon_mei_0de03b0b5a3299a/boiled-down-merge-sort-wrecursion-fj0</guid>
      <description>&lt;p&gt;In my coding journey, I've encountered recursion many times and can only wrap my head around basic recursion algorithms like factorials and the Fibonacci sequence. When it comes to merge sort, I bang my head against the wall because while I understand the idea of it, when it comes down to the code, it just didn't make sense... until now...&lt;/p&gt;

&lt;p&gt;I do feel that with recursion, it's an idea where you understand it better the more you see and work with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Merge sort idea
&lt;/h2&gt;

&lt;p&gt;This video explains extremely well how merge sort with recursion works: &lt;a href="https://youtu.be/4oqjcKenCH8?t=6248" rel="noopener noreferrer"&gt;Harvard CS50x lecture (watch until 1:58:33)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The three basic steps are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort the left half of the list&lt;/li&gt;
&lt;li&gt;Sort the right half of the list&lt;/li&gt;
&lt;li&gt;Merge sorted halves&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, that's a lot of help... how do we sort the thing?!&lt;/p&gt;

&lt;p&gt;We use a two-pointer system to compare the left and right halves.&lt;/p&gt;

&lt;p&gt;Let's look at the code and then break it down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// base case&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Get the midpoint to split the list into halves&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;midpoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;leftHalf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="nx"&gt;midpoint&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rightHalf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;midpoint&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="cm"&gt;/* 
  Recursive step which further splits halves down into smaller halves
  until the base case
  */&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftHalf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rightHalf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Left half pointer&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l&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="c1"&gt;// Right half pointer&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;r&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="c1"&gt;// List pointer&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&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="c1"&gt;// Compare values to fill list&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
      &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
      &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// This only works when only left half still contains elements&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// This only works when only right half still contains elements&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Break down
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Base case
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// base case&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This base case is crucial since it enables the rest of the recursion steps to work. Once we reach the base case, we can build up our sorted list.&lt;/p&gt;

&lt;p&gt;Let's use a simple list to think about this: &lt;code&gt;[1, 5, 2, 3]&lt;/code&gt;. When we get to the base case, we should have 4 pieces: &lt;code&gt;[1]&lt;/code&gt;, &lt;code&gt;[5]&lt;/code&gt;, &lt;code&gt;[2]&lt;/code&gt;, and &lt;code&gt;[3]&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursive step (first look)
&lt;/h3&gt;

&lt;p&gt;Now, if we look at the initial left half of &lt;code&gt;[1, 5, 2, 3]&lt;/code&gt;, we have &lt;code&gt;[1, 5]&lt;/code&gt;, which is also the step before we get to the base case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftHalf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rightHalf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have our base cases returned, we can begin to sort them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sort
&lt;/h3&gt;

&lt;p&gt;We want to initialize a couple of pointers to help us keep track of elements from each half, and a pointer to keep track of where we are in the unsorted list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Left half pointer&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l&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="c1"&gt;// Right half pointer&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;r&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="c1"&gt;// List pointer&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's sort and merge values into our list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Compare values to fill list&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using our basic example, we are working with &lt;code&gt;[1]&lt;/code&gt; (left half) and &lt;code&gt;[5]&lt;/code&gt; (right half).&lt;/p&gt;

&lt;p&gt;Notice, if we were merging a larger list, we might encounter a scenario where (for argument's sake) all elements from one half go into our list while the other half remains.&lt;/p&gt;

&lt;p&gt;This is how we handle that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These loops only run when the pointer hasn't reached the end of its respective half.&lt;/p&gt;

&lt;p&gt;Now, our &lt;code&gt;list&lt;/code&gt; is equal to &lt;code&gt;[1, 5]&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Return and recursive step (second look)
&lt;/h3&gt;

&lt;p&gt;We return this list to the previous recursive step...&lt;/p&gt;

&lt;p&gt;Notice that while we've sorted &lt;code&gt;[1, 5]&lt;/code&gt; on the left half, the algorithm was also busy sorting the right half, which should be &lt;code&gt;[2, 3]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how our code and values look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftHalf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1,5]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rightHalf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2,3]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we run through our algorithm to sort and merge these two halves into a bigger list which is our final step, because we get &lt;code&gt;[1, 2, 3, 5]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time complexity
&lt;/h2&gt;

&lt;p&gt;The time complexity to split our list into halves all the way down to the base case is logn, and the time complexity to merge them all back together is n, which means the time complexity for merge sort is nlogn.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thoughts
&lt;/h2&gt;

&lt;p&gt;After taking a look at what I've written, I feel like this is what others have written and talked about, also. Haha. But writing this for myself really solidified the idea.&lt;/p&gt;

&lt;p&gt;I hope this made some sense, bare with me as I'm still new at writing blogs, and I was never the best at English class.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Boiled down: packages, npm, webpack, babel, task runner</title>
      <dc:creator>Simon Mei</dc:creator>
      <pubDate>Tue, 07 May 2024 13:34:07 +0000</pubDate>
      <link>https://dev.to/simon_mei_0de03b0b5a3299a/boiled-down-packages-npm-webpack-babel-task-runner-3lfl</link>
      <guid>https://dev.to/simon_mei_0de03b0b5a3299a/boiled-down-packages-npm-webpack-babel-task-runner-3lfl</guid>
      <description>&lt;h2&gt;
  
  
  Packages
&lt;/h2&gt;

&lt;p&gt;JavaScript packages are pieces of code that has been made available to be used by more than one person or group. If you needed a password-hashing program, you may be able to leverage a package called &lt;a href="https://www.npmjs.com/package/bcrypt" rel="noopener noreferrer"&gt;bcrypt&lt;/a&gt;, instead of creating it from scratch yourself. This helps you save time and stops people from reinventing the wheel all the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  npm
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.npmjs.com/about-npm" rel="noopener noreferrer"&gt;npm&lt;/a&gt; is a software registry and package manager. You can use npm to discover packages and to install them in your project.&lt;/p&gt;

&lt;p&gt;npm consists of three distinct components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the website: used to discover packages and to manage profiles&lt;/li&gt;
&lt;li&gt;the CLI: developers can interact with npm through the CLI tool&lt;/li&gt;
&lt;li&gt;the registry: a large public database of JavaScript software and the meta-information surrounding it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why use npm?
&lt;/h3&gt;

&lt;p&gt;Package managers help automate tedious tasks such as navigating to package websites to download and move packages into your projects. &lt;/p&gt;

&lt;p&gt;Imagine you have a project you want to share with others, but it has many large packages that can slow down the data transfer process. With npm, you can specify your packages or "dependencies" in a configuration file (called &lt;code&gt;package.json&lt;/code&gt;) and share that file instead. Using &lt;code&gt;package.json&lt;/code&gt;, others will know exactly which packages and which version your project uses.&lt;/p&gt;

&lt;h2&gt;
  
  
  webpack
&lt;/h2&gt;

&lt;p&gt;Most programming languages provide a way to import code from one file to another, but JavaScript is not inherently able to do so. For security reasons, browsers cannot go into the server's file system to search for modules to use, therefore, we need to load the entire module file into a variable that'll be shared globally.&lt;/p&gt;

&lt;p&gt;That looks like this (moment.js being the package):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="node_modules/moment/min/moment.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="index.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, we can use webpack (also an npm package), a JavaScript module bundler. Popularized by React, webpack allows you to use the &lt;code&gt;require&lt;/code&gt; syntax from node.js to import modules.&lt;/p&gt;

&lt;p&gt;Running the following command installs webpack and the webpack CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install webpack webpack-cli --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--save-dev&lt;/code&gt; saves your packages as a development environment dependency, which will be reflected in the &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Now, running the following command turns your &lt;code&gt;require&lt;/code&gt; statements into legitimate JavaScript code that will have pulled the content from the required file(s):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./node_modules/.bin/webpack index.js --mode=development
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will create a single output file &lt;code&gt;dist/main.js&lt;/code&gt; which is what we use in our HTML file instead of &lt;code&gt;index.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="dist/main.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time we make a change to &lt;code&gt;index.js&lt;/code&gt;, we will need to run the webpack command, but this time, webpack has saved our "entry file" (&lt;code&gt;index.js&lt;/code&gt;) and our "mode" in the file &lt;code&gt;webpack.config.js&lt;/code&gt;. So, now we run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./node_modules/.bin/webpack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bundling saves us time in not having to load external scripts via global variables, using &lt;code&gt;require&lt;/code&gt; in JavaScript instead of adding &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags in HTML, and better performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  babel
&lt;/h2&gt;

&lt;p&gt;Babel is a transpiler which converts modern JavaScript into a backwards compatible version for old environments (browsers, platforms, legacy code bases).&lt;/p&gt;

&lt;p&gt;We can use the ES2015 &lt;code&gt;import&lt;/code&gt; statement instead of &lt;code&gt;require&lt;/code&gt; for loading modules, which is the more common way today. Most modern browsers support ES2015, so it's hard to tell if babel helped, but if you had some ancient browser, you'd be able to find our code has been transpiled.&lt;/p&gt;

&lt;h2&gt;
  
  
  task runner
&lt;/h2&gt;

&lt;p&gt;Using npm's built-in scripting capabilities, we can write some npm scripts to make using webpack easier. We can do so by editing &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"script": {
  "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
  "build": "webpack --progress --mode=production",
  "watch": "webpack --progress --watch"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;npm run build&lt;/code&gt; will run webpack while showing percent progress (--progress), and minimizing the code for production (--mode=production).&lt;/p&gt;

&lt;p&gt;Running &lt;code&gt;npm run watch&lt;/code&gt; will automatically re-run webpack each time any JavaScript file changes.&lt;/p&gt;

&lt;p&gt;npm knows the location of each npm module path, which is why we don't specify the full path for webpack: &lt;code&gt;./node_modules/.bin/webpack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can get another tool that provides a simple web server for live reloading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install webpack-dev-server --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add an npm script to &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"serve": "webpack-dev-server --open"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can run &lt;code&gt;npm run serve&lt;/code&gt; to automatically open &lt;code&gt;index.html&lt;/code&gt; that will rebuild its own bundled JavaScript and refresh the browser automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There are a lot of tools being used here to make web development simpler, but arguably these tools are also overwhelming to use initially.&lt;/p&gt;

&lt;p&gt;Many popular frameworks leverage these tools to make the development process less painful by creating everything mentioned here for you. For example, React's create-react-app or Vues' vue-cli will set up all this stuff for you.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Boiled down: Class in JavaScript</title>
      <dc:creator>Simon Mei</dc:creator>
      <pubDate>Thu, 02 May 2024 18:03:12 +0000</pubDate>
      <link>https://dev.to/simon_mei_0de03b0b5a3299a/boiled-down-class-in-javascript-g43</link>
      <guid>https://dev.to/simon_mei_0de03b0b5a3299a/boiled-down-class-in-javascript-g43</guid>
      <description>&lt;h2&gt;
  
  
  Similarities and differences between Class and Constructor Functions
&lt;/h2&gt;

&lt;p&gt;In JavaScript, classes are similar to constructor functions in that they both provides ways to create objects with prototypal inheritance. Some claim that classes are essentially syntatic sugar for over constructor functions, but there are differences...&lt;/p&gt;

&lt;p&gt;Classes differ from constructor functions in that &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They are labelled by a special internal property called &lt;code&gt;[[IsClassConstructor]]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Class must be called with &lt;code&gt;new&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Class methods are non-enumerable, so we can't use &lt;code&gt;for..in&lt;/code&gt; to retrieve class methods (which we don't usually want anyway)&lt;/li&gt;
&lt;li&gt;Classes always &lt;code&gt;use strict&lt;/code&gt; inside its construct
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User {
  greet() {
    return "Hello, there"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Class Expressions
&lt;/h2&gt;

&lt;p&gt;Classes can be defined inside another expression, passed around, returned, assigned, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Class expression
let User = class {
  greet() {
    return "Heyo";
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Class expressions may also have a name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let User = class CoolClass {
  greet() {
    return CoolClass
  }
}

new User().greet(); // Returns CoolClass definition
console.log(CoolClass) // error, not visible outside of the class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Class fields
&lt;/h2&gt;

&lt;p&gt;Class fields is a feature that allows you to define properties directly on the class itself, instead of within the constructor method. It provides concise and readable code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User {
  name = "Joseph";
  surname = "Joestar";

  greet() {
    console.log(`Hello, this is ${this.name} ${this.surname}`)
  }
}

const user = new User();
user.greet(); // Hello, this is Joseph Joestar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add a &lt;code&gt;#&lt;/code&gt; in front of the variable turns properties into private ones: &lt;code&gt;#name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also create &lt;em&gt;static fields&lt;/em&gt; to define a property that is shared among all instances of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User {
  static type = "Admin";

  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hi, I am ${this.name} and I have ${this.type} permissions`)
  }
}

const user = new User("Joseph");
user.greet(); // Output: Hi, I am Joseph and I have Admin persmissions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Class fields are a relatively new feature in JavaScript and may not be supported across all browsers and JavaScript environments. You may need to use polyfill or a transpiler like Babel to ensure compatibility with older environments.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Boiled down: Getters/setters in JavaScript</title>
      <dc:creator>Simon Mei</dc:creator>
      <pubDate>Thu, 02 May 2024 17:11:28 +0000</pubDate>
      <link>https://dev.to/simon_mei_0de03b0b5a3299a/getterssetters-in-javascript-2569</link>
      <guid>https://dev.to/simon_mei_0de03b0b5a3299a/getterssetters-in-javascript-2569</guid>
      <description>&lt;h2&gt;
  
  
  Security and flexibility with getters and setters
&lt;/h2&gt;

&lt;p&gt;In JavaScript, there are two kinds of object properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data properties:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Joseph&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Joestar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// name and surname are data properties&lt;/span&gt;

&lt;span class="nx"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="c1"&gt;// Joseph&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Accessor properties
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_surname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_surname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_surname&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// properties starting with an underscore are internal&lt;/span&gt;
&lt;span class="c1"&gt;// and should not be touched outside the object&lt;/span&gt;

&lt;span class="c1"&gt;// get name and set name are accessor properties&lt;/span&gt;
&lt;span class="nx"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jolene&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="c1"&gt;// Jolene&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see how data properties allow us to directly access an object's properties, whereas accessor properties provide us &lt;em&gt;getters&lt;/em&gt; and &lt;em&gt;setters&lt;/em&gt; to retrieve and manipulate an object's properties.&lt;/p&gt;

&lt;p&gt;We can even keep the actual value under a different variable name. But why should we use accessor properties? They allow us more control over operations with our objects and safeguard data that we don't want to be directly accessed or manipulated.&lt;/p&gt;

&lt;p&gt;Within our &lt;em&gt;setter&lt;/em&gt;, we can manipulate the data before storing it in our object's property such as validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compatability using getters and settters
&lt;/h2&gt;

&lt;p&gt;If we introduce new properties in the future for an object that may affect other properties, we can modify the accessor properties to make it compatible with our current needs.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Lessons from leetcode: 347 Top K Frequent Elements</title>
      <dc:creator>Simon Mei</dc:creator>
      <pubDate>Fri, 23 Feb 2024 17:21:34 +0000</pubDate>
      <link>https://dev.to/simon_mei_0de03b0b5a3299a/lessons-from-leetcode-347-top-k-frequent-elements-3cb</link>
      <guid>https://dev.to/simon_mei_0de03b0b5a3299a/lessons-from-leetcode-347-top-k-frequent-elements-3cb</guid>
      <description>&lt;h2&gt;
  
  
  For future Simon to look back on...
&lt;/h2&gt;

&lt;p&gt;As a self-taught dev, learning the ins-and-outs of Python usually happens as I am solving problems on &lt;a href="https://leetcode.com/"&gt;leetcode&lt;/a&gt; or writing random programs in &lt;a href="https://replit.com/~"&gt;replit&lt;/a&gt;. This post is more for myself to remember what I've learned. If you're still interested in reading, then I hope this helps you!&lt;/p&gt;

&lt;h2&gt;
  
  
  List Comprehension
&lt;/h2&gt;

&lt;p&gt;I learned the importance of initializing a list of empty lists using list comprehension or a for loop, over simply using multiplication thanks to this &lt;a href="https://leetcode.com/problems/top-k-frequent-elements/description/"&gt;leetcode problem: Top K Frequent Elements.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I created a list of empty lists like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bucket = [[]] * 5 # [[], [], [] ,[] ,[]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And whenever I tried to update one list in &lt;code&gt;bucket&lt;/code&gt;, every other list was updated as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bucket[1].append(5) # [[5], [5], [5], [5], [5]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was scratching my head about this for a while, thinking it was the other parts of my code that was the problem. In reality, it all started with my &lt;code&gt;bucket&lt;/code&gt; initialization. I had essentially created an object &lt;code&gt;[]&lt;/code&gt; that I copied 5 times in &lt;code&gt;bucket&lt;/code&gt;, and thus each &lt;code&gt;[]&lt;/code&gt; was a reference to the first &lt;code&gt;[]&lt;/code&gt; object. Updating one would mean updating all.&lt;/p&gt;

&lt;p&gt;The way around this would be to create a &lt;code&gt;for&lt;/code&gt; loop or to use list comprehension which allows us to write the code more compactly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bucket = []
for _ in range(6):
  bucket.append([])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bucket = [[] for _ in range(6)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bucket sort
&lt;/h2&gt;

&lt;p&gt;Another neat trick I learned from this leetcode problem was to use bucket sort to complete the task. Often times, I am thinking about using the least amount of resources as possible, and I usually run into a dead end doing so.&lt;/p&gt;

&lt;p&gt;Funny thing is that my initial method of solving the problem involved using the same amount of space and more time than the solution I learned from &lt;a href="https://www.youtube.com/watch?v=YPTqKIgVk-k&amp;amp;pp=ygUMbmVldGNvZGUgMzQ3"&gt;neetcode&lt;/a&gt;. So, I really need to focus more about saving time first and optimize space second.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
