<?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: Robby Auziqni</title>
    <description>The latest articles on Forem by Robby Auziqni (@lethalwarrior).</description>
    <link>https://forem.com/lethalwarrior</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%2F477715%2F46bdeffe-77ed-42c2-a5d1-94b8d9a5be34.png</url>
      <title>Forem: Robby Auziqni</title>
      <link>https://forem.com/lethalwarrior</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lethalwarrior"/>
    <language>en</language>
    <item>
      <title>Leetcode - Palindrome Number (Go)</title>
      <dc:creator>Robby Auziqni</dc:creator>
      <pubDate>Sat, 23 Sep 2023 14:20:14 +0000</pubDate>
      <link>https://forem.com/lethalwarrior/leetcode-palindrome-number-go-55bi</link>
      <guid>https://forem.com/lethalwarrior/leetcode-palindrome-number-go-55bi</guid>
      <description>&lt;p&gt;This post is the first in a series of Golang-based answers to some code challenges on &lt;a href="https://leetcode.com/"&gt;Leetcode&lt;/a&gt;. Please note that the solution I provided is not the only solution to this challenge, as there are other ways to solve this challenge. Please write down your alternative solutions and suggestions in the comments if you have one. I'll work on the &lt;a href="https://leetcode.com/problems/palindrome-number/"&gt;&lt;strong&gt;Palindrome Number&lt;/strong&gt;&lt;/a&gt; challenge in this post.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given an integer &lt;code&gt;x&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;x&lt;/code&gt; is &lt;strong&gt;&lt;em&gt;a palindrome&lt;/em&gt;&lt;/strong&gt;, and &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this challenge, we are given an integer &lt;code&gt;x&lt;/code&gt; and tasked to determine whether the &lt;code&gt;x&lt;/code&gt; is &lt;em&gt;&lt;strong&gt;a palindrome&lt;/strong&gt;&lt;/em&gt; or not.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards, such as madam or racecar, the date and time 12/21/33 12:21, and the sentence: "A man, a plan, a canal – Panama".&lt;br&gt;
-- &lt;cite&gt;&lt;a href="https://en.wikipedia.org/wiki/Palindrome"&gt;Wikipedia&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is, to say that if we have &lt;code&gt;x = 121&lt;/code&gt;, then &lt;strong&gt;it is a Palindrome&lt;/strong&gt;, while &lt;code&gt;x = 1234&lt;/code&gt; isn't. Now, One way to solve the challenge is to do these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check whether the &lt;code&gt;x&lt;/code&gt; is a negative number or not, if so, return false.&lt;/li&gt;
&lt;li&gt;Convert the &lt;code&gt;x&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;em&gt;the two pointers&lt;/em&gt; technique, traverse through &lt;code&gt;x&lt;/code&gt; to compare the elements from left and right, and if they don't equal, then return false. Otherwise, return true.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;IsPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;strX&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strconv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Itoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;strX&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;strX&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we implement the first step with this code to determine whether &lt;code&gt;x&lt;/code&gt; is a negative number or not. Doing this will make the function early return and doesn't have to run unnecessary steps after.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;strX&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strconv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Itoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, change the &lt;code&gt;x&lt;/code&gt; from type &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt;, and store it to a new variable &lt;code&gt;strX&lt;/code&gt; because we are gonna loop through it's elements. To do this, we can use one of Golang's &lt;a href="https://pkg.go.dev/strconv"&gt;&lt;code&gt;strconv&lt;/code&gt;&lt;/a&gt; functions, &lt;code&gt;strconv.Itoa()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;strX&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;strX&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, using &lt;em&gt;the two pointers&lt;/em&gt; technique, we traverse the &lt;code&gt;strX&lt;/code&gt;, with &lt;code&gt;i&lt;/code&gt; starting from the first element and &lt;code&gt;j&lt;/code&gt; starting from the last element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PU28S8KS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://drive.google.com/uc%3Fid%3D1ytEWQCfGCedJ31fyUjbB17aO4v4aQy-0" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PU28S8KS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://drive.google.com/uc%3Fid%3D1ytEWQCfGCedJ31fyUjbB17aO4v4aQy-0" alt="first-iteration" width="769" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z-EEhLeI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://drive.google.com/uc%3Fid%3D1_P_-l9t7TYFnLMfXgP2buJ494Fy-3tX9" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z-EEhLeI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://drive.google.com/uc%3Fid%3D1_P_-l9t7TYFnLMfXgP2buJ494Fy-3tX9" alt="second-iteration" width="769" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;i&lt;/code&gt; will increment through the loop, while &lt;code&gt;j&lt;/code&gt; will decrement. Inside the loop we check if they are equal or not, when there is a single occurrence where they aren't equal, then return a false. Otherwise, return true.&lt;/p&gt;

&lt;p&gt;That's all! You can check the code on my GitHub as well as solutions for other challenges &lt;a href="https://github.com/LethalWarrior/leetcode-solutions/tree/master/easy/palindrome-number"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
      <category>go</category>
    </item>
  </channel>
</rss>
