Using PySnooper for Python Debugging

When your Python code doesn’t run as expected or when you want to examine if your program is running correctly, you may use a full-fledged debugger with breakpoints and watches. But in some cases, you can’t be bothered to set one up right now. You want to know which lines are running and which aren’t, and what the values of the local variables are. Most people would use print lines, in strategic locations, some of them showing the values of variables. [Read more]

Direct Download Links Docker

Docker requires user login in order to download Docker for Windows and Docker for Mac. This is so annoying for those who just need docker to run images.

Fortunately, the direct download links are still valid and you can download docker for windows and Mac without login.

Here is the links:

Atomic

1 Introduction to Atomic Atomic classes Chemically, we know that atoms are the smallest unit of a general substance and are inseparable in chemical reactions. In our case Atomic means that an operation is uninterruptible. Even when multiple threads are executed together, once an operation starts, it will not be disturbed by other threads. Therefore, the so-called atomic class is simply a class with atomic/atomic operational characteristics. The atomic classes of the concurrent package java. [Read more]

How to Extract Text From Pdf in Python 3

To extract text from pdf using Python 3, we will use the pdfminer.six package. Install pdfminer.six pip install pdfminer.six Extract text from pdf import io from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.converter import TextConverter from pdfminer.layout import LAParams from pdfminer.pdfpage import PDFPage def convert_pdf_to_txt(path): '''Convert pdf content from a file path to text :path the file path ''' rsrcmgr = PDFResourceManager() codec = 'utf-8' laparams = LAParams() with io.StringIO() as retstr: with TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams) as device: with open(path, 'rb') as fp: interpreter = PDFPageInterpreter(rsrcmgr, device) password = "" maxpages = 0 caching = True pagenos = set() for page in PDFPage. [Read more]

How to remove all white space in Python

If you want to remove leading and ending spaces, use str.strip(): sentence = ' newbie dev' sentence.strip() >>> 'newbie dev' Remove ALL spaces in a string, even between words: Use str.replace(): sentence = ' newbie dev' sentence.replace(" ", "") >>> 'newbiedev' Use regex import re sentence = ' newbie dev' sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE) Remove ONLY DUPLICATE spaces: Use regex sentence = ' hello apple' " ". [Read more]

Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] word = "ABCCED", -> returns true, word = "SEE", -> returns true, word = "ABCB", -> returns false. [Read more]

Wiggle Sort

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]….

For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

Wiggle Sort Solution

public class WiggleSort {
    public void wiggleSort(int[] nums) {
        for(int i = 1; i < nums.length; i++) {
            int current = nums[i - 1];

            if((i % 2 == 1) == (current > nums[i])) {
                nums[i - 1] = nums[i];
                nums[i] = current;
            }
        }
    }
}

Unique Paths

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

Unique Paths Solution

class UniquePaths {
    public int uniquePaths(int m, int n) {
        Integer[][] map = new Integer[m][n];

        //only 1 way to get to ith row, 0th column (move down)
        for(int i = 0; i < m; i++){
            map[i][0] = 1;
        }

        //only 1 way to get to ith column, 0th row (move right)
        for(int j= 0; j < n; j++){
            map[0][j]=1;
        }

        //x ways to get to ith row, jth column (# of ways to get to
        //ith - 1 row, jth column + # of ways to get to jth - 1 column
        //ith column
        for(int i = 1;i < m; i++){
            for(int j = 1; j < n; j++){
                map[i][j] = map[i - 1][j] + map[i][j - 1];
            }
        }

        return map[m - 1][n - 1];
    }
}

Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Summary Ranges Solution public class SummaryRanges { public List<String> summaryRanges(int[] nums) { List<String> result = new ArrayList(); if(nums.length == 1) { result.add(nums[0] + ""); return result; } for(int i = 0; i < nums.length; i++) { int current = nums[i]; while(i + 1 < nums.length && (nums[i + 1] - nums[i] == 1)) { i++; } if(current ! [Read more]

Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] Subsets Ii Solution public class SubsetsII { public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<List<Integer>>(); if(nums.length == 0 || nums == null) { return result; } helper(nums, new ArrayList<Integer>(), 0, result); return result; } public void helper(int[] nums, ArrayList<Integer> current, int index, List<List<Integer>> result) { result. [Read more]