<?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: michael-2509</title>
    <description>The latest articles on DEV Community by michael-2509 (@michael2509).</description>
    <link>https://dev.to/michael2509</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%2F376292%2F963b56e0-1381-4c54-a840-784a5d861580.png</url>
      <title>DEV Community: michael-2509</title>
      <link>https://dev.to/michael2509</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michael2509"/>
    <language>en</language>
    <item>
      <title>Valid Number</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Fri, 30 Sep 2022 18:27:40 +0000</pubDate>
      <link>https://dev.to/michael2509/valid-number-2fl3</link>
      <guid>https://dev.to/michael2509/valid-number-2fl3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;br&gt;
A valid number can be split up into these components (in order):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A decimal number or an integer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;(Optional) An 'e' or 'E', followed by an integer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A decimal number can be split up into these components (in order):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;(Optional) A sign character (either '+' or '-').&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One of the following formats:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One or more digits, followed by a dot '.'.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One or more digits, followed by a dot '.', followed by one or more digits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A dot '.', followed by one or more digits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An integer can be split up into these components (in order):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;(Optional) A sign character (either '+' or '-').&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One or more digit&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check &lt;a href="https://leetcode.com/problems/valid-number/description/"&gt;leetcode&lt;/a&gt; for full code challenge &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt;&lt;br&gt;
The challenge looked so simple until I started solving it. My initial approach was to check the data type and return the appropriate result whether the conditions were true or false. However, this approach won't work for all type cases, so I had to use another approach which lead to using the regex or regular expression in python which helps to check a specified text pattern.&lt;/p&gt;

&lt;p&gt;With this approach, I was able to check the validity of the input based on the expressions I specified. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Merge k sorted list</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Thu, 29 Sep 2022 16:51:24 +0000</pubDate>
      <link>https://dev.to/michael2509/merge-k-sorted-list-1e10</link>
      <guid>https://dev.to/michael2509/merge-k-sorted-list-1e10</guid>
      <description>&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.&lt;/p&gt;

&lt;p&gt;Merge all the linked-lists into one sorted linked-list and return it.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
  1-&amp;gt;4-&amp;gt;5,
  1-&amp;gt;3-&amp;gt;4,
  2-&amp;gt;6
]
merging them into one sorted list:
1-&amp;gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;4-&amp;gt;5-&amp;gt;6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: lists = []
Output: []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: lists = [[]]
Output: []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;APPROACH&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This would be the first time I would be learning &lt;code&gt;linked list&lt;/code&gt; so this took quite some time to figure out. However, let's jump right in.&lt;/p&gt;

&lt;p&gt;I love this definition of linked list from &lt;strong&gt;freecodecamp&lt;/strong&gt;: &lt;a href="https://www.google.com/amp/s/www.freecodecamp.org/news/introduction-to-linked-lists-in-python/amp/"&gt;Linked Lists&lt;/a&gt; are a data structure that store data in the form of a chain. The structure of a linked list is such that each piece of data has a connection to the next one (and sometimes the previous data as well). Each element in a linked list is called a node&lt;/p&gt;

&lt;p&gt;You can learn more about &lt;a href="https://www.tutorialspoint.com/python_data_structure/python_linked_lists.htm"&gt;Linked lists&lt;/a&gt; here&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aproach&lt;/strong&gt;&lt;br&gt;
Having understood links, I rewrote my algorithm to solve the challenge this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Loop through the k &lt;code&gt;linked-lists&lt;/code&gt; list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Append the node element of the linked list to a new array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If there are no empty list and list length equals one, sort new array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a head node from the sorted array: &lt;br&gt;
this is the first element of the linked list of the next elements of the list would be linked to&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Loop through rest of the sorted array to link the current element to the previous nodes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>COMBINE TWO TABLES</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Wed, 28 Sep 2022 12:48:33 +0000</pubDate>
      <link>https://dev.to/michael2509/combine-two-tables-4nnp</link>
      <guid>https://dev.to/michael2509/combine-two-tables-4nnp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Chalenge&lt;/strong&gt;&lt;br&gt;
Write an SQL query to report the first name, last name, city, and state of each person in the Person table. If the address of a personId is not present in the Address table, report null instead.&lt;/p&gt;

&lt;p&gt;Return the result table in any order.&lt;/p&gt;

&lt;p&gt;Check &lt;a href="https://leetcode.com/problems/combine-two-tables/description/"&gt;leetcode&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt;&lt;br&gt;
In this challenge we are to write a SQL code to read data from two separate tables. let's review some basis of SQL and the SQL statements I  would use.&lt;/p&gt;

&lt;p&gt;SQL is a language used to interact with the database. SQL stands for Structured  Query Language.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;select&lt;/code&gt; clause tells the query what column to read from the data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from&lt;/code&gt; clause tells the query what data to use&lt;/p&gt;

&lt;p&gt;&lt;code&gt;join&lt;/code&gt;clause tells query on additional table from which you would love to pull data&lt;/p&gt;

&lt;p&gt;&lt;code&gt;on&lt;/code&gt; clause specifies a logical statement to combine the table in from and join statement.&lt;/p&gt;

&lt;p&gt;Now that we understand this basis, to solve this challenge, I wrote my query using &lt;code&gt;select&lt;/code&gt; clause to the pull the  firstname, lastname, city and state column. I used the &lt;code&gt;from&lt;/code&gt; clause to specify the table I want to pull data, then used the &lt;code&gt;join&lt;/code&gt;clause to specify the other table I needed to pull data from and finally, &lt;code&gt;on&lt;/code&gt; clause which I used to combine both tables in join.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>datascience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>UTF-8 Validation</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Tue, 27 Sep 2022 22:21:00 +0000</pubDate>
      <link>https://dev.to/michael2509/utf-8-validation-28m6</link>
      <guid>https://dev.to/michael2509/utf-8-validation-28m6</guid>
      <description>&lt;p&gt;Another &lt;a href="https://leetcode.com/problems/utf-8-validation/"&gt;leetCode&lt;/a&gt; challenge on UTF-8 Validation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PROBLEM&lt;/strong&gt;&lt;br&gt;
Given an integer array data representing the data, return whether it is a valid UTF-8 encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters). - You can use the link above to see more details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION&lt;/strong&gt;&lt;br&gt;
This was quite a challenge! let's look at the step to solve it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Loop through the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the binary representation for each integer using the format method that takes in two arguments&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Loop through the first octet sequence to determine if it is a valid UTF-8 encoding, by checking if the character is 1 - 4 bytes Long.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check the next two bit of the sequence if they follow the rules, to check for validity&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rules&lt;/strong&gt;&lt;br&gt;
A character in &lt;strong&gt;UTF8&lt;/strong&gt; can be from &lt;strong&gt;1 to 4 bytes long&lt;/strong&gt;, subjected to the following rules:&lt;/p&gt;

&lt;p&gt;For a &lt;strong&gt;1-byte&lt;/strong&gt; character, the first bit is a 0, followed by its Unicode code.&lt;br&gt;
For an &lt;strong&gt;n-bytes&lt;/strong&gt; character, the first n bits are all one's, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.&lt;/p&gt;

&lt;p&gt;This is how the UTF-8 encoding would work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Number of Bytes   |        UTF-8 Octet Sequence
                       |              (binary)
   --------------------+-----------------------------------------
            1          |   0xxxxxxx
            2          |   110xxxxx 10xxxxxx
            3          |   1110xxxx 10xxxxxx 10xxxxxx
            4          |   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;x denotes a bit in the binary form of a byte that may be either 0 or 1.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Multi-Threaded FizzBuzz</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Mon, 26 Sep 2022 13:10:40 +0000</pubDate>
      <link>https://dev.to/michael2509/multi-threaded-fizzbuzz-1655</link>
      <guid>https://dev.to/michael2509/multi-threaded-fizzbuzz-1655</guid>
      <description>&lt;p&gt;You have the four functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;printFizz&lt;/code&gt; that prints the word &lt;code&gt;"fizz"&lt;/code&gt; to the console,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;printBuzz&lt;/code&gt; that prints the word &lt;code&gt;"buzz"&lt;/code&gt; to the console,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;printFizzBuzz&lt;/code&gt; that prints the word &lt;code&gt;"fizzbuzz"&lt;/code&gt; to the console, and&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;printNumber&lt;/code&gt; that prints a given integer to the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of FizzBuzz will be passed to four different threads:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Thread A: calls &lt;code&gt;fizz()&lt;/code&gt; that should output the word &lt;code&gt;"fizz"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread B: calls &lt;code&gt;buzz()&lt;/code&gt; that should output the word &lt;code&gt;"buzz"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread C: calls &lt;code&gt;fizzbuzz()&lt;/code&gt; that should output the word &lt;code&gt;"fizzbuzz"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread D: calls &lt;code&gt;number()&lt;/code&gt; that should only output the integers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modify the given class to output the series [1, 2, "fizz", 4,  "buzz", ...] where the ith token (1-indexed) of the series is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;"fizzbuzz" if i is divisible by 3 and 5,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"fizz" if i is divisible by 3 and not 5,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"buzz" if i is divisible by 5 and not 3, or&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;i if i is not divisible by 3 or 5.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implement the FizzBuzz class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;FizzBuzz(int n) Initializes the object with the number n that represents the length of the sequence that should be printed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;void fizz(printFizz) Calls printFizz to output "fizz".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;void buzz(printBuzz) Calls printBuzz to output "buzz".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;void fizzbuzz(printFizzBuzz) Calls printFizzBuzz to output "fizzbuzz".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;void number(printNumber) Calls printnumber to output the numbers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: n = 15
Output: [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: n = 5
Output: [1,2,"fizz",4,"buzz"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Logic&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;loop through &lt;code&gt;n&lt;/code&gt; , n times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;check for every value of n ranging from 1 to n, if it mets the condition&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print out the right word.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code Logic&lt;br&gt;
&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,self.n+1):
            if "condition is true":
                "printWord()"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Let's understand this First!&lt;/strong&gt;&lt;br&gt;
Let's assume the order of thread execution is as follows &lt;code&gt;Thread A&lt;/code&gt;, &lt;code&gt;Thread B&lt;/code&gt;, &lt;code&gt;Thread C&lt;/code&gt; and &lt;code&gt;Thread D&lt;/code&gt;. The first step would be to write the logic of the code in the each of methods that run our functions to suit what each method is suppose to print out. &lt;/p&gt;

&lt;p&gt;We loop through n and check if each value of n ranging from 1 to n mets the condition in the method. Unfortunately, Knowing the order of the thread and that Thread A runs first which automatically calls buzz method. &lt;/p&gt;

&lt;p&gt;This would leave us in a complex situtation because Thread A would have to run n number of times before Thread B picks up. Sadly this is not we want to achieve. We want a situation where Thread A pauses after the first iteration permitting Thread B to run and then, Thread C picks up after Thread B's first iteration and same for Thread D. In other words, I want to keep all threads in sync.&lt;/p&gt;

&lt;p&gt;To achieve this, we can use a threading object called &lt;code&gt;barrier&lt;/code&gt;&lt;br&gt;
Think of &lt;a href="https://realpython.com/intro-to-python-threading/#threading-objects"&gt;barrier&lt;/a&gt; as a delay that interupts a thread from continuing it's operation giving room to the next thread to carry out it's function and any other threads before the initial thread picks up from where it's last stop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from threading import Barrier

class FizzBuzz:
    def __init__(self, n: int):
        self.n = n
        self.block = Barrier(4)

    # printFizz() outputs "fizz"
    def fizz(self, printFizz: 'Callable[[], None]') -&amp;gt; None:

        for i in range(1,self.n+1):
            if i%5!=0 and i%3==0:
                printFizz()
            self.block.wait()

    # printBuzz() outputs "buzz"
    def buzz(self, printBuzz: 'Callable[[], None]') -&amp;gt; None:

        for i in range(1,self.n+1):
            if i%3!=0 and i %5 ==0:
                printBuzz()
            self.block.wait()


    # printFizzBuzz() outputs "fizzbuzz"
    def fizzbuzz(self, printFizzBuzz: 'Callable[[], None]') -&amp;gt; None:

        for i in range(1,self.n+1):
            if i%5==0 and i%3==0:
                printFizzBuzz()
            self.block.wait()

    # printNumber(x) outputs "x", where x is an integer.
    def number(self, printNumber: 'Callable[[int], None]') -&amp;gt; None:

        for i in range(1,self.n+1):

            if (i % 3) !=0 and (i%5) !=0:
                printNumber(i)
            self.block.wait()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Understanding the Code&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Initialize barrier to 4, making it understand that 4 threads would be in sync. This would mean all 4 thread must hit the wait
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;self.block = Barrier(4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Starting our Program, Thread A calls Buzz method, The loop begins from 1 then our &lt;code&gt;if&lt;/code&gt; statement is skipped because the condition is false which leads to the next line of code &lt;code&gt;self.bar.wait()&lt;/code&gt;. This prevents &lt;code&gt;buzz&lt;/code&gt; method from running next iteration and giving control to next thread which is &lt;code&gt;Thread B&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# printBuzz() outputs "buzz"
    def buzz(self, printBuzz: 'Callable[[], None]') -&amp;gt; None:

        for i in range(1,self.n+1):
            if i%3!=0 and i %5 ==0:
                printBuzz()
            self.block.wait()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The advantage of this approach is that all threads would remain blocked when they call &lt;code&gt;wait()&lt;/code&gt;until the barrier reaches the specified number of threads waiting to be blocked before they get unblocked.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>thread</category>
      <category>classes</category>
    </item>
    <item>
      <title>Sort Character by frequency</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Sun, 25 Sep 2022 22:14:48 +0000</pubDate>
      <link>https://dev.to/michael2509/sort-character-by-frequency-4pem</link>
      <guid>https://dev.to/michael2509/sort-character-by-frequency-4pem</guid>
      <description>&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;br&gt;
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.&lt;/p&gt;

&lt;p&gt;Return the sorted string. If there are multiple answers, return any of them.&lt;/p&gt;

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

&lt;p&gt;Input: s = "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. &lt;/p&gt;

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

&lt;p&gt;Input: s = "cccaaa" Output: "aaaccc" Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers. Note that "cacaca" is incorrect, as the same characters must be together. &lt;/p&gt;

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

&lt;p&gt;Input: s = "Aabb" Output: "bbAa" Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect. Note that 'A' and 'a' are treated as two different characters. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find the frequencyof each character string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To achieve this, the counter function, item method and bucket in python is used.&lt;/p&gt;

&lt;p&gt;The Counter.items() method helps to see the elements of the list along with their respective frequencies in a tuple. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackabuse.com/bucket-sort-in-python/"&gt;Bucket&lt;/a&gt; Sort is a comparison-type algorithm which assigns elements of a list we want to sort in Buckets, or Bins. The contents of these buckets are then sorted, typically with another algorithm. After sorting, the contents of the buckets are appended, forming a sorted collection&lt;br&gt;
Bucket Sort can be thought of as a scatter-order-gather approach towards sorting a list, due to the fact that the elements are first scattered in buckets, ordered within them, and finally gathered into a new, sorted list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sort in decreasing order&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The string is now sorted in reverse order based on the frequency using the reverse function in python. &lt;br&gt;
 &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>sort</category>
      <category>hash</category>
      <category>bucket</category>
    </item>
    <item>
      <title>Print in Order</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Sat, 24 Sep 2022 22:46:26 +0000</pubDate>
      <link>https://dev.to/michael2509/print-in-order-31do</link>
      <guid>https://dev.to/michael2509/print-in-order-31do</guid>
      <description>&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;br&gt;
Suppose we have a class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().&lt;/p&gt;

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

&lt;p&gt;We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's Understand the challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What an Instance and a Thread?&lt;br&gt;
An object is an instance of a class. In the example below, phone is a class while instance of phone is phone_one. Therefore to create an instance of foo would be to create an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Phone:
    def __init__(self):
        self.brand = "samsung"
        self.model = "A50 
        self.color = "blue"

#object 
phone_one = Phone()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A thread is a set of operations that would be operated at different time sequence. It allow's one perform different actions at different time sequence instead of the order of execution.&lt;br&gt;
let's say you have to wait for an input to complete action one, you can move to the next available action, action two while you await action one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import threading
import time

def eat(food):
    print("preparing to eat",food)
    time.sleep(1)
    print("back to eating ",food)

def movie(movie):
    print("watching", movie)
    print("Done watching")


x = threading.Thread(target=eat, args=("rice",))
x.start()

y = threading.Thread(target=movie, args=("spiderman",))
y.start()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what you expected as output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;preparing to eat rice
back to eating  rice
watching spiderman
Done watching

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actual output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;preparing to eat rice
watching spiderman
Done watching
back to eating  rice
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you observe the flow while running eat function, we out of the eat function to the movie function before the final  execution of eat and this is because we encoutered a delay in eat function&lt;/p&gt;

&lt;p&gt;So to solve this challenge i used a lock object that thread provides. when we have two or more threads access a shared piece of data is called race condition. which is the case in this challenge. Having the same instance of foo pass to the three different threads. &lt;/p&gt;

&lt;p&gt;To solve a race condition, a common method is to used the Lock object that thread  provides. Lock allows one thread to run at a time until the lock is released before next thread runs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import threading
from threading import Lock

class Number:
    def __init__(self):
        self.lock_one = threading.Lock()
        self.lock_two = threading.Lock()
        self.lock_one.acquire()
        self.lock_two.acquire()

    def first(self, num) -&amp;gt; None:
        print("one", num)
        self.lock_one.release()


    def second(self, num) -&amp;gt; None:
        self.lock_one.acquire()
        print("two", num)
        self.lock_two.release()


    def third(self,num) -&amp;gt; None:
        self.lock_two.acquire()
        print("three", num)

  #object      
num_instance = Number()

thread_a = threading.Thread(target=num_instance.third, args=(1,))
thread_a.start()

thread_b = threading.Thread(target=num_instance.second, args=(2,))
thread_b.start()

thread_c = threading.Thread(target=num_instance.first, args=(3,))
thread_c.start()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OUTPUT&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;one 3
two 2
three 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BONUS&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Palindrome</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Fri, 23 Sep 2022 19:10:04 +0000</pubDate>
      <link>https://dev.to/michael2509/palindrome-57jm</link>
      <guid>https://dev.to/michael2509/palindrome-57jm</guid>
      <description>&lt;p&gt;&lt;strong&gt;QUESTION&lt;/strong&gt;&lt;br&gt;
Given an integer x, return true if x is palindrome integer.&lt;/p&gt;

&lt;p&gt;An integer is a palindrome when it reads the same backward as forward.&lt;/p&gt;

&lt;p&gt;For example, 121 is a palindrome while 123 is not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Reverse Integer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compare integer and reversed integer&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CODE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reverse Integer &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The integer x is converted to a string using &lt;a href="https://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=&amp;amp;ved=2ahUKEwjMsID7xqv6AhUuMuwKHd5jC9AQFnoECA8QBQ&amp;amp;url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FFunction%2FtoString%23%3A~%3Atext%3DtoString%2520.%2Cis%2520concatenated%2520with%2520a%2520string.&amp;amp;usg=AOvVaw1W9rsegTar106tHu2zOMf5"&gt;toString()&lt;/a&gt; method and a for loop is applied to convert the string to it's reverse state using the decrementing for loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//convert interger to string
  var str = x.toString()

//reverse string
 let reverseStr = ""
    for(condition){
       //logic to reverse string
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Compare integer and reversed integer string&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The condition statement if else was used to compare interger &lt;code&gt;x&lt;/code&gt; and the reversed string &lt;code&gt;str&lt;/code&gt; using  type coercion, otherwise the condition would return false even if it's a palindrome number. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=&amp;amp;ved=2ahUKEwjIw_i7yqv6AhUHwAIHHaJvBC8QFnoECA8QBQ&amp;amp;url=https%3A%2F%2Fitnext.io%2Fjavascript-type-coercion-implicit-conversion-and-why-to-use-and-avoid-ea9a38ecc777%23%3A~%3Atext%3DWhat%2520is%2520Type%2520Coercion%2520in%2Calso%2520known%2520as%2520type%2520conversion.&amp;amp;usg=AOvVaw3CJToQXclCIIh3NWSVfNKY"&gt;Type coercion&lt;/a&gt;  helps convert one value type to another which make is it easy to compare values of difference types. Since I'm comparing a string and an interger, I used loose equality &lt;code&gt;==&lt;/code&gt; which makes use of type coercion. In this case, string is coerced to an integer before comparism&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//loose equality(using type coercion)
123 == "123" -&amp;gt; true
// strict comparis
123 === "123" -&amp;gt; false`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(condition) {
        return true 
    } else{
        return false
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final Run&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var isPalindrome = function(x) {
    //convert interger to string
    var str = x.toString()

    //reverse string
    //var reverseStr = str.split("").reverse().join("")
    let reverseStr = ""
    for(condition){
       //logic to reverse string
    }

    //check for palindrome
  if(condition) {
        return true 
    } else{
        return false
    }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>How to remove specific element occurrence from an Array.</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Thu, 22 Sep 2022 18:09:54 +0000</pubDate>
      <link>https://dev.to/michael2509/how-to-remove-specific-element-occurrence-from-an-array-4pl3</link>
      <guid>https://dev.to/michael2509/how-to-remove-specific-element-occurrence-from-an-array-4pl3</guid>
      <description>&lt;p&gt;&lt;strong&gt;QUESTION&lt;/strong&gt;&lt;br&gt;
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP&lt;/strong&gt;&lt;br&gt;
Since we want to remove all occurrences of a particular value from the array, we can use this approach &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Loop through the array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;while(condition){}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The while method reiterates sets of action within the curly braces until conditions are met or becomes false.&lt;/p&gt;

&lt;p&gt;For the condition to remain true, the while loop checks if the integer value can be found in the array, otherwise the condition becomes false and the loop is exited. So I used the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes"&gt;includes&lt;/a&gt; method on the array to achieve this.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Remove the specific integer val that occurs in array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;array.splice("start", "deleteCount")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To remove ocurrences of the specific value, i used the splice array method and indexOf method to create the logic to remove the values.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice"&gt;splice&lt;/a&gt; takes in a first argument(start) which in this case would be the index of the integer value I want to remove and a second value(deleteCount) which I specified to be "1" so just a single element is remove from the array begining from (start)&lt;/p&gt;

&lt;p&gt;To determine the (start) value, I used the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf"&gt;indexOf&lt;/a&gt; method. &lt;code&gt;indexOf&lt;/code&gt; returns the index of the first occurrence of a specific value within the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How the Code Runs&lt;/strong&gt;&lt;br&gt;
The while loop checks if the integer value can be found in the array nums. If true, it go ahead to remove the first occurrence of the integer value from the array using splice and indexOf logic, this process repeats itself until the while condition is false.&lt;/p&gt;

&lt;p&gt;Final code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var removeElement = function(nums,
val){ 
    //loop through array 
   while (condition){ 
   //remove element from the array using splice method.
  nums.splice("start",1); } 
    return nums.length; 
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;BONUS&lt;/strong&gt;&lt;br&gt;
If you ever wonder how this can be applied to a real project.&lt;/p&gt;

&lt;p&gt;You can use this approach to filter out specific genre from a list of movie genre on a movie website. Allowing user to remove specific genre they no longer want to see on movies page.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Removing duplicates</title>
      <dc:creator>michael-2509</dc:creator>
      <pubDate>Wed, 21 Sep 2022 22:52:15 +0000</pubDate>
      <link>https://dev.to/michael2509/removing-duplicates-30c4</link>
      <guid>https://dev.to/michael2509/removing-duplicates-30c4</guid>
      <description>&lt;p&gt;Challenge&lt;br&gt;
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.&lt;/p&gt;

&lt;p&gt;Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.&lt;/p&gt;

&lt;p&gt;Return k after placing the final result in the first k slots of nums.&lt;/p&gt;

&lt;p&gt;Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
The initial approach I took was the  filter and indexOf method on the array to sort the unique elements&lt;/p&gt;

&lt;p&gt;However this approach gave me slight challenge so I had to think a little hard to create a new algorithm to solve it.&lt;/p&gt;

&lt;p&gt;Algorithm&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Check the first two arrays for duplication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if they are duplicates, replace the second array index with the next array index&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if they are not duplicates, leave arrays the same&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
