<?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: tomasmor42</title>
    <description>The latest articles on DEV Community by tomasmor42 (@tomasmor42).</description>
    <link>https://dev.to/tomasmor42</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%2F263009%2F46a91f30-94d2-4163-9188-53274d2f8304.jpeg</url>
      <title>DEV Community: tomasmor42</title>
      <link>https://dev.to/tomasmor42</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tomasmor42"/>
    <language>en</language>
    <item>
      <title>Leetcode marathon day 2 </title>
      <dc:creator>tomasmor42</dc:creator>
      <pubDate>Sun, 05 Apr 2020 09:28:29 +0000</pubDate>
      <link>https://dev.to/tomasmor42/leetcode-marathon-day-2-5234</link>
      <guid>https://dev.to/tomasmor42/leetcode-marathon-day-2-5234</guid>
      <description>&lt;p&gt;The task of the second day is the task of finding a happy number: &lt;/p&gt;

&lt;p&gt;A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.&lt;/p&gt;

&lt;p&gt;First I was thinking (as usual) to try to implement it straightforwardly. Let's create a function that gives us the next iteration for a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    def square_sum(n):
        return sum([int(i)**2 for i in str(n)])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At first, I was thinking that if we have a number with one digit it would be the end of the calculations and implemented it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n &amp;lt; 10:
            return (n == 1)
        else:
            res = self.square_sum(n)
            return self.isHappy(res)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But quite fast I realised that this is not correct. Because if we have 7, for example, we will receive on the next step 49, which is not one digit.&lt;br&gt;
But if we take another number as a threshold, like 3, it might work. But we actually don't need 3 only because &lt;code&gt;3^2 = 9&lt;/code&gt; which is a one-digit number.  We could use a higher threshold if it's not a happy number. And the lowest happy number is 7. So the final version is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n &amp;lt; 7:
            return (n == 1)
        else:
            res = self.square_sum(n)
            return self.isHappy(res)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's not in any way an optimal solution, but it's working and according to the benchmarks it's working pretty well. &lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Leetcode marathon and day 1 </title>
      <dc:creator>tomasmor42</dc:creator>
      <pubDate>Fri, 03 Apr 2020 20:11:13 +0000</pubDate>
      <link>https://dev.to/tomasmor42/leetcode-marathon-and-day-1-31ha</link>
      <guid>https://dev.to/tomasmor42/leetcode-marathon-and-day-1-31ha</guid>
      <description>&lt;p&gt;I was never a big fan of classical interview-tasks. Like, find the minimal size sub-array multiplied elements of which give a number sum of digits of which is a palindrome. &lt;br&gt;
But these tasks for better or for worse are part of interviewing culture. And if I ever want to work in a big company (and currently not only big companies are using "smart" problems for an interview process) I probably will need to learn how to solve them. Or at least don't freak out I see them. &lt;br&gt;
So I decided to participate in the Leetcode marathon. The marathon started on the 1st of April with compatibly easy tasks and during the month they suppose to be more complicated. &lt;/p&gt;

&lt;p&gt;I'm a Python developer so all the tasks I'm going to implement in Python. &lt;/p&gt;

&lt;p&gt;The first task was to find the first day was the following: &lt;/p&gt;

&lt;p&gt;Given a non-empty array of integers, every element appears twice except for one. Find that single one.&lt;/p&gt;

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

&lt;p&gt;Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?&lt;/p&gt;

&lt;p&gt;A straightforward solution would be to go through the list and create a second structure one where we'll have numbers from the list and number of their appearances. With Python dictionary might be a good structure for it because we can search quite fast. &lt;/p&gt;

&lt;p&gt;But authors of the problem definitely wanted something else. I was thinking about some mathematical expression, to multiply every number on something and take a sum or something. But I didn't come up with a nice mathematical formula, but I remembered about the &lt;a href="https://en.wikipedia.org/wiki/Exclusive_or"&gt;XOR&lt;/a&gt; function. This binary function is only 1 when values are different. For integers comparison representations of values bit by bit. It means that for equal numbers we will have always 0 as a result. &lt;br&gt;
It means that full function will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution(object):
    def singleNumber(self, nums):
        res = nums.pop(0)
        for i in nums:
            res = res ^ i
        return res
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>python</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
