development

Python datetime strptime: unconverted data remains

The datetime conversion function strptime() kept failing until the invisible error got caught!

Read more
development

Python Coding Tips: Set & List

The following code snippets are very helpful during sets and list operations. Intersection of sets must check for valid case $ cat sets.py s1 = {1,2,3} s2 = {} # empty sets means the intersection is empty too! s3 = {} if set(s1) & set(s2): print(s1.intersection(s2)) else: print(“no intersection”) Result $ python sets.py no intersection […]

Read more
development

Python Collections: Counter

Many times we need to keep track of frequencies of unique keys. We can always use a dictionary but counter is more appropriate for its brevity. Python collections define the counter class. Counter works on any iterator and generates a count of elements. So it has a natural use case on the list 🙂 from […]

Read more
development

K Weakest Rows in a Matrix

Problem The K Weakest Rows in a Matrix Solution Uses Binary search to count 1s Uses heap to keep k weakest rows It naturally satisfies the condition for weak rows with the same number of soldiers. The smaller index row is processed first and hence any next row would not get pushed to the heap. […]

Read more
development

Number of 1s in a sorted array

Problem Count number of 1s in a sorted array. e.g. [1,1,1,1,0,0,0,0,0,0] Code def countOneBS(row, start, end): if row[start] == 0: return 0 if row[end] == 1: return end – start + 1 # The mid is dependent on the index of start mid = start + (end – start) // 2 # print(“mid=”, mid, “start=”, […]

Read more
development

Basic HashMap in C++

Useful Points I use chaining for key collisions. The key is also stored with the payload. A GET operation searches the key in bucket’s linked-list and returns the value. #include<bits/stdc++.h> using namespace std; class HashNode { private: string key; string value; HashNode *next; public: HashNode(string key, string value) { this->key = key; this->value = value; […]

Read more
development

Longest Palindrome Substring in a String

DP based solution class Solution(object): def __init__(self): self.lookup = {} def isPalindrome(self, s, dp, i, k): j = i + k – 1 if s[i] == s[j] and dp[i + 1][j – 1]: return True return False def longestPalindrome(self, s): “”” :type s: str :rtype: str “”” size = len(s) if size < 2: return […]

Read more
development

Palindrome String Check With Non-alphanumeric Characters

class Solution(object): def isPalindrome(self, s): “”” :type s: str :rtype: bool “”” if len(s) < 1: return True start, end = 0, len(s) – 1 while start < end: # Loop till a special char is found while start < end and not s[start].isalnum(): start += 1 # Always keep the check start < end […]

Read more
development

Stocks Maximize Profit, Multiple Buy & Sell

class Solution(object): def maxProfit(self, prices): “”” :type prices: List[int] :rtype: int “”” if len(prices) < 2: return 0 minP = prices[0] result = 0 for price in prices: if minP == float(‘inf’): minP = price profit = (price – minP) if profit > 0: result += profit minP = float(‘inf’) minP = min(price, minP) return […]

Read more