# Tag: programming ## Python datetime strptime: unconverted data remains

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

## 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 Sorting a list of tuple…

## 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 collections import Counter def checkDiff(list1,…

## Difference between Increasing & Non-Decreasing Order

Let’s take an array for example: int arr[] = {1,2,3,4,5,6}; The above array is sorted in increasing and non-decreasing order. Now, let’s add a few duplicates. int arr[] = {1,1,2,2,3,4,4}; The above array is not in an increasing order. But it is in non-decreasing order.

## 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. from heapq import heappushpop, heappush,…

## 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=”, start, “end=”, end) return countOneBS(row,…

## 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; this->next = NULL; } string…

## 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 s dp = [ [False]*size…

## 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 while start < end and…

## 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 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 result Reference https://www.programcreek.com/2014/02/leetcode-best-time-to-buy-and-sell-stock-java/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Written…