LeetCode: Arrays 101: Inserting Items Into an Array

Hello,

Here are my solutions for the second part of the card: Arrays 101, from LeetCode.

Duplicate Zeroes

Given an array of integers, remove duplicate zeroes and shift the remaining elements.

https://leetcode.com/problems/duplicate-zeros/

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        index = 0
        arr_length = len(arr)
        while index < arr_length:
            if arr[index] == 0:
                arr.insert(index, 0)
                arr.pop()
                index += 1
            index += 1

 Merge Sorted Array

Given two sorted arrays, merge them together into nums1.

https://leetcode.com/problems/merge-sorted-array/

This problem wasn’t very Pythonic and I actually want to practice my problem solving skills together with my Python skills, so, I’ve took the liberty to modify the problem a little:

from typing import List


class Solution:
    def merge(self, nums1: List[int], nums2: List[int]) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        n = len(nums2)
        # remove 0 from nums1
        nums2_index = 0

        for i, num in enumerate(nums1):
            # We've reached the end of nums2, means we're merged.
            if nums2_index == n:
                break
            if nums2[nums2_index] <= num:
                nums1.insert(i, nums2[nums2_index])
                nums2_index += 1

        # If there's anything left in nums2 we'll add it to the end
        for item in nums2[nums2_index:]:
            nums1.append(item)


if __name__ == '__main__':
    s = Solution()
    a = [1,2,3]
    s.merge(a, [2,5,6])
    print(a)

Thank you!

LeetCode: Arrays 101: Introduction – Solutions

Hello,

LeetCode is a good place to practice programming by solving problems of all levels!

After you solve a problem, you’ll get access to all submitted solutions, sorted by time and memory usage. That’s a nice way to compare your code to others and see what you did well and where you can improve.

Here’s my solutions for the Arrays 101: Introduction card:

Max Consecutive Ones

https://leetcode.com/problems/max-consecutive-ones/

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        max_counter = 0
        counter = 0
        for i in nums:
            if i == 1:
                counter += 1
            else:
                if counter > max_counter:
                    max_counter = counter
                counter = 0
        if counter > max_counter:
            max_counter = counter
        return max_counter

Find Numbers with Even Number of Digits

https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

class Solution:

    @staticmethod
    def is_even(value):
        return len(str(value)) % 2 == 0

    def findNumbers(self, nums: List[int]) -> int:
        return_value = 0
        for number in nums:
            if self.is_even(number):
                return_value += 1
        return return_value

Squares of a Sorted Array

https://leetcode.com/problems/squares-of-a-sorted-array/

When writing code like this be careful because you’re altering the A list in place.

class Solution:
    def sortedSquares(self, A: List[int]) -> List[int]:
        for index, num in enumerate(A):
            A[index] = num ** 2
        return sorted(A)

Thanks for reading!

If you like what I’m writing, don’t forget to subscribe! 🙂

Nucu Car: Devlog 0x02

Hello,

I’m still working on my NucuCar project from time to time, I’m currently at my parents home and I left the raspberry pi at my apartment so I’m only testing the builds it on my computer. Luckily I can test the telemetry by using the CPU temperature sensor implemented by dotnet IoT.

For the next step, I wanted an effective way to store telemetry data in the cloud, preferably for free, since this is a hobby project after all.

After testing multiple solutions I’ve settled with Firebase, I tried to write a telemetry publisher for it using it’s SDK which internally uses gRPC and everything went well until I put it on the board. It turns out that dotnet gRPC doesn’t work on raspberry pi. I got the basic server working using a hack but that’s about it, so I moved to the REST API and I’ve implemented a simple translator which translates raw C# dictionaries into compatible Firebase RESTful post request Json payload.

The project got a little bigger and at the current state it was quite messy, a restructuring is needed and this is what I’ve done:

NucuCar.Domain

Initially I’ve designed the NucuCar.Domain project to contain .proto files and data classes without too much implementation, however, many Telemetry Publishers such as Azure, Disk were in this package and it was kind of wrong because they contain too much implementation and they were dependent on heavy external packages and they would bring too much of a burden.

The TelemetryPublisher abstract class and the ITelemeter interface both which don’t contain implementation remained in NucuCar.Domain and the concrete publishers along with the worker were extracted in NucuCar.Telemetry.

I’ve also extracted the Firebase translator into a package of it’s own, and published it on NuGet, making this my first published package. 😀

NucuCar.Common

In NucuCar.Common, I’ve moved some of the classes which were utility classes and were still present in NucuCar.Domain, the ConnectionParser class and a class which provides basic argument guarding.

Another thing that I needed and I’ve implemented in NucuCar.Common is a HttpClient wrapper class, I found very verbose to serialize Json every time and deserialize it when sending data to Firebase.

Thus I made a simple wrapper to simplify the process, while also introducing a retry mechanism and cancellation and timeout support.

Conclusion

Overall I’m quite happy with the end result and I’m looking forward to see what interesting things I can implement in the future.

I’m not sure if I want this to be a remote controlled toy car project anymore, it sounds good but I think it would be more practical If I could connect multiple sensors, create a simple interface and monitor my apartment using this, perhaps add some support to automatically water my plants, that would be nice.

The project is open source and can be found on my Github.

Thanks for reading!