How To Master A Programming Language Really Fast 🔥

Niharika Singh ⛓ - Aug 23 '20 - - Dev Community

Having competency in at least one programming language is SUPER important for a developer. Developing competency in more than one language gives you an edge.

You might know some people who can pick up languages in no time.

When you know a language really well, you might think that the only thing that will change with other languages is the syntax.

You can't be more wrong.

The only thing that changes when you move from JavaScript to Rust is NOT the syntax.

Programming style also changes. Key fundamentals change.


A couple months ago, I was learning to program in Rust by using the docs. (This is a really good resource for anyone who wants to get started with Rust)

In my personal experience, learning curve of Rust lang 🦀 is really steep. No amount of books/documentation can conclude your learning.

Luckily, around this time I was solving a lot of competitive programming questions on LeetCode.

I decided that I'll take up a 30 day challenge: Solve 30 programming questions in Rust.

THAT really made all the difference.


Let's take Contiguous Array as an example question.

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

Example 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

Note: The length of the given binary array will not exceed 50,000.

Solving it in Java was quite simple:

class Solution {
    public int findMaxLength(int[] nums) {
        int sum = 0;
        int res = 0;
        // build a <sum, index> hashmap
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);
        for (int i = 0; i < nums.length; i++) {
            System.out.println(i);  
            if (nums[i] == 0) {
                sum++;
            } else {
                sum--;
            }
            if (map.containsKey(sum)) {
                res = Math.max(res, i - map.get(sum));                
            } else {
                map.put(sum, i);
            }
        }
        return res;
    }
}

Also, in JavaScript it is simple:

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxLength = function(nums) {
    let hash = {0:-1};
    let count = 0;
    let max = 0;
    for (let i=0;i<nums.length;i++) {
        if (nums[i] == 0) count--;
        else count++;

        if (hash[count]!=null) max = Math.max(max, i - hash[count]);
        else hash[count] = i 
    }
    return max;
};

But the real challenge was to solve it in Rust. You search and then you research until you finally learn.

use std::cmp;
use std::collections::HashMap;

impl Solution {
    pub fn find_max_length(nums: Vec<i32>) -> i32 {
        let mut sum: i32 = 0;
        let mut len: i32 = 0;

        let mut map = HashMap::new();
        map.insert(0, -1);

        let size = &nums.len();

        for i in 0..*size {
            if nums[i as usize] == 0 {
                sum -= 1;
            } else {
                sum += 1;
            }
            if map.contains_key(&sum) {
                let x: i32 = match map.get(&sum) {
                    Some(&s) => s,
                    None => -1
                };  
                len = cmp::max(len, (i as i32 -x));
            } else {
                map.insert(sum, i as i32);
            }
        }
        len
    }
}

In short, start solving competitive programming questions in the language you want to learn. You will see a stark difference within a month!

Let me know your thoughts in discussion below.

Cheers!

. . . . . . . . . . . . . . . . .
Terabox Video Player