<?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: friendlybytes</title>
    <description>The latest articles on DEV Community by friendlybytes (@mawulijo).</description>
    <link>https://dev.to/mawulijo</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%2F183694%2F36688b35-b815-48fb-a542-be554757b81c.jpeg</url>
      <title>DEV Community: friendlybytes</title>
      <link>https://dev.to/mawulijo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mawulijo"/>
    <language>en</language>
    <item>
      <title>Interviewcake - HiCal Solution in Golang</title>
      <dc:creator>friendlybytes</dc:creator>
      <pubDate>Wed, 02 Dec 2020 17:30:52 +0000</pubDate>
      <link>https://dev.to/mawulijo/interviewcake-hical-solution-in-golang-3eaj</link>
      <guid>https://dev.to/mawulijo/interviewcake-hical-solution-in-golang-3eaj</guid>
      <description>&lt;p&gt;Your company built an in-house calendar tool called HiCal. You want to add a feature to see the times in a day when everyone is available.&lt;/p&gt;

&lt;p&gt;To do this, you’ll need to know when any team is having a meeting. In HiCal, a meeting is stored as an object of a Meeting class with integer variables startTime and endTime. These integers represent the number of 30-minute blocks past 9:00am.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breakdown
&lt;/h2&gt;

&lt;p&gt;What if we only had two ranges? Let's take:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Meeting&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Meeting&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;These meetings clearly overlap, so we should merge them to give:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Meeting&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But how did we know that these meetings overlap?&lt;/p&gt;

&lt;p&gt;We could tell the meetings overlapped because the end time of the first one was after the start time of the second one! But our ideas of "first" and "second" are important here—this only works after we ensure that we treat the meeting that starts earlier as the "first" one.&lt;/p&gt;

&lt;p&gt;How would we formalize this as an algorithm? Be sure to consider these edge cases:&lt;/p&gt;

&lt;p&gt;The end time of the first meeting and the start time of the second meeting are equal. For example: &lt;code&gt;[Meeting(1, 2), Meeting(2, 3)]&lt;/code&gt;&lt;br&gt;
The second meeting ends before the first meeting ends. For example: &lt;code&gt;[Meeting(1, 5), Meeting(2, 3)]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is my solution in Go&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



</description>
      <category>go</category>
      <category>quiz</category>
    </item>
    <item>
      <title>Simplifying a Laborious Task With Golang</title>
      <dc:creator>friendlybytes</dc:creator>
      <pubDate>Mon, 04 May 2020 13:14:35 +0000</pubDate>
      <link>https://dev.to/mawulijo/simplifying-a-laborous-task-with-golang-32ag</link>
      <guid>https://dev.to/mawulijo/simplifying-a-laborous-task-with-golang-32ag</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
In this post, I am going explain how I used Golang to simplify a daunting task at work.&lt;/p&gt;

&lt;p&gt;Before I start, I assume you have basic knowledge in SQL like me :-)&lt;/p&gt;

&lt;p&gt;Lets take a look at this query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT [circuit_id],[unique_id],[unique_number],[created_user],
[created_date],[last_edited_user],[last_edited_date],[object_id]
FROM [&amp;lt;DB_NAME&amp;gt;].[dbo].[&amp;lt;TABLE_NAME&amp;gt;]
WHERE
unique_id='1111'OR 
unique_id='2934'OR
unique_id='3150'OR
unique_id='3640'
### thousands of unique_id ###
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Obviously, it is a simple query, provided you have a few number unique IDs. But unfortunately, the “god of few numbers” never blesses me in such a situation. It is always the other way round :(&lt;/p&gt;

&lt;p&gt;The QA team for a project I happen to part of always demands reports like these mostly with thousands of IDs (unique_id in the query above) in an excel file.&lt;/p&gt;
&lt;h2&gt;
  
  
  How I did it
&lt;/h2&gt;

&lt;p&gt;I would copy the column with the unique IDs in the file and paste them in an editor&lt;br&gt;
Put the right condition “OR unique_id=‘id_number’” around every unique id (id_number in this case) by way of using alt, command and arrow keys combo to position the cursor at several places at the same time. and type the condition.&lt;br&gt;
Quite better than doing it one by one I guess. But bear in mind, my finger has to on the appropriate arrow key till I reach the end of the file and not to mention that these IDs come with various lengths so consistency of the cursor position is not guaranteed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cause to change my ways
&lt;/h2&gt;

&lt;p&gt;So, out of frustration and the resolution to make golang part of me, I decided to automate this laborious task to a large extent. I wrote a tinie tiny program in Go to help me out.&lt;/p&gt;

&lt;p&gt;Here is the program structure&lt;/p&gt;

&lt;p&gt;|- main.go&lt;br&gt;
|&lt;br&gt;
|- uniqueIds.txt&lt;br&gt;
|&lt;br&gt;
|- queryFile.sql ( created by our code )&lt;/p&gt;

&lt;p&gt;Here is main.go with explanations below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let's see what is happening here.&lt;/p&gt;

&lt;p&gt;Inside of &lt;code&gt;createQueryFile()&lt;/code&gt; is where all the action happens. On line 20, we create a file called queryFile.sql using the Create() method provided by os package. We also check for errors whiles doing so just incase anything goes wrong.&lt;/p&gt;

&lt;p&gt;On &lt;code&gt;line 26&lt;/code&gt;, we open the queryFile.sql file in append and read only mode and on line 31, we write the the beginning part of our sql query stored in the pQueryString variable declared in line 13 to the file.&lt;/p&gt;

&lt;p&gt;On &lt;code&gt;line 34&lt;/code&gt;, we open the file uniqueIds.txt in read write mode but defer its closure since we will be reading from it and we don’t want any premature closure till we are done reading its contents. This file contains all the unique IDs pulled from the excel file. We store it in the IDsFile variable and read its contents starting from &lt;code&gt;line 42&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since we want to read the contents of the file line by line, next is to scan the uniqueIds.txt file. We make it “scannable” using the NewScanner() method provided by the bufio package by passing the IDsFile variable as our argument since it is I/O capable. We then use the Scan() method which returns a boolean, – true if content exists and false otherwise, we pick the first unique id with the if condition on &lt;code&gt;line 44&lt;/code&gt; and append it to the queryFile.sql in line 45 using the q variable declared on &lt;code&gt;line 26&lt;/code&gt;. This is because q returns a pointer the queryFile.sql file – thus its “memory location” where we can alter its contents.&lt;/p&gt;

&lt;p&gt;We move on at line 48 with an infinite loop to append the remaining IDs in a formatted way (here OR unique_id='XXXX') with the help of of &lt;code&gt;Fprintln&lt;/code&gt; from the &lt;code&gt;fmt&lt;/code&gt; package. Remember &lt;code&gt;Scan()&lt;/code&gt; returns a bool. So the loop breaks when it reaches the end of the file.&lt;/p&gt;

&lt;p&gt;In doing all this, we also use the opportunity to get rid of all white spaces surrounding our unique IDs in the &lt;code&gt;uniqueIds.txt&lt;/code&gt; file using &lt;code&gt;TrimSpace()&lt;/code&gt; method from the strings package.&lt;/p&gt;

&lt;p&gt;Just like all SQL queries, we terminate ours with &lt;code&gt;line 56&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We then invoke our function in &lt;code&gt;line 60&lt;/code&gt; inside the main function so that it gets executed anytime we run the program with the famous &lt;code&gt;go run main.go&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;With this program, all I now have to do is provide the &lt;code&gt;uniqueIds.txt&lt;/code&gt; file with IDs each on a separate line and I get back to learning Go ASAP. ;-)&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
To end this post, I would like stress on the fact that Go is easy to learn and small enough to fit into the programmers mind. I had fun doing this as I didn’t expect it would be this simple with. I am now ever prepared for my QA team.&lt;/p&gt;

&lt;p&gt;I hope you find this useful and give Golang a try in solving those daunting tasks you run away from.&lt;/p&gt;

&lt;p&gt;Thanks for your time. And oh did I mention everything was done using the standard library? Obviously. SOLID Go&lt;/p&gt;

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