Sherry L

[LeetCode] Learning Python with the Leetcode Beginner Guide

I got a RaspberryPi long time ago but never had the chance to play with it. Recently I finally started looking into doing some small IoT projects. And before getting into that, with the growing interest of image-processing and scraping and… endless things to learn (😂), I decide to learn Python, which is also the language recommended by the RaspberryPi developer community.

I have been coding along an Udemy Python course to learn the basics, and want to get familiar with this language as fast as possible. To get my hands dirty, I figure I should expose myself to this language more than just course exercises. Since I have learned some syntaxes and with the problem solving skills I had from my day-to-day experience working with JavaScript, I decided to boost my familiarity of Python with the Leetcode Beginners Guide.

This guide is a good starter with basic data structures. I have come across one of the interesting problem sets, where Python’s simplicity surprises me. So here is my solving process for 1342. Number of Steps to Reduce a Number to Zero:

Observe:

We should calculate how many steps is needed to reduce a number to zero. The rules are:

  • Divide the number by two if it’s even
  • Subtract one if it’s odd

Find the edge cases or ‘terminating point’:

Once the value of the number hits 0, we’re done. Return the steps needed.

Pseudo code:

0. num == 0 ? True: Go to step 5, False: Go to step 1
1. num % 2 == 1 ? True: Go to step 3, False: Go to Step 3
2. Even: num = num / 2 Go to step 4
3. Odd: num = num - 1 Go to step 4
4. step++ Go to step 0
5. return final steps

Solution 1 - modulo

class Solution(object):
    def numberOfSteps(self, num):
        """
        :type num: int
        :rtype: int
        """
        times = 0
        while num > 0:
          if (num % 2) != 1:
            num = num / 2
          else:
            num -= 1
          times += 1
        return times

# Time Complexity: O(logN)
# Space Complexity: O(1)

Runtime: 7 ms
Memory Usage: 13.5 MB


Solution 2 - bitwise operator

Since we are playing with odd and even numbers, we can use a bitwise operator to solve this one. Actually just changing a lil bit of the code where the modulo was used, and apply the bitmask 1 to num to determine whether num is odd or even:

class Solution(object):
    def numberOfSteps(self, num):
        """
        :type num: int
        :rtype: int
        """
        times = 0
        while num > 0:
          if (num & 1) == 0:  // if num bitwise-and 1 is 0, means it's even
            num = num / 2
          else:
            num -= 1
          times += 1
        return times

# Time Complexity: O(logN)
# Space Complexity: O(1)

Runtime: 23 ms
Memory Usage: 13.4 MB


The other problem sets in this beginner guide are also basic enough to help boosting my confidence and make me feel more comfortable using the language. Starting from here, I can now explore the practical side of the language itself while continuing to learn its applications in different settings.