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…

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…

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.…

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=",…

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;…

%d bloggers like this: