<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sócrates Díaz</title>
    <description>The latest articles on Forem by Sócrates Díaz (@socratesdz).</description>
    <link>https://forem.com/socratesdz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F97605%2F2be12eb0-a6eb-4bda-a7bd-8dd88cb50c94.jpeg</url>
      <title>Forem: Sócrates Díaz</title>
      <link>https://forem.com/socratesdz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/socratesdz"/>
    <language>en</language>
    <item>
      <title>Software is like gardening</title>
      <dc:creator>Sócrates Díaz</dc:creator>
      <pubDate>Mon, 12 Nov 2018 17:14:32 +0000</pubDate>
      <link>https://forem.com/socratesdz/software-is-like-gardening-1bb0</link>
      <guid>https://forem.com/socratesdz/software-is-like-gardening-1bb0</guid>
      <description>&lt;p&gt;Recently I've been reading &lt;em&gt;The Pragmatic Programmer&lt;/em&gt; (for the second time 😁), and I came to one analogy that struck me hard, and changed my whole thinking about software development.&lt;/p&gt;

&lt;p&gt;In the chapter 6, called &lt;em&gt;While you are coding&lt;/em&gt;, page 184, the author says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unfortunately, the most common metaphor for software development is building construction. [...] Rather than construction, software is more like gardening--it is more organic than concrete.[...] You constantly monitor the health of the garden, and make adjustments (to the soil, the plants, the layout) as needed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This just made me realize the contradictions I've been living with and the need to come to terms with software development and its real lifecycle. More often than not, software is not just written and that's it. It evolves over time as needs arise. The code you wrote 6 months ago might be rewritten, and that's fine. Dependencies update, your software changes due to it. Et cetera.&lt;/p&gt;

&lt;p&gt;This may not be news to you as a software developer, but an unpleasant quantity of non-technical people who works with developers, may not understand this and think this is like building construction (as I did before). This is your pitch for them:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Software is not static. Software is alive.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;h6&gt;
  
  
  Credits: Photo by &lt;a href="https://unsplash.com/photos/vrbZVyX2k4I?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Markus Spiske&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/gardening?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;
&lt;/h6&gt;

</description>
      <category>career</category>
      <category>tips</category>
    </item>
    <item>
      <title>Optimizing code with algebra</title>
      <dc:creator>Sócrates Díaz</dc:creator>
      <pubDate>Sun, 30 Sep 2018 03:38:06 +0000</pubDate>
      <link>https://forem.com/socratesdz/optimizing-code-with-algebra-37en</link>
      <guid>https://forem.com/socratesdz/optimizing-code-with-algebra-37en</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O6cCrgbj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c24nlacbohaiz8vrwngt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O6cCrgbj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c24nlacbohaiz8vrwngt.jpg" alt="Photo by Roman Mager on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Photo by Roman Mager on Unsplash
&lt;/h6&gt;

&lt;p&gt;During a semester in college I took a subject called "Discrete Mathematics". The lecturer decided to split the class in groups. Each group consisted of 2 or 3 students. They were meant to do expositions about a particular subject assigned by the lecturer.&lt;/p&gt;

&lt;p&gt;In the group I was on (we were only two, actually), we were assigned with the subject of &lt;em&gt;recurrency relations&lt;/em&gt;. &lt;a href="https://en.wikipedia.org/wiki/Recurrence_relation"&gt;According to Wikipedia&lt;/a&gt;, this is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;an equation that recursively defines a sequence or multidimensional array of values, once one or more initial terms are given: each further term of the sequence or array is defined as a function of the preceding terms.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, a &lt;em&gt;recursive function&lt;/em&gt; ;)&lt;/p&gt;

&lt;p&gt;A perfect situation to explain this concept with code :D&lt;/p&gt;

&lt;h2&gt;
  
  
  Hanoi Towers
&lt;/h2&gt;

&lt;p&gt;Do you remember this toy?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F26Rw_eM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t3dh0kcy1au86mi86eev.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F26Rw_eM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t3dh0kcy1au86mi86eev.jpg" alt="haoni-towers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The famous Hanoi Towers. The game consists of moving every disk from the first pole to the last one (from left to right or viceversa) in the least amount of steps possible. The rules are that a bigger disk cannot be over a smaller one and you can move only one disk at the time. Something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g9Fida9y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mq04i1fxf3thuw4horhc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g9Fida9y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mq04i1fxf3thuw4horhc.gif" alt="hanoi-demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This problem has an algorithm that solves it, it goes like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dz8xVaEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jirw4nz2rll5eua3uyxz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dz8xVaEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jirw4nz2rll5eua3uyxz.png" alt="hanoi-formula"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where &lt;em&gt;n&lt;/em&gt; is the amount of disks in the problem.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's code that
&lt;/h2&gt;

&lt;p&gt;Let's use Python to write that algorithm. It's something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if we have 4 disks...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good. It works. Let's give it a few tries&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;31&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1023&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1099511627775&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks pretty good. Now I know how many moves it will take me if I'm using 40 disks (btw, this is gonna take you 10460 years, never try this at home). But what if I have 7000?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="n"&gt;which&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Previous&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;repeated&lt;/span&gt; &lt;span class="mi"&gt;994&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hanoi&lt;/span&gt;
&lt;span class="nb"&gt;RecursionError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;maximum&lt;/span&gt; &lt;span class="n"&gt;recursion&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt; &lt;span class="n"&gt;exceeded&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;comparison&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh... It seems this is not performant enough. Of course you could "solve" this just increasing the maximum recursion depth in Python (with &lt;code&gt;sys.setrecursionlimit(n)&lt;/code&gt;), but it's a much better idea to optimize this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimizing
&lt;/h2&gt;

&lt;p&gt;There are several ways to solve recurrence relations, one of them is &lt;em&gt;iteration&lt;/em&gt;. It consists of solving the operation step by step and deducting the non-recursive equation from the process. First we try to solve a given value:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--12VMqyWY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1tu014vif32swkw3mrjp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--12VMqyWY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1tu014vif32swkw3mrjp.jpg" alt="hanoi-algorithm-step-by-step"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, from the last line, we can deduce an equivalent equation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BmM46L7h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fsqr8515gwqcvpfaht5y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BmM46L7h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fsqr8515gwqcvpfaht5y.jpg" alt="hanoi-solved"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you notice, &lt;code&gt;t(4) = 8 + 7&lt;/code&gt; is the same as &lt;code&gt;t(4) = 2^(4-1) + (2^(4-1) - 1)&lt;/code&gt;, that's why it's our initial statement. But now we have solved the recurrence relation, let's use that in our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;better_hanoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pfrhuZmf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ntdygfkkefqgrl8zxdj6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pfrhuZmf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ntdygfkkefqgrl8zxdj6.jpg" alt="better_code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we know how many steps will take to solve it with 7000 disks (probably the lifetime of the universe, several times).&lt;/p&gt;

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

&lt;p&gt;We have optimized a small algorithm with concepts of discrete mathematics, this is just to give a little insight on how we can benefit of such simple concepts to build better programs. &lt;/p&gt;

</description>
      <category>coding</category>
      <category>math</category>
      <category>optimization</category>
      <category>python</category>
    </item>
    <item>
      <title>How to teach programming to my 10 years old nephew?</title>
      <dc:creator>Sócrates Díaz</dc:creator>
      <pubDate>Sun, 30 Sep 2018 01:11:50 +0000</pubDate>
      <link>https://forem.com/socratesdz/how-to-teach-programming-to-my-10-years-old-nephew-lp</link>
      <guid>https://forem.com/socratesdz/how-to-teach-programming-to-my-10-years-old-nephew-lp</guid>
      <description>&lt;p&gt;Often my sister and her children come home during the weekend, and my nephew is always around to see what I'm up to. I asked him, "do you want to make your own game?" to which he replied "well, yeah, but I need to learn to code first". And then this question struck me "How do I teach him?".&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>learning</category>
      <category>teaching</category>
    </item>
  </channel>
</rss>
