Skip to content

Instantly share code, notes, and snippets.

View anishLearnsToCode's full-sized avatar
🧙
Unexpected '{' on line 32

Anish Sachdeva anishLearnsToCode

🧙
Unexpected '{' on line 32
View GitHub Profile
private int hammingWeight(int x) {
int result = 0;
while (x > 0) {
x = x & (x - 1);
result++;
}
return result;
}

Quicksort

public static void quickSort(int[] array) {
   quickSort(array, 0, array.length - 1);
}

private static void quickSort(int[] arr, int begin, int end) {
   if (begin < end) {
       int partitionIndex = partition(arr, begin, end);
 quickSort(arr, begin, partitionIndex - 1);

Greatest Common Divisor (Euclid's Algorithm)

private static int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

String Searching

KMP (knuth Morrison Pratt) Algorithm

private int kmpIndex(String string, String pattern) {
    int[] patternPrefix = patternPrefixArray(pattern);
    for (int t = 0, p = 0 ; t < string.length() && p < pattern.length() ; ) {
        if (string.charAt(t) == pattern.charAt(p)) {
            if (p == pattern.length() - 1) return t - p;
 p++;

Binary Searches

It seems like Binary Search is actually just a single algorithm, when that is absolutely not the case at all. There are several different variants of Binary Search all giving a different kind of target value. One that gives rightmost, another that ives leftmost, one which gives the first largest than target etc.

Smallest Element Larger than the target value

public char nextGreatestLetter(char[] letters, char target) {
    int left = 0, right = letters.length - 1, middle;
 while (left &lt;= right) {
@anishLearnsToCode
anishLearnsToCode / creating_2d_array_python.py
Created August 2, 2021 14:44
Creating a 2D Array in 🐍 Python
array = [[0 for i in range(columns)] for j in range(rows)]
@anishLearnsToCode
anishLearnsToCode / iterm2-solarized.md
Created January 21, 2021 19:27 — forked from kevin-smets/iterm2-solarized.md
iTerm2 + Oh My Zsh + Solarized color scheme + Source Code Pro Powerline + Font Awesome + [Powerlevel10k] - (macOS)

Default

Default

Powerlevel10k

Powerlevel10k

@anishLearnsToCode
anishLearnsToCode / starcounter.js
Created November 21, 2020 11:02 — forked from yyx990803/starcounter.js
Count your total stars!
var https = require('https'),
user = process.argv[2],
opts = parseOpts(process.argv.slice(3))
request('/users/' + user, function (res) {
if (!res.public_repos) {
console.log(res.message)
return
}
var pages = Math.ceil(res.public_repos / 100),

JetBrains Keyboard Shortcuts

  • Folding Code: Ctrl + .
  • Search Everywhere: Ctrl + Shift + F
  • Search File Names: Shift * 2
  • Search in File: Ctrl + F
@anishLearnsToCode
anishLearnsToCode / command.sh
Created September 3, 2020 07:07
Install tensorflow without the fuss and error messages on Windows/Mac
$ pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl