<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://chrisnun.es/feed.xml" rel="self" type="application/atom+xml" /><link href="https://chrisnun.es/" rel="alternate" type="text/html" hreflang="en-US" /><updated>2023-07-31T19:55:37-05:00</updated><id>https://chrisnun.es/feed.xml</id><title type="html">Chris Nunes</title><subtitle>My name is Chris, and I'm a student at the University of Texas at Austin. I do web development things.</subtitle><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><entry><title type="html">Beginner Series: Git and Github</title><link href="https://chrisnun.es/beginner-series-git-and-github/" rel="alternate" type="text/html" title="Beginner Series: Git and Github" /><published>2021-03-09T16:15:00-06:00</published><updated>2021-03-09T16:15:00-06:00</updated><id>https://chrisnun.es/beginner-series-git-and-github</id><content type="html" xml:base="https://chrisnun.es/beginner-series-git-and-github/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;Welcome to the Beginner Series! This is a series of posts designed to introduce people to different concepts and technologies that I think are cool or useful.&lt;/p&gt;

&lt;p&gt;This tutorial will give you a brief overview of how to get started with Git. Git is a tool that is used for version control and collaboration, and it is an essential part of working on any large coding project.&lt;/p&gt;

&lt;p&gt;In this guide, we will be going over how to do some basic operations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Creating a new “repository”&lt;/li&gt;
  &lt;li&gt;Downloading your repository to your local machine&lt;/li&gt;
  &lt;li&gt;Making changes to the repository&lt;/li&gt;
  &lt;li&gt;“Pushing” your local changes&lt;/li&gt;
  &lt;li&gt;“Pulling” any remote changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are a lot of features that Git provides, but these are the most basic ones, and are all that you need to get started!&lt;/p&gt;

&lt;h1 id=&quot;table-of-contents&quot;&gt;Table of Contents&lt;/h1&gt;

&lt;p&gt;You can use these links to skip around between different sections in the tutorial.&lt;/p&gt;

&lt;ol class=&quot;table-of-contents&quot;&gt;
  &lt;li&gt;
    &lt;a href=&quot;#prerequisite-install-git-and-create-a-github-account&quot;&gt;Prerequisite: Install Git and create a Github Account&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href=&quot;#step-1-creating-a-repository-on-github&quot;&gt;Creating a Repository on Github&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href=&quot;#step-2-cloning-your-repository&quot;&gt;Cloning Your Repository&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href=&quot;#step-3-finding-your-files&quot;&gt;Finding Your Files&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href=&quot;#step-4-editing-your-files&quot;&gt;Editing Your Files&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href=&quot;#step-5-pushing-your-local-changes-to-git&quot;&gt;Pushing Your Local Changes to Git&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href=&quot;#step-6-pulling-remote-changes-to-your-local-machine&quot;&gt;Pulling Remote Changes to Your Local Machine&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;
        &lt;a href=&quot;#creating-a-remote-change&quot;&gt;Creating a Remote Change&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href=&quot;#pulling-a-remote-change&quot;&gt;Pulling a Remote Change&lt;/a&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;prerequisite-install-git-and-create-a-github-account&quot;&gt;Prerequisite: Install Git and create a Github Account&lt;/h1&gt;

&lt;p&gt;Before we can start working with Git, we need to actually install it on our computer! You can go to &lt;a href=&quot;https://git-scm.com/book/en/v2/Getting-Started-Installing-Git&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; to learn how to install Git on your specific operating system.&lt;/p&gt;

&lt;p&gt;Once you’ve done that step, you should be able to use Git on your computer. You can confirm that this works by typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;&lt;/code&gt; in your terminal.&lt;/p&gt;

&lt;p&gt;You should see output like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git version 2.24.1 (Apple Git-126)&lt;/code&gt;. If you see something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;command not found: git&lt;/code&gt;, then the installation probably did not work and you should try again.&lt;/p&gt;

&lt;p&gt;After this step, we want to create a &lt;a href=&quot;https://github.com/&quot; target=&quot;_blank&quot;&gt;Github&lt;/a&gt; account. Github is a website that lets you host your code and collaborate on projects with others. You can think of it as something like Google Drive, where you can upload your projects, download them, and share projects with others.&lt;/p&gt;

&lt;h1 id=&quot;step-1-creating-a-repository-on-github&quot;&gt;Step 1: Creating a Repository on Github&lt;/h1&gt;

&lt;p&gt;Each Git project is called a “repository”. A repository tracks all the changes that you make to your code, and allows you to use Git features like “pushing” and “pulling”. To create a new repository, go to &lt;a href=&quot;https://github.com/new/&quot; target=&quot;_blank&quot;&gt;github.com/new/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once on that page, you should see this view:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/new-repo.png&quot; alt=&quot;A screen showing fields that you must fill out in order to create a new Git repository&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;These are the steps to create a new repository&lt;/p&gt;

&lt;p&gt;You have to fill out this form to create a new Git repository. You can fill out most of these fields with whatever you want, such as &lt;strong&gt;Repository name&lt;/strong&gt; and &lt;strong&gt;Description&lt;/strong&gt;. However, when creating this first repository, make sure that the &lt;strong&gt;“Add a README file”&lt;/strong&gt; box is checked.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/add-readme.png&quot; alt=&quot;An option called &amp;quot;Add a README file, with the checkbox clicked and selected&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Select the &quot;Add a README file&quot; option&lt;/p&gt;

&lt;p&gt;This will create a single file called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; in your repository, instead of creating a new empty repository. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; is the first thing people see when they view your repository on Github, so it is a great place to put a description of your project, or instructions on how to use it.&lt;/p&gt;

&lt;h1 id=&quot;step-2-cloning-your-repository&quot;&gt;Step 2: Cloning Your Repository&lt;/h1&gt;

&lt;p&gt;Now that our repository is created on Github, we are in an interesting situation. Our repository exists &lt;strong&gt;remotely&lt;/strong&gt; on Github, but it doesn’t exist &lt;strong&gt;locally&lt;/strong&gt; on our personal computer. Imagine a file that exists on Google Drive, but you don’t have on your computer. So, our next step is to download our repository to our local computer.&lt;/p&gt;

&lt;p&gt;When working with Git, downloading/copying a repository to our computer is referred to as &lt;strong&gt;“cloning”&lt;/strong&gt; the repository. From here on out, that is how I will be referring to it.&lt;/p&gt;

&lt;p&gt;To clone our repository from Github, we need to view the repository online. It should already be pulled up in your browser after creating the repository, but if it isn’t, you can go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://github.com/&lt;span class=&quot;nt&quot;&gt;&amp;lt;your_username&amp;gt;&lt;/span&gt;?tab=repositories&lt;/code&gt; and click on a specific repository.&lt;/p&gt;

&lt;p&gt;When you have a specific repository open, you should see this view (with a different repository name and username):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/repo-view.png&quot; alt=&quot;A view of a newly created Git repository. It shows all the files in the repository, as well as the name and description&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Your empty repository!&lt;/p&gt;

&lt;p&gt;This shows you all the files that are in your repository, as well as other information such as the repository name, the description, and other useful things. This is a nice page to go to find any information you need about a repository.&lt;/p&gt;

&lt;p&gt;To clone our repository, all we care about is the green button that says &lt;strong&gt;Code&lt;/strong&gt;. If you click on it, it will open a little window showing you different ways to clone your repository.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/clone-repo.png&quot; alt=&quot;A window showing different ways to clone a Git repository. The options are HTTPS, SSH, or Github CLI&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;You want the url that starts off &quot;https://github.com/your_username/....&quot;&lt;/p&gt;

&lt;p&gt;These options (HTTPS, SSH, and Github CLI) all have different uses, but for this tutorial, we will use HTTPS. To clone your repository, first copy the URL that it shows you in the dialogue box.&lt;/p&gt;

&lt;p&gt;Then, open up a terminal window on your computer and navigate to the directory where you want to clone the repository. For instance, you could navigate to your desktop directory.&lt;/p&gt;

&lt;p&gt;Then, enter the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git clone &lt;span class=&quot;nt&quot;&gt;&amp;lt;github_url&amp;gt;&lt;/span&gt;&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;github_url&amp;gt;&lt;/span&gt;&lt;/code&gt; is the HTTPS url that you copied a moment ago. If everything is set up correctly, the repository will be cloned, and you will see the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/clone-output.png&quot; alt=&quot;A terminal screen showing the output of the git clone command. It shows how many files were downloaded, and that it successfully cloned the repository&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Cloning Git repository from the terminal&lt;/p&gt;

&lt;p&gt;Once that command successfully executes, the repository will be on your computer, and there will be a folder on your computer with all the repository files on it.&lt;/p&gt;

&lt;p&gt;If you know where this repository is located on your computer, you can skip to &lt;a href=&quot;#step-4-editing-your-files&quot;&gt;Step 4&lt;/a&gt;. If not, Step 3 will tell you how to find your repository!&lt;/p&gt;

&lt;h1 id=&quot;step-3-finding-your-files&quot;&gt;Step 3: Finding Your Files&lt;/h1&gt;

&lt;p&gt;If you’re not familiar with Git or using your terminal, your first question will probably be something like “Where did the files get downloaded? How do I find them?”. These are fair questions that we need to resolve before moving on.&lt;/p&gt;

&lt;p&gt;Rather than doing a scavenger hunt to find the files, we’re going to run a command to find where the repository was downloaded. In your terminal where you just cloned the repository, run the command that matches your computer:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MacOS/Linux: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pwd&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Windows: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These commands will both output one line containing the current directory. The current directory also happens to be the directory where we just cloned our repository! We can use this info to find where our repository is in our file system.&lt;/p&gt;

&lt;p&gt;So for example, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pwd &lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd&lt;/code&gt; tell you that you’re in the directory &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Users/Chris/Desktop&lt;/code&gt;, and the name of your repository is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test-repo&lt;/code&gt;, then the path to the repository will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Users/Chris/Desktop/test-repo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once our files are located, we can go to the next step, which is opening our files to edit them.&lt;/p&gt;

&lt;h1 id=&quot;step-4-editing-your-files&quot;&gt;Step 4: Editing Your Files&lt;/h1&gt;

&lt;p&gt;If you’re following this tutorial, you’re probably at least somewhat familiar with writing code. At the very least, you’ve edited a file before. All that we’re doing in this step is editing our README.md file so that we have some changes to push to Git.&lt;/p&gt;

&lt;p&gt;To begin, open the recently cloned README.md file in any kind of text editor. Change your file so that it looks something like this:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gh&quot;&gt;# My first Github Repository!&lt;/span&gt;

I am learning how to use Git and Github, and this is my first repository!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The specifics don’t matter, we just want to make sure it’s different than it was before. &lt;strong&gt;Save your changes&lt;/strong&gt;, and we’re ready for the next step!&lt;/p&gt;

&lt;h1 id=&quot;step-5-pushing-your-local-changes-to-git&quot;&gt;Step 5: Pushing Your Local Changes to Git&lt;/h1&gt;

&lt;p&gt;Right now, we have &lt;strong&gt;local&lt;/strong&gt; changes on our computer, but our &lt;strong&gt;remote&lt;/strong&gt; repository doesn’t have the changes. This is consistent with our Google Drive Analogy. If you download a file from Google Drive and edit it, the version of the file on Google Drive won’t be changed until you re-upload the file.&lt;/p&gt;

&lt;p&gt;So logically, once we’ve made our local changes, the next step is to &lt;strong&gt;“push”&lt;/strong&gt; the changes to the remote version. However, first we need to learn about Git &lt;strong&gt;&lt;em&gt;commits&lt;/em&gt;&lt;/strong&gt;. A Git commit is like a milestone or checkpoint along the timeline of a Git project. When you make a commit, you are encapsulating all the changes that have been made since the previous commit.&lt;/p&gt;

&lt;p&gt;Another important Git concept is &lt;strong&gt;&lt;em&gt;adding&lt;/em&gt;&lt;/strong&gt; files to Git. While Git automatically tracks all the changes you make to your files, it won’t actually care about the changes unless you use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add&lt;/code&gt; command. Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add &lt;span class=&quot;nt&quot;&gt;&amp;lt;filename&amp;gt;&lt;/span&gt;&lt;/code&gt; tells Git that you want to include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;filename&amp;gt;&lt;/span&gt;&lt;/code&gt; file in the next commit.&lt;/p&gt;

&lt;p&gt;With those two concepts in mind, here is how the process of pushing local changes to Github works:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tell Git which changes you want to add, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add &lt;span class=&quot;nt&quot;&gt;&amp;lt;filename&amp;gt;&lt;/span&gt;&lt;/code&gt; command&lt;/li&gt;
  &lt;li&gt;Create a single commit to summarize the changes you have made using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit -m &quot;&lt;span class=&quot;nt&quot;&gt;&amp;lt;message&amp;gt;&lt;/span&gt;&quot;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Finally, push our new commit (and all the changes it contains) to our remote repository using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;example-addcommitpush&quot;&gt;Example Add/Commit/Push&lt;/h3&gt;

&lt;p&gt;We’ll use our changes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; to demonstrate how this works. At this point, your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; file should be updated with more text, like this:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gh&quot;&gt;# My first Github Repository!&lt;/span&gt;

I am learning how to use Git and Github, and this is my first repository!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that the change is done, we should make sure that Git has noticed. In your terminal window, navigate into the git repository. Once there, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git status&lt;/code&gt;. You should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/git-status.png&quot; alt=&quot;A terminal screen showing the output of the git status command. It shows that there is one modified file, and that it is not currently added to Git&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;The modified file is highlighted in red&lt;/p&gt;

&lt;p&gt;As we can see, Git sees that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; has been modified. We can also see that Git says that there are “no changes added to commit”. We can fix this by using the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add README.md&lt;/code&gt;. This command should give back no output (if all goes well).&lt;/p&gt;

&lt;p&gt;Once you have added the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; file, re-run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git status&lt;/code&gt; to see what has changed.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/git-status-2.png&quot; alt=&quot;A terminal screen showing the output of the git status command. It shows that there is still one modified file, and that it is now ready to be committed&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Now that we have used 'git add', the file is green&lt;/p&gt;

&lt;p&gt;Now, our modified &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; is now ready to be committed! To commit the change, we use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit -m &quot;&lt;span class=&quot;nt&quot;&gt;&amp;lt;message&amp;gt;&lt;/span&gt;&quot;&lt;/code&gt; command, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;message&amp;gt;&lt;/span&gt;&lt;/code&gt; is an informative message describing what changes were made.&lt;/p&gt;

&lt;p&gt;To commit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; changes, I would use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit -m &quot;updated README with repo description&quot;&lt;/code&gt;. As you can see, the message doesn’t have to be particularly formal or long, it only has to describe the changes made.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/git-commit.png&quot; alt=&quot;A terminal screen showing the output of the git commit command. It shows that there is one change included in the new commit&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Using 'git commit' with a commit message&lt;/p&gt;

&lt;p&gt;Once you have run the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit&lt;/code&gt; command, we are ready to push our code to the remote repository. This command is simple: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt;. Running this command should give the following output:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/git-push.png&quot; alt=&quot;A terminal screen showing the output of the git push command. It shows many changes were uploaded, and that the operation was a success&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;A lot of nonsense, but no errors!&lt;/p&gt;

&lt;p&gt;If all goes well, our change should be pushed to the remote repository! We can easily verify that the changes are present by opening/refreshing the repository page on Github. Now that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; is changed, so has the repository web page!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/repo-final.png&quot; alt=&quot;A web page showing information about our repository. The description of the repository now shows the changes that we just uploaded&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Our repository shows our changes!&lt;/p&gt;

&lt;p&gt;Woohoo! Let’s review what we’ve done.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Created a repository on Github&lt;/li&gt;
  &lt;li&gt;Cloned the repository to our local machine&lt;/li&gt;
  &lt;li&gt;Edited one of the files in the repository&lt;/li&gt;
  &lt;li&gt;Added the changed file to our next commit using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Created a new commit of our changes using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Pushed our local changes to Github using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On top of all of this, there is one more important skill that we need to learn in order to collaborate using Git. We have learned how to add our local changes and push them to the remote repository, but we don’t know how to &lt;strong&gt;&lt;em&gt;pull&lt;/em&gt;&lt;/strong&gt; remote changes onto our local machine.&lt;/p&gt;

&lt;p&gt;We could do this by re-cloning the repository every time we wanted new changes, but this would cause us to lose any local changes that we hadn’t saved, and it would be very overkill. As it turns out, there is a way to pull &lt;em&gt;only&lt;/em&gt; the changed files from the remote repository, instead of re-cloning the entire repository.&lt;/p&gt;

&lt;h1 id=&quot;step-6-pulling-remote-changes-to-your-local-machine&quot;&gt;Step 6: Pulling Remote Changes to Your Local Machine&lt;/h1&gt;

&lt;p&gt;We can &lt;strong&gt;pull&lt;/strong&gt; changed files onto our local machine by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull&lt;/code&gt; command. This command will download all the changed files in the remote repository to your local repository.&lt;/p&gt;

&lt;p&gt;This command is very useful when working on projects with other people! For example, if you are working on a project with a friend, you can both just push your own code and pull the other’s code easily.&lt;/p&gt;

&lt;p&gt;Fun fact: before I knew how to use Git, my friends and I would always send code back and forth through email or Facebook chat. Git has made collaboration much faster and better!&lt;/p&gt;

&lt;h3 id=&quot;creating-a-remote-change&quot;&gt;Creating a Remote Change&lt;/h3&gt;

&lt;p&gt;In order to actually demonstrate the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull&lt;/code&gt; command, we need changes in our remote repository that do not exist locally. So, we need to edit our remote repository using the Github web interface.&lt;/p&gt;

&lt;p&gt;To do this, view your repository on Github and find the “Edit README” button on the main page (circled in red).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/edit-repo.png&quot; alt=&quot;A web page showing information about our repository. There is a circle around a button that will let you edit your README file.&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Click on the pen icon circled in red&lt;/p&gt;

&lt;p&gt;Click on this button, and it will take you to a screen where you can edit your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; file. For the sake of this example, just change your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; file in some noticeable way.&lt;/p&gt;

&lt;p&gt;For example, I edited my file to look like this:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gh&quot;&gt;# My first Github Repository!&lt;/span&gt;

I am learning how to use Git and Github, and this is my first repository!

I made this change from Github.com! :0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once you’ve edited the text to something you like, scroll down until you find the “Commit Changes” section of the page.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/commit-repo.png&quot; alt=&quot;A dialogue box prompting you to enter a message, and then commit your changes.&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;Add your commit message here&lt;/p&gt;

&lt;p&gt;Just like we had to provide a commit message when using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit -m &quot;&lt;span class=&quot;nt&quot;&gt;&amp;lt;message&amp;gt;&lt;/span&gt;&quot;&lt;/code&gt;, we will also need to provide a message when we make changes online. Once again, the message can be something simple, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Changed README from Github&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once you add your message, click the “Commit Changes” button and your changes will be saved.&lt;/p&gt;

&lt;p&gt;Now that we have a new change on our remote branch, we need to figure out how to get that change on our local branch as well!&lt;/p&gt;

&lt;h3 id=&quot;pulling-a-remote-change&quot;&gt;Pulling a Remote Change&lt;/h3&gt;

&lt;p&gt;Now that we have a change in the remote repository, we need to download, or &lt;strong&gt;&lt;em&gt;pull&lt;/em&gt;&lt;/strong&gt;, the change to our local repository.&lt;/p&gt;

&lt;p&gt;To do this, navigate using your terminal to the git repository that we were working in before. This will be the same folder where we ran the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git clone&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt; commands earlier.&lt;/p&gt;

&lt;p&gt;Once there, we just need to run one command: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull&lt;/code&gt;. This will download all the changes from the remote repository that we do not currently have on our local machine.&lt;/p&gt;

&lt;p&gt;If the command is executed successfully, you will see an output similar to the output from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git clone&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/git-intro/git-pull.png&quot; alt=&quot;A terminal screen showing the output of the git pull command. It shows that there is one newly-modified file, and that is was updated with a new change.&quot; /&gt;&lt;/p&gt;
&lt;p class=&quot;caption&quot;&gt;We can see the new changes highlighted in green&lt;/p&gt;

&lt;p&gt;Now if you open your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; file in a text editor, you’ll see that the file has the new edit that you made on Github!&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Now you know the basics of Git! In your everyday use, you will mainly use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt;. However, there is a massive world of Git uses that we didn’t cover in this tutorial, and you’ll likely run into them in the future when you start collaborating with Git.&lt;/p&gt;

&lt;p&gt;To continue learning, I would recommend putting all of your projects into a Git repository and hosting them on Github! This is a useful way to back up your projects, and also a good way to flex your projects to your friends (and recruiters!)&lt;/p&gt;

&lt;p&gt;On an unrelated note, below is a link to my Github account! Feel free to go follow me and star all of my cool projects 👀.&lt;/p&gt;
&lt;h5 id=&quot;my-github&quot;&gt;&lt;a href=&quot;https://github.com/chrisnunes57&quot; target=&quot;_blank&quot;&gt;My Github&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="git" /><category term="GitHub" /><category term="tutorial" /><category term="beginner-series" /><summary type="html">Introduction</summary></entry><entry><title type="html">Super Speedy Blog Post Workflow</title><link href="https://chrisnun.es/Super-Speedy-Blog-Post-Workflow/" rel="alternate" type="text/html" title="Super Speedy Blog Post Workflow" /><published>2021-01-31T10:21:00-06:00</published><updated>2021-01-31T10:21:00-06:00</updated><id>https://chrisnun.es/Super-Speedy-Blog-Post-Workflow</id><content type="html" xml:base="https://chrisnun.es/Super-Speedy-Blog-Post-Workflow/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Like the archetypal tech nerd, I like to script literally anything that can be done programmatically. I have scripts on my computer to deploy websites, text friends, and show/hide my desktop icons. I even built a system with a servo motor and a reed switch to turn off my light when I left my room.&lt;/p&gt;

&lt;p&gt;All this to say, I like to save time. In this post I’ll be sharing some shortcuts I use to make writing blog posts much faster. This post will be short and sweet, just like my blog post workflow.&lt;/p&gt;

&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;This blog is created using &lt;a href=&quot;https://jekyllrb.com/&quot; target=&quot;_blank&quot;&gt;https://jekyllrb.com/&lt;/a&gt;, which means that each blog post is written in markdown. So, whenever I want to create a new post, I have to create a new markdown file in a Jekyll-specific format. For example, this is a boilerplate markdown file that I used for posting Leetcode solutions.&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;layout&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;post&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
&lt;span class=&quot;na&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;yyyy-mm-dd 12:34&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Solving Leetcode problem &quot;&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;leetcode&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;easy&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;arrays&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;

&lt;span class=&quot;gs&quot;&gt;** Problem goes here **&lt;/span&gt;

&lt;span class=&quot;gu&quot;&gt;##### [Try it on Leetcode](/){:target=&quot;_blank&quot;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All the data at the top within the two “---” lines is post metadata, while the bottom part of the file is where the actual post content goes.&lt;/p&gt;

&lt;p&gt;This would be annoying to type out for each new post, and copying and pasting it from an existing file would take FAR too long.&lt;/p&gt;

&lt;h2 id=&quot;the-solution&quot;&gt;The Solution&lt;/h2&gt;

&lt;p&gt;My goal was to create &lt;strong&gt;one&lt;/strong&gt; terminal command that would create a new markdown file with Jekyll post metadata already in place. In the end, I didn’t accomplish this exact goal, but I got close enough to make it equally seamless.&lt;/p&gt;

&lt;p&gt;The end result I achieved was this: whenever I enter the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt; on my terminal, it will open a new markdown in &lt;a href=&quot;https://typora.io/&quot; target=&quot;_blank&quot;&gt;Typora&lt;/a&gt;, a markdown editor that I like. Then, the markdown template will be automatically copied to my keyboard, so I just have to paste it into the document to get started.&lt;/p&gt;

&lt;p&gt;In the end, the entire process only takes a few seconds from beginning to end:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/blog-workflow-post/process.gif&quot; alt=&quot;A short video showing someone entering a command, then a window popping up on their computer with markdown content in it&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;how-it-works&quot;&gt;How it Works&lt;/h2&gt;

&lt;h4 id=&quot;note-if-you-just-want-the-code-skip-to-the-bottom-for-a-recap&quot;&gt;Note: If you just want the code, skip to the bottom for a recap.&lt;/h4&gt;

&lt;p&gt;The actual programming for this is really short, but it can be confusing if you are new to working with scripts. First, I created a file in my home directory called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post_template.md&lt;/code&gt;. This is the file that will contain the post template that we want to use. I use a slightly more generic template than the one I showed above, so the contents of my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post-template.md&lt;/code&gt; file are:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;layout&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;post&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
&lt;span class=&quot;na&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;yyyy-mm-dd 12:34&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
&lt;span class=&quot;na&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; 
&lt;span class=&quot;na&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;

&lt;span class=&quot;gu&quot;&gt;##### [Try it out](/){:target=&quot;_blank&quot;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I save this file in my home directory, but you can save it wherever you want.&lt;/p&gt;

&lt;p&gt;Next, we need to copy the contents of this file to the keyboard. Doing this programmatically is a two-step operation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt; command to output the contents of the file&lt;/li&gt;
  &lt;li&gt;Use &lt;a href=&quot;https://thoughtbot.com/blog/input-output-redirection-in-the-shell&quot; target=&quot;_blank&quot;&gt;piping&lt;/a&gt; to redirect the output into our clipboard&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To accomplish this on a MacOS terminal, you would run this code: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; ~/post-template.md | pbcopy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; ~/post-template.md&lt;/code&gt; command on its own simply prints out the contents of the file to the terminal. When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;| pbcopy&lt;/code&gt; is appended, it sends the contents of the file to the user’s clipboard, as if they had copied it themselves.&lt;/p&gt;

&lt;p&gt;Note that this command assumes that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post-template.md&lt;/code&gt; file is in the home directory. If the file was in a different location, the command would have to be updated to reflect the new file path.&lt;/p&gt;

&lt;p&gt;Once this is done, we need to open up the Typora application. This is much simpler, and the command is just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; Typora&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we can condense these commands and run both of them in just one line of input. To combine these commands so that they can both be entered on the same line, we simply join them together with a semicolon. So, our new (longer) command is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; ~/post-template.md | pbcopy&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; open &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; Typora&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now if we input that command into a terminal, we will have the template copied to our keyboard and Typora will open. However, this isn’t enough! We don’t want to type out that whole thing every time we want to create a new blog post. Instead, we just want to enter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt; and have the terminal know what we mean.&lt;/p&gt;

&lt;p&gt;We can accomplish this using an &lt;em&gt;alias&lt;/em&gt;. An alias does what the name implies: it takes a command and gives it a different name. This allows us to give simple names to longer or more complicated commands.&lt;/p&gt;

&lt;p&gt;In our case, we want to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; ~/post-template.md | pbcopy&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; open &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; Typora&lt;/code&gt; whenever we enter the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt;. We are &lt;em&gt;aliasing&lt;/em&gt; the longer command and giving it a shorter name.&lt;/p&gt;

&lt;p&gt;To add an alias to your system, you need to modify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.zshrc&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt; file on your system, whichever one it uses. I have a newer Macbook, so I use a zsh shell. Inside of my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.zshrc&lt;/code&gt; file, I added the line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;alias &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'cat ~/post_template.md | pbcopy; open -a Typora'&lt;/span&gt;&lt;/code&gt;. This line is long, so we can break it down.&lt;/p&gt;

&lt;p&gt;An alias always starts with the keywords &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias &lt;span class=&quot;nt&quot;&gt;&amp;lt;alias_name&amp;gt;&lt;/span&gt;&lt;/code&gt;. The placeholder &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;alias_name&amp;gt;&lt;/span&gt;&lt;/code&gt; is the alias that you are giving the longer command. Then, we have the second part of the line: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias &lt;span class=&quot;nt&quot;&gt;&amp;lt;alias_name&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&amp;lt;long_command_1234&amp;gt;'&lt;/span&gt;&lt;/code&gt;. The placeholder &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;long_command_1234&amp;gt;&lt;/span&gt;&lt;/code&gt; represents the longer command that you are aliasing.&lt;/p&gt;

&lt;p&gt;In the end, we create an alias named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt; for our longer command. After reloading our zsh session, we can run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt; and it will copy the template to the keyboard and open typora!&lt;/p&gt;

&lt;h2 id=&quot;recap&quot;&gt;Recap&lt;/h2&gt;

&lt;p&gt;To recap, here were the steps I took to set up this workflow:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a markdown file with the template we want to use for posts.&lt;/li&gt;
  &lt;li&gt;Create a script to copy the template file to our clipboard and open Typora
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; ~/post-template.md | pbcopy&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; open &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; Typora&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Add an alias to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.zshrc&lt;/code&gt; directory
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nb&quot;&gt;alias &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'cat ~/post_template.md | pbcopy; open -a Typora'&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Gain ability to write start a new blog post by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt; in terminal&lt;/li&gt;
  &lt;li&gt;Profit???&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As far as scripts go, this one is tiny, and it really only saves me about 20 seconds of typing every time I start a blog post (which is not often). BUT — I made it, and using it makes me feel like a terminal master.&lt;/p&gt;

&lt;p&gt;Hopefully this gives you great inspiration for your own productivity scripts, or serves as a nice little guide if you’ve never done anything like this before. Either way, I hope you enjoyed, and if you haven’t yet, read my most recent post about how I created an interactive, real-time chess game!&lt;/p&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="scripting" /><category term="terminal" /><category term="shell" /><category term="tutorial" /><summary type="html">Introduction</summary></entry><entry><title type="html">How I Created my Interactive, Real-Time Chess Game</title><link href="https://chrisnun.es/Creating-an-Interactive-Chess-Game/" rel="alternate" type="text/html" title="How I Created my Interactive, Real-Time Chess Game" /><published>2021-01-28T17:12:00-06:00</published><updated>2021-01-28T17:12:00-06:00</updated><id>https://chrisnun.es/Creating-an-Interactive-Chess-Game</id><content type="html" xml:base="https://chrisnun.es/Creating-an-Interactive-Chess-Game/">&lt;p&gt;If you haven’t noticed, if you go to &lt;a href=&quot;https://chrisnun.es/&quot;&gt;chrisnun.es&lt;/a&gt; on a desktop computer, there is a big chess board floating up and down. If you click on the board, you have the option to challenge me to a chess game, which can be played out in real time!&lt;/p&gt;

&lt;p&gt;Conveniently, if you don’t have the time to sit down and play a full game of chess, the website will save your progress and we can resume the game later. At the time of writing this, I have received a few anonymous challenges through the website, but not one person has followed up and played a game 😔.&lt;/p&gt;

&lt;p&gt;This post will give a high-level breakdown of how I created this real-time chess game, in case any chess fans are inspired (😩) and want to do something like this on their own.&lt;/p&gt;

&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;

&lt;p&gt;I have always been pretty into chess! I learned how to play in high school and ended up founding a chess club in my senior year. I took a multi-year break from the game in college, until my passion was re-ignited by &lt;a href=&quot;https://en.wikipedia.org/wiki/The_Queen%27s_Gambit_(miniseries)&quot; target=&quot;_blank&quot;&gt;The Queen’s Gambit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Around the same time, I was redesigning this website and I was in search of something cool/fun/quirky that I could feature on the homepage. This redesign happened to coincide with my rediscovery of chess, so the chessboard seemed like a natural thing to feature on the site.&lt;/p&gt;

&lt;p&gt;From there, it was a natural move to have the game be interactive between myself and website users. After dismissing the idea of a chess A.I. that played at my level (because that is hard), I settled on having the website create a game that I could respond to on my phone.&lt;/p&gt;

&lt;h2 id=&quot;the-idea&quot;&gt;The Idea&lt;/h2&gt;

&lt;p&gt;I wanted this interactive chess game to function the same way that a normal online chess game would. If you have ever used Chess.com, Lichess, or any other chess website or app, you are probably pretty familiar with the flow of events.&lt;/p&gt;

&lt;p&gt;Generally, the first player sends a challenge to the second player, who has the option to accept or decline the challenge. If the second player accepts the challenge, then the game is created and they begin to play. If the second player declines the challenge, then no game is created and the first player has to send a new challenge to try again.&lt;/p&gt;

&lt;p&gt;In this scenario, the website visitor acts as the first player and sends a game challenge to my phone. As the second player, I accept the games and play them on my phone, while the first player plays through the chessboard on the website.&lt;/p&gt;

&lt;h2 id=&quot;tech-stack&quot;&gt;Tech Stack&lt;/h2&gt;

&lt;p&gt;There were a few different libraries/tools that I used in creating the game, mainly:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://chessboardjs.com/&quot; target=&quot;_blank&quot;&gt;chessboard.js&lt;/a&gt; — Provided the chessboard UI&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/jhlywa/chess.js/blob/master/README.md&quot; target=&quot;_blank&quot;&gt;chess.js&lt;/a&gt; — Handled chess logic, mainly determining legal/illegal moves&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lichess.org/api#tag/Board&quot; target=&quot;_blank&quot;&gt;Lichess Board API&lt;/a&gt; — The big one. Was used to communicate game state between my phone and the website&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://expressjs.com/&quot; target=&quot;_blank&quot;&gt;NodeJS w/ Express&lt;/a&gt; — Used as a medium between the Lichess API and the frontend client&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most important one of these was the Lichess API, as it allowed me to communicate moves back and forth between the web client and my phone. The chess games are played through the Lichess service, and the Lichess API is what sends game updates to the frontend.&lt;/p&gt;

&lt;p&gt;The actual structure of the program is bigger than it would seem, so I’m going to devote an entire mini section to it.&lt;/p&gt;

&lt;h2 id=&quot;structure-and-flow&quot;&gt;Structure and Flow&lt;/h2&gt;

&lt;p&gt;Here is a cheesy clip-art diagram of how the game is structured and how the data is passed around:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/chess-post/structure.png&quot; alt=&quot;A diagram depicting how the app is structured. There are several different arrows showing how the website users are connected to the game.&quot; id=&quot;chess-structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In case that diagram was horribly uninstructive, here is a more detailed breakdown of each component of this system.&lt;/p&gt;

&lt;h3 id=&quot;my-phone&quot;&gt;My Phone&lt;/h3&gt;

&lt;p&gt;This part is pretty simple. I have an account on &lt;a href=&quot;https://lichess.org/&quot; target=&quot;_blank&quot;&gt;Lichess.org&lt;/a&gt;, and whenever someone goes to my website and creates a new game, I get a notification from the Lichess app that says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;chrisnunes_website has challenged you to a game&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From there, I just play the game out through that app just as I would any other game. From my perspective, it is as if I am playing a Lichess user named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes_website&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;the-lichess-api&quot;&gt;The Lichess API&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://lichess.org/api#tag/Board&quot; target=&quot;_blank&quot;&gt;Lichess Board API&lt;/a&gt; is what allows me to create the abstraction of a simple chess game for both myself and the client. To the client, it seems like they are simply playing chess against some online opponent, and for me, it seems like I am just playing a normal chess game on my phone. Thanks to the Lichess Board API, both of these can be true.&lt;/p&gt;

&lt;p&gt;The Lichess Board API allows you to programmatically control the actions of a Lichess user, such as creating challenges, sending chat messages, and making moves in a game. To take advantage of this, I created a Lichess user named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes_website&lt;/code&gt; that acts as a proxy for the user playing through the website.&lt;/p&gt;

&lt;p&gt;This means that whenever a user sends me a challenge through the website, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes_website&lt;/code&gt; user on Lichess is really sending me a challenge. And whenever the user is making moves in the game, it is really &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes_website&lt;/code&gt; sending me moves under the hood.&lt;/p&gt;

&lt;p&gt;In essence, each game played on this site is actually a Lichess match between the users &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes_website&lt;/code&gt; (the web user) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes&lt;/code&gt; (me). I control the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes&lt;/code&gt; user through my phone, while the web client and the game server combine to allow the web user to control the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chrisnunes_website&lt;/code&gt; user.&lt;/p&gt;

&lt;h3 id=&quot;the-game-server&quot;&gt;The Game Server&lt;/h3&gt;

&lt;p&gt;This is the messenger between the client and the Lichess API. All of the requests to the Lichess API require an authorization token, which I didn’t want exposed on the frontend. So, I created a game server to act as a go-between and make the authorized requests in private.&lt;/p&gt;

&lt;p&gt;This server sends and receives data with both the client and the Lichess API. From the client, the game server receives requests to create new challenges, as well as moves the user makes in existing games. Whenever the game server receives these messages from the client, it passes them along to the Lichess API with the proper authorization.&lt;/p&gt;

&lt;p&gt;The server is also constantly listening for new game events or updates from the Lichess API. For example, the Lichess API will send moves from my phone to the game server, or send updates about challenges being accepted or declined. In both of these cases, the game server will once again just forward the messages to the client.&lt;/p&gt;

&lt;h3 id=&quot;the-client&quot;&gt;The Client&lt;/h3&gt;

&lt;p&gt;Finally, the client. In this game, the client is synonymous with the website, or the web browser that the user is using. The client is responsible for communicating game moves with the game server and updating the chess interface for the user.&lt;/p&gt;

&lt;p&gt;The client will always be listening for the game server to send it information about new moves, and it will send the game server messages of its own whenever the user makes a move or creates a challenge. The client also utilizes the browser local storage to track any ongoing games the user has. That way, when a user opens the page, the client can immediately know whether there is an existing game for this user or not.&lt;/p&gt;

&lt;p&gt;The second function of the client is to update the graphical interface that the user sees. The board is rendered using &lt;a href=&quot;https://chessboardjs.com/&quot; target=&quot;_blank&quot;&gt;chessboard.js&lt;/a&gt;, and whenever the game server sends the client a new move, the client updates the chessboard accordingly.&lt;/p&gt;

&lt;p&gt;The third function of the client is to provide the user a way to interact with the game. However, the drag-and-drop chessboard provided by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chessboard.js&lt;/code&gt; does not have chess logic included with it by default. So, we use &lt;a href=&quot;https://github.com/jhlywa/chess.js/blob/master/README.md&quot; target=&quot;_blank&quot;&gt;chess.js&lt;/a&gt; to validate moves that the user makes and ensure that they are legal. If the user makes a legal move, the client will send it to the game server, which will in turn use the Lichess API to send it to my phone.&lt;/p&gt;

&lt;h3 id=&quot;recap&quot;&gt;Recap&lt;/h3&gt;

&lt;p&gt;To put it in simple terms: we have a client that the user can interact with to play the chess game. The client sends and receives messages from the game server, which in turn uses the Lichess API to send and receive messages from me, playing on my phone.&lt;/p&gt;

&lt;p&gt;These four components all work together to create a simple effect: a user and I can both be playing the same chess game, at the same time, through two unrelated mediums.&lt;/p&gt;

&lt;h2 id=&quot;something-cool-ndjson&quot;&gt;Something Cool: ndjson&lt;/h2&gt;

&lt;p&gt;Whenever I have worked with realtime applications in the past, I have used tools like Google’s &lt;a href=&quot;https://firebase.google.com/docs/database&quot; target=&quot;_blank&quot;&gt;Firebase Realtime Database&lt;/a&gt; and &lt;a href=&quot;https://reactjs.org/&quot; target=&quot;_blank&quot;&gt;React&lt;/a&gt; to handle the realtime updating for me. This was the first project I have done that used neither of these tools, so I had to learn how to create this realtime effect on my own.&lt;/p&gt;

&lt;p&gt;In the end, my solution was to stream &lt;a href=&quot;http://ndjson.org/&quot; target=&quot;_blank&quot;&gt;ndjson&lt;/a&gt;, or “Newline delimited JSON”. From a server/sender perspective, this was pretty similar to how you normally send responses. The difference is, with streaming data you can send multiple responses to the same request.&lt;/p&gt;

&lt;p&gt;For example, in Express, this code would stream an infinite amount of numbers in response to a request:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;/numbers&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setHeader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;application/json&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is similar to how you would send a normal response, but in this case we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;/code&gt; to send multiple responses. Normally, you would use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;span class=&quot;nx&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;/code&gt; to send a single response.&lt;/p&gt;

&lt;p&gt;However, from the client/receiver’s point of view, it is significantly different. For static data, getting data from an API request and accessing it could be as simple as:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;chrisnun.es/numbers&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, with streaming, you have to worry about constantly listening for new data and processing it. The basic structure is to set up a reader for the endpoint that will always be listening for new data to be sent, like so:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;chrisnun.es/numbers&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;reader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getReader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// listen for more data&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// data is sent as a Buffer, so we must convert to text&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TextDecoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;utf-8&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Even though it made my code more complicated to write, it was really rewarding to figure out a new way of streaming data that is honestly really easy to use. I think in the future I will definitely use streaming to reduce the size of my API calls and implement real-time applications.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This chess game was really fun to build, and I honestly learned a lot from going through the whole development process. Unfortunately, the game is still pretty untested, and there are probably a lot of bugs to clean up in the gameplay. For instance, I have no idea what will happen if the computer user tries to promote a pawn. I don’t even know if they can. The thought keeps me up at night.&lt;/p&gt;

&lt;p&gt;But all in all, I am really happy with how this project turned out, and I think that it was a smashing success. Shoutout again to the Lichess team for putting out great APIs and developer tools, I hope that one day the Chess.com team can do the same! (😳)&lt;/p&gt;

&lt;p&gt;If you want to test out the game, follow the link below and start a game from the homepage! If I don’t respond, I am likely either busy or asleep. BUT, you can come back and check periodically to see if the challenge has been accepted!&lt;/p&gt;

&lt;p&gt;That’s all for now, be prepared for more great content in the future!&lt;/p&gt;

&lt;h5 id=&quot;test-out-the-game&quot;&gt;&lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;Test out the game&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="chess" /><category term="javascript" /><category term="node" /><category term="api" /><category term="tutorial" /><summary type="html">If you haven’t noticed, if you go to chrisnun.es on a desktop computer, there is a big chess board floating up and down. If you click on the board, you have the option to challenge me to a chess game, which can be played out in real time!</summary></entry><entry><title type="html">17. Letter Combinations of a Phone Number</title><link href="https://chrisnun.es/17-Letter-Combinations-of-a-Phone-Number/" rel="alternate" type="text/html" title="17. Letter Combinations of a Phone Number" /><published>2020-07-24T21:17:00-05:00</published><updated>2020-07-24T21:17:00-05:00</updated><id>https://chrisnun.es/17-Letter-Combinations-of-a-Phone-Number</id><content type="html" xml:base="https://chrisnun.es/17-Letter-Combinations-of-a-Phone-Number/">&lt;p&gt;Given a string containing digits from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2-9&lt;/code&gt; inclusive, return all possible letter combinations that the number could represent.&lt;/p&gt;

&lt;p&gt;A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: &quot;23&quot;
Output: [&quot;ad&quot;, &quot;ae&quot;, &quot;af&quot;, &quot;bd&quot;, &quot;be&quot;, &quot;bf&quot;, &quot;cd&quot;, &quot;ce&quot;, &quot;cf&quot;].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although the above answer is in lexicographical order, your answer could be in any order you want.&lt;/p&gt;

&lt;h3 id=&quot;solution&quot;&gt;Solution&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;letterCombinations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mapping&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'c'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'3'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'d'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'e'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'f'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'4'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'g'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'h'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'i'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'5'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'j'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'k'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'l'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'6'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'m'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'o'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'7'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'p'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'q'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'t'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'u'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'v'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'9'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'y'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'z'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;combination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;# check to see if we have more digits to process
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;combination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# we have more numzz
&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapping&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;combination&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:])&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This solution isn’t super complex to code, but if you’re not familiar with recursion, it’ll be complex to understand. I’m not great with recursion myself, but I’ll try to explain (for my own sake). At a basic level, this program goes through every digit provided in the list and iterates through all of their letters.&lt;/p&gt;

&lt;p&gt;This function tracks two things: a current combination of letters that we’re building, and a list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next_digits&lt;/code&gt;, which tracks all of the digits in the given string that we’re yet to add to our current combination. We also have a dictionary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapping&lt;/code&gt;, which provides a mapping between each digit and the letters associated with it.&lt;/p&gt;

&lt;p&gt;We start off the same way that every recursive method does: the base case, where we stop recursing if a condition is met. In this case, we stop calling the method when we have run out of digits to add to the current combination.&lt;/p&gt;

&lt;p&gt;If there are still digits remaining to test, we loop through each letter that the digit is associated with. Then, for each letter, we first remove the current digit from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next_digits&lt;/code&gt; list and then add the current letter to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;combination&lt;/code&gt; variable. Using these updated values, we recur and the same process happens again.&lt;/p&gt;

&lt;p&gt;When the program runs out of digits and letters to process, it adds the current 	&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;combination&lt;/code&gt; to a list of combinations. In the end, we return this list of combinations as our final answer.&lt;/p&gt;

&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;

&lt;p&gt;Was asked a variation of this question on a Whole Foods HackerRank!&lt;/p&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/letter-combinations-of-a-phone-number/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="medium" /><category term="recursion" /><summary type="html">Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.</summary></entry><entry><title type="html">1. Two Sum</title><link href="https://chrisnun.es/1-Two-Sum/" rel="alternate" type="text/html" title="1. Two Sum" /><published>2020-07-10T15:54:00-05:00</published><updated>2020-07-10T15:54:00-05:00</updated><id>https://chrisnun.es/1-Two-Sum</id><content type="html" xml:base="https://chrisnun.es/1-Two-Sum/">&lt;p&gt;Given an array of integers, return &lt;strong&gt;indices&lt;/strong&gt; of the two numbers such that they add up to a specific target.&lt;/p&gt;

&lt;p&gt;You may assume that each input would have &lt;strong&gt;&lt;em&gt;exactly&lt;/em&gt;&lt;/strong&gt; one solution, and you may not use the &lt;em&gt;same&lt;/em&gt; element twice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;solution-1-brute-force-slow&quot;&gt;Solution 1: Brute Force (Slow)&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;twoSum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the most naïve solution that I could think of. We iterate over every number in the given list, and for each number, we combine it with every other value in the list to see if we have a match.&lt;/p&gt;

&lt;p&gt;This solution is both functional and simple to understand, but it isn’t efficient enough for our purposes. It works in order &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n &lt;span class=&quot;err&quot;&gt;*&lt;/span&gt; n)&lt;/code&gt; time, when there is an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt; solution out there.&lt;/p&gt;

&lt;h4 id=&quot;notes&quot;&gt;Notes&lt;/h4&gt;

&lt;p&gt;The return statement at the end, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return(-1, -1)&lt;/code&gt;, is just there as a placeholder, as the problem tells us that we will always have a solution.&lt;/p&gt;

&lt;p&gt;Also, this monstrosity (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enumerate(nums[i+1:], i + 1)&lt;/code&gt;) simply calls the enumerate method with the portion of the list that is after index &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nums[i+1:]&lt;/code&gt;) and tells it to start counting at an index &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i + 1&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;solution-2-one-pass-linear&quot;&gt;Solution 2: One pass, Linear&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;twoSum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;seen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
            
            &lt;span class=&quot;n&quot;&gt;seen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
                
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This solution is roughly the same amount of code, but much more efficient. This solution works by keeping track of the values that we have already seen, as well as their indices. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;seen&lt;/code&gt; variable maps each value to its index in the list.&lt;/p&gt;

&lt;p&gt;To begin, we loop through the list like normal. For each value, we calculate the &lt;em&gt;complementary&lt;/em&gt; value that is would need to reach the target. For example, if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; value is 9, and the current &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; value is 5, then the compliment of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target - n = 4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we have calculated this complimentary value, we check to see if we have already seen it in the list. If we have, then we return the index associated it with our current index. If not, then we add the current value to the list and continue with our iteration.&lt;/p&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/two-sum/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="easy" /><category term="arrays" /><summary type="html">Given an array of integers, return indices of the two numbers such that they add up to a specific target.</summary></entry><entry><title type="html">1290. Convert Binary Number in a Linked List to Integer</title><link href="https://chrisnun.es/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/" rel="alternate" type="text/html" title="1290. Convert Binary Number in a Linked List to Integer" /><published>2020-07-09T14:02:00-05:00</published><updated>2020-07-09T14:02:00-05:00</updated><id>https://chrisnun.es/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer</id><content type="html" xml:base="https://chrisnun.es/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/">&lt;p&gt;Given &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;head&lt;/code&gt; which is a reference node to a singly-linked list. The value of each  node in the linked list is either 0 or 1. The linked list holds the  binary representation of a number.&lt;/p&gt;

&lt;p&gt;Return the &lt;em&gt;decimal value&lt;/em&gt; of the number in the linked list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://assets.leetcode.com/uploads/2019/12/05/graph-1.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: head = [0]
Output: 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: head = [1]
Output: 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 4:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
Output: 18880
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 5:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: head = [0,0]
Output: 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The Linked List is not empty.&lt;/li&gt;
  &lt;li&gt;Number of nodes will not exceed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Each node’s value is either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;solution&quot;&gt;Solution&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getDecimalValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this solution we have an integer, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result&lt;/code&gt;, in which we store our current result. As we loop through each node in the linked list, we take the bit value at each node and update our result accordingly.&lt;/p&gt;

&lt;p&gt;The algorithm goes like this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;For each node in the list
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Left Shift&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result&lt;/code&gt; by 1 bit to make space for the new bit value&lt;/li&gt;
      &lt;li&gt;Set result equal to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result OR new_bit&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Update our current node and move to the next one&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;When we’re finished, return our result.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;notes&quot;&gt;Notes&lt;/h3&gt;

&lt;p&gt;Blog is back! Going to be posting more.&lt;/p&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="easy" /><category term="bit manipulation" /><category term="binary" /><category term="linked list" /><summary type="html">Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.</summary></entry><entry><title type="html">1137. N-th Tribonacci Number</title><link href="https://chrisnun.es/1137-N-th-Tribonacci-Number/" rel="alternate" type="text/html" title="1137. N-th Tribonacci Number" /><published>2020-06-09T22:24:00-05:00</published><updated>2020-06-09T22:24:00-05:00</updated><id>https://chrisnun.es/1137-N-th-Tribonacci-Number</id><content type="html" xml:base="https://chrisnun.es/1137-N-th-Tribonacci-Number/">&lt;p&gt;The Tribonacci sequence Tn is defined as follows:&lt;/p&gt;

&lt;p&gt;T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n &amp;gt;= 0.&lt;/p&gt;

&lt;p&gt;Given &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;, return the value of Tn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: n = 25
Output: 1389537
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 &amp;lt;= n &amp;lt;= 37&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The answer is guaranteed to fit within a 32-bit integer, ie. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;answer &amp;lt;= 2^31 - 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;solution-1-recursion-with-memoization&quot;&gt;Solution 1: Recursion with Memoization&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tribonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tribonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tribonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tribonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This solution is similar to the recursive solution to solving the &lt;a href=&quot;/509-Fibonacci-Number/&quot;&gt;regular fibonacci sequence&lt;/a&gt;. The only difference is that instead of calculating the previous &lt;em&gt;two&lt;/em&gt; values, we calculate the previous three.&lt;/p&gt;

&lt;h3 id=&quot;solution-2-cheese-&quot;&gt;Solution 2: Cheese 🧀&lt;/h3&gt;

&lt;p&gt;There is actually a way to solve this method in order &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(1)&lt;/code&gt; time and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt; space. We do this by manually calculating the first 37 tribonacci numbers and hard-coding them into our solution.&lt;/p&gt;

&lt;p&gt;Note: we use the first 37 values because Leetcode only tests for values in the range of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 &amp;lt;= n &amp;lt;= 37&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tribonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;81&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;149&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;274&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;504&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;927&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1705&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3136&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10609&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;19513&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;35890&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;66012&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;121415&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;223317&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;410744&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;755476&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1389537&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2555757&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4700770&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8646064&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15902591&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;29249425&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;53798080&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;98950096&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;181997601&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;334745777&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;615693474&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1132436852&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2082876103&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/n-th-tribonacci-number/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="easy" /><category term="recursion" /><summary type="html">The Tribonacci sequence Tn is defined as follows:</summary></entry><entry><title type="html">509. Fibonacci Number</title><link href="https://chrisnun.es/509-Fibonacci-Number/" rel="alternate" type="text/html" title="509. Fibonacci Number" /><published>2020-06-09T22:15:00-05:00</published><updated>2020-06-09T22:15:00-05:00</updated><id>https://chrisnun.es/509-Fibonacci-Number</id><content type="html" xml:base="https://chrisnun.es/509-Fibonacci-Number/">&lt;p&gt;The &lt;strong&gt;Fibonacci numbers&lt;/strong&gt;, commonly denoted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F(n)&lt;/code&gt; form a sequence, called the &lt;strong&gt;Fibonacci sequence&lt;/strong&gt;, such that each number is the sum of the two preceding ones, starting from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;. That is,&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N &amp;gt; 1.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Given &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;, calculate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F(N)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;0 ≤ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; ≤ 30.&lt;/p&gt;

&lt;h3 id=&quot;solution-1-basic-recursion&quot;&gt;Solution 1: Basic Recursion&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the simplest possible solution, which uses recursion. The two base cases are if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N == 0&lt;/code&gt; and if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N == 1&lt;/code&gt;. These are the situations in which we have no more work to do, and we can just return the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; is greater that 0 or 1, then we have some extra work to do. We know that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib(N) == fib(N - 1) + fib(N - 2)&lt;/code&gt;, so we return those numbers in our solution.&lt;/p&gt;

