<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Leandre</title>
    <description>The latest articles on DEV Community by Leandre (@dreking).</description>
    <link>https://dev.to/dreking</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%2F569655%2Fab608df0-420f-4753-9b27-e5aeb36159b8.jpeg</url>
      <title>DEV Community: Leandre</title>
      <link>https://dev.to/dreking</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dreking"/>
    <language>en</language>
    <item>
      <title>The IPv4 address validation algorithm</title>
      <dc:creator>Leandre</dc:creator>
      <pubDate>Sun, 07 Feb 2021 16:33:28 +0000</pubDate>
      <link>https://dev.to/dreking/the-ipv4-address-validation-algorithm-b3h</link>
      <guid>https://dev.to/dreking/the-ipv4-address-validation-algorithm-b3h</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;We are implementing an IPv4 validation algorithm to check whether a string input is a valid IPv4 address.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is an IPv4 Address
&lt;/h1&gt;

&lt;p&gt;IPv4 addresses are usually represented in dot-decimal notation, consisting of four decimal numbers, each ranging from 0 to 255, separated by dots(.). Each part represents a group of 8 bits (an octet) of the address.&lt;br&gt;
Example: 172.16.254.1&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;IPv4 has 4 parts that are separated by a dot(.). Each part is a numerical value ranging from 0 to 255. &lt;br&gt;
We don't care about the IP address classes, we only care about a valid IPv4 address&lt;/p&gt;

&lt;h2&gt;
  
  
  Here is our solution implemented in JavaScript:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function validateIpAddress(ipaddress) {
    if (!ipaddress) return false;

    const ipArray = ipaddress.split('.');
    if (!Array.isArray(ipArray) || ipArray.length !== 4) return false;

    const invalidIPArray = ipArray.filter((octet) =&amp;gt; {
        const number = parseInt(octet);
        return isNaN(number) || number &amp;lt; 0 || number &amp;gt; 255;
    });

    if (invalidIPArray.length) return `Wrong octet: ${invalidIPArray}`;

    return true;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Breakdown
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: In algorithms, it's always the best approach to check for falsy values to eliminate unwanted errors. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Let's split the string argument by dot(.) using &lt;code&gt;split function&lt;/code&gt; which returns an array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: We check the array length as it must be composed of 4 parts. If the array length different from 4, you guessed it!! It's invalid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Filter the array &lt;code&gt;filter function&lt;/code&gt; performing these 2 important tasks: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parse array value to an integer value&lt;/li&gt;
&lt;li&gt;Check and return the value that is not ranging between 0-255, or if the value failed while converting to an integer that returns NaN.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Check the returned filtered array length, if the length is not 0, you guessed it!! it's an invalid IP. We return the invalid parts of the given IP address.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;filter()&lt;/strong&gt; method creates an array filled with all array elements that pass a test/condition specified&lt;br&gt;
&lt;strong&gt;split()&lt;/strong&gt; method is used to split a string into an array of substrings and returns the new array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Feel free to contribute and improve this approach.&lt;br&gt;
Thank you for reading the blog post. Subscribe for more articles&lt;br&gt;
Contributor: &lt;a href="https://dev.to/adeogratias"&gt;Deogratias&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ADeogratias/programming_is_fun/blob/master/check_ip_address.js"&gt;Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
    <item>
      <title>fizz buzz implementation</title>
      <dc:creator>Leandre</dc:creator>
      <pubDate>Mon, 01 Feb 2021 11:35:43 +0000</pubDate>
      <link>https://dev.to/dreking/fizz-buzz-implementation-4h3f</link>
      <guid>https://dev.to/dreking/fizz-buzz-implementation-4h3f</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;We are going to code the famous fizz buzz game. An easy coding interview question that is often used by companies to see if a programmer they want to hire really knows how to code.&lt;br&gt;
It is very famous and we felt like we should consider working on it and trying different approaches to it.&lt;br&gt;
Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by 3 with the word "fizz", any number divisible by 5 with the word "buzz", and any number divisible by 3 and 5.&lt;/p&gt;

&lt;p&gt;Example: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, ...&lt;/p&gt;
&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;Let's first try the naive approach:&lt;br&gt;
`&lt;br&gt;
def fizzbuzz_naive_approach():&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, 101):

    if i%3 == 0:
        print('fizz')

    if i%5 == 0:
        print('buzz')

    if i%3 and i%5 != 0:
        print(i)

return 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The problem with this approach is that for multiples of both 3 and 5 it prints fizz on one row and buzz on the row below.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's try to improve this: &lt;br&gt;
`&lt;br&gt;
def fizzbuzz_naive_approach2():&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, 101):

    if i%3 == 0 and i%5 != 0:
        print('fizz')

    if i%5 == 0 and i%3 != 0:
        print('buzz')

    if i%3 == 0 and i%5 == 0:
        print('fizzbuzz')

    if i%3 != 0 and i%5 != 0:
        print(i)

return 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The naive approach is improved but still is difficult to read and maintain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are going to try and improve the codes implemented above: &lt;br&gt;
`&lt;br&gt;
def fizzbuzz_approach3():&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, 101):

    if i%3 == 0 and i%5 == 0:
        print('fizzbuzz')

    elif i%3 == 0:
        print('fizz')

    elif i%5 == 0:
        print('buzz')

    else:
        print(i)

return 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The implementation above is working, but still has more room for improvement. Wondering how to improve this if it is working?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;`&lt;br&gt;
def fizzbuzz_approach4():&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1 ,101):
    output = ""
    if i%3==0: output += "fizz"
    if i%5==0: output += "buzz"

    if output == "": output = i

    print(output)

return 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;/p&gt;
&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;We are now satisfied with the current approach. &lt;br&gt;
Thank you for walking through the different fizz buzz approaches&lt;/p&gt;
&lt;h1&gt;
  
  
  Bonus
&lt;/h1&gt;

&lt;p&gt;We created a dictionary to help us loop through any multiple we might be interested in, and the good thing about this implementation is that you do not have to change any code in case you add more multiples to the game. You just have to add the dictionary and you are done with any additional multiple.&lt;br&gt;
`&lt;br&gt;
def fizzbuzz_approach5():&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;multiples_dict = {3:"fizz", 5:"buzz", 7:"bazz"}

for i in range(1 ,101):
    output = ""
    for j in multiples_dict:
        if i%j==0: output += multiples_dict[j]

    if output == "": output = i

    print(output)

return 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;`&lt;br&gt;
&lt;strong&gt;Remember there is always room to improve your code&lt;/strong&gt;&lt;br&gt;
Thank you for reading the blog post. Subscribe for more articles &lt;br&gt;
&lt;a href="https://github.com/ADeogratias/programming_is_fun"&gt;Link of the code&lt;/a&gt;&lt;br&gt;
Credit to &lt;/p&gt;
&lt;div class="ltag__user ltag__user__id__571387"&gt;
  
    .ltag__user__id__571387 .follow-action-button {
      background-color: #61122f !important;
      color: #ffffff !important;
      border-color: #61122f !important;
    }
  
    &lt;a href="/adeogratias" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bwbxnvx3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--j5yovGqD--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/571387/2544c455-cff8-468a-86d2-be7ca9f1569c.png" alt="adeogratias image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/adeogratias"&gt;ADeogratias&lt;/a&gt;
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/adeogratias"&gt;/adeogratias&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>python</category>
    </item>
  </channel>
</rss>