&lt;p&gt;This solution works fine and is easy to understand, but if you dig deeper, its inefficiency becomes clear. It’s easier to spot with a visual: here is an image showing the chain of method calls if we try to calculate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib(6)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/fibonacci.jpeg&quot; alt=&quot;A graph showing a lot of method calls&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If we look at the general shape of the graph, we can see that each method call creates two more method calls beneath it. This leads to an exponential growth as we move down the tree, and an exponential time complexity of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(2 ^ n)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also see that many method calls are calculated multiple times. For instance, we calculate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib(3)&lt;/code&gt; three times, and each one creates 4 more methods. If we could simply store the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib(3)&lt;/code&gt;, we could save ourselves a lot of additional computations.&lt;/p&gt;

&lt;p&gt;As slow as this solution may be, it is still good enough to pass the Leetcode test cases. However, that is because they only test our code with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 &amp;lt;= N &amp;lt;= 30&lt;/code&gt;. If we even barely increase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; to 40, our solution is too slow. So, we look for a faster way.&lt;/p&gt;

&lt;h3 id=&quot;solution-2-memoization&quot;&gt;Solution 2: Memoization&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Memoization is basically a fancy word for storing the result of a method call so that we don’t have to re-calculate it later. In this solution, we create a cache to store the result from each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;This brings our time complexity down to something reasonable, and runs much faster.&lt;/p&gt;

&lt;h3 id=&quot;solution-3-bottom-up-dp&quot;&gt;Solution 3: Bottom-Up DP&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;“Bottom-Up” in this case means that, unlike with our recursive solution, we start with the lowest values, 1 and 0. We then build our way up to the desired value of N, which is our final solution.&lt;/p&gt;

&lt;p&gt;This is the most efficient solution we’ve seen so far. It has time complexity of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt;, as well as space efficiency of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt;. However, there is one more solution that works in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(1)&lt;/code&gt; time AND space.&lt;/p&gt;

&lt;h3 id=&quot;solution-4-massive-brain-&quot;&gt;Solution 4: Massive Brain 🤯&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Contributed by LeetCode user mereck.
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;golden_ratio&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
  	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;golden_ratio&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I have no clue how this math actually works, I actually just ripped this solution off of Leetcode. It uses math, so I don’t really understand it. But you can use the Golden Ratio (math thing) to solve this instantly.&lt;/p&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/fibonacci-number/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="easy" /><category term="recursion" /><summary type="html">The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,</summary></entry><entry><title type="html">20. Valid Parentheses</title><link href="https://chrisnun.es/20.-Valid-Parentheses/" rel="alternate" type="text/html" title="20. Valid Parentheses" /><published>2020-05-31T10:44:00-05:00</published><updated>2020-05-31T10:44:00-05:00</updated><id>https://chrisnun.es/20.-Valid-Parentheses</id><content type="html" xml:base="https://chrisnun.es/20.-Valid-Parentheses/">&lt;p&gt;Given a string containing just the characters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'('&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;')'&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'{'&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'}'&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'['&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;']'&lt;/code&gt;, determine if the input string is valid.&lt;/p&gt;

&lt;p&gt;An input string is valid if:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open brackets must be closed by the same type of brackets.&lt;/li&gt;
  &lt;li&gt;Open brackets must be closed in the correct order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note that an empty string is also considered valid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: &quot;()&quot;
Output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: &quot;()[]{}&quot;
Output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: &quot;(]&quot;
Output: false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 4:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: &quot;([)]&quot;
Output: false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 5:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: &quot;{[]}&quot;
Output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;solution&quot;&gt;Solution&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isValid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;mapping&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;(&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;{&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[&quot;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapping&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# we're at an opening bracket
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# we're at closing bracket
&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapping&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
                
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This solution works in ordern &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt; time using one loop and a stack.&lt;/p&gt;

&lt;p&gt;We go through this string one character at a time. If we’re at an opening bracket, there is nothing that we need to do right now, because we’ll come back to it and process it later. So, we add it to the stack and carry on.&lt;/p&gt;

&lt;p&gt;If we arrive at a closing bracket, however, we have to do some checking. We have to make sure that the most recent bracket that we processed matches the current bracket. For example, if we arrive at a closing square bracket &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;]&lt;/code&gt;, then we want to make sure that the last thing we added to the stack is a matching &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt;. If the last bracket doesn’t match, or if there is nothing else in the stack, we know that we have an invalid expression and return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we’ve processed the whole string, we make sure there is nothing left in the stack. If the stack is not empty, then that means we had brackets that were unmatched, and we return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/valid-parentheses/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="easy" /><category term="strings" /><summary type="html">Given a string containing just the characters '(', ')', '{', '}', '[', and ']', determine if the input string is valid.</summary></entry><entry><title type="html">65. Valid Number</title><link href="https://chrisnun.es/65-Valid-Number/" rel="alternate" type="text/html" title="65. Valid Number" /><published>2020-05-28T19:39:00-05:00</published><updated>2020-05-28T19:39:00-05:00</updated><id>https://chrisnun.es/65-Valid-Number</id><content type="html" xml:base="https://chrisnun.es/65-Valid-Number/">&lt;p&gt;Validate if a given string can be interpreted as a decimal number.&lt;/p&gt;

&lt;p&gt;Some examples:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;0&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; 0.1 &quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;abc&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;1 a&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;2e10&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; -90e3  &quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; 1e&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;e3&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; 6e-1&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; 99e2.5 &quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;53.5e93&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; --6 &quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-+3&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;95a54e53&quot;&lt;/code&gt; =&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; It is intended for the problem statement to be ambiguous. You should gather all requirements up front before  implementing one. However, here is a list of characters that can be in a valid decimal number:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Numbers 0-9&lt;/li&gt;
  &lt;li&gt;Exponent - “e”&lt;/li&gt;
  &lt;li&gt;Positive/negative sign - “+”/”-“&lt;/li&gt;
  &lt;li&gt;Decimal point - “.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, the context of these characters also matters in the input.&lt;/p&gt;

&lt;h3 id=&quot;solution-1-regex&quot;&gt;Solution 1: Regex&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;^\s*[+-]?(\d+\.?|\.\d+)\d*(e[+-]?\d+)?\s*$&quot;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For this problem, it seemed like you can go in one of two directions. You could either solve this by looping through the string and keeping track of state and edge cases, or you could use Regex to match a pattern.&lt;/p&gt;

&lt;p&gt;If you don’t know how Regex works, or need to refresh your memory, &lt;a href=&quot;https://regexone.com/&quot;&gt;this&lt;/a&gt; website is a great resource. You can view the solution and test cases for the above Regex at &lt;a href=&quot;https://regex101.com/r/WZ23Iu/1&quot;&gt;this site&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;solution-2-literally-anything-other-than-regex&quot;&gt;Solution 2: Literally anything other than Regex&lt;/h3&gt;

&lt;p&gt;I did not want to take the brainpower to figure out how to actually do this problem, so Regex is as advanced as I’ll get for this one. Maybe if I’m feeling brave one day I’ll finish the problem in a different way.&lt;/p&gt;

&lt;h5 id=&quot;try-it-on-leetcode&quot;&gt;&lt;a href=&quot;https://leetcode.com/problems/valid-number/&quot; target=&quot;_blank&quot;&gt;Try it on Leetcode&lt;/a&gt;&lt;/h5&gt;</content><author><name>Chris Nunes</name><email>chrisnunes57@gmail.com</email></author><category term="leetcode" /><category term="hard" /><category term="regex" /><summary type="html">Validate if a given string can be interpreted as a decimal number.</summary></entry></feed>