Skip to content

Instantly share code, notes, and snippets.

View gsrai's full-sized avatar

Gagan Srai gsrai

View GitHub Profile
@gsrai
gsrai / upgrade_go.sh
Created May 6, 2022 17:15
Upgrade golang version on an M1 Mac (Apple Silicon)
#!/usr/bin/env bash
GO_FILE_NAME="go1.18.1.darwin-arm64.tar.gz" # find filename on https://go.dev/dl/
# usage:
# chmod u+x upgrade_go.sh
# sudo ./upgrade_go.sh
sudo mv /usr/local/go /usr/local/_go_old
mkdir /tmp/downloads
@gsrai
gsrai / main.go
Created May 6, 2022 12:19
Grains in Go - exercism.io solution
package main
import (
"fmt"
"time"
)
/*
* There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up.
* Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
@gsrai
gsrai / index.html
Last active March 28, 2022 14:31
React + Babel + Tailwind | HTML only
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>404 Not Found - React + Babel + Tailwind HTML only</title>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<!-- Don't use this in production: -->
@gsrai
gsrai / fastestSolution.ts
Last active February 20, 2022 15:43
valid anagram go vs typescript (go is 16x faster and uses 16x less memory 🤯)
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false
let counter = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
counter[s.charCodeAt(i) - 'a'.charCodeAt(0)]++
counter[t.charCodeAt(i) - 'a'.charCodeAt(0)]--
}
return !counter.some(i => i < 0)
};
@gsrai
gsrai / Results.md
Last active May 28, 2023 13:05
Fibonacci (iterative, recursive, memoized)

JS vs Go vs C fib

Running on MacBook Air (M1, 2020) with 16 GB of RAM.

gcc fib.c -O2

n = 40

result = 102334155

@gsrai
gsrai / sema.js
Created December 26, 2021 18:31
Async Await Semaphores
class Timer {
startTime
endTime
constructor() {
this.startTime = Date.now();
}
finish() {
this.endTime = Date.now();
@gsrai
gsrai / core.clj
Created December 5, 2020 13:23
Binary Search in Clojure
(defn binary-search [n coll]
(loop [upper (dec (count coll))
lower 0]
(let [mid (int (Math/ceil (/ (+ upper lower) 2)))]
(if (= upper lower)
nil
(cond (= n (nth coll mid)) mid
(< n (nth coll mid)) (recur (dec mid) lower)
(>= n (nth coll mid)) (recur upper mid))))))

Are decorators just higher order functions?

a HOF is just a function which takes a function as an argument. Alternatively, it’s a function which returns a new function. In both cases it’s a higher-order function. There is another case when you define a function which returns a function. This could be a middleware in Clojure’s Ring or a Python decorator:

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
@gsrai
gsrai / kata.clj
Created November 14, 2020 19:57
Roman numeral kata in Clojure
(ns roman-numeral-converter.core
(:require [clojure.test :refer [deftest is run-tests]]
[clojure.string :refer [replace]]))
(def numeral-mapping {1 "I" 5 "V" 10 "X" 50 "L" 100 "C" 500 "D" 1000 "M"})
(defn arabic-to-roman [n]
(loop [[x & xs :as all] (-> numeral-mapping keys sort reverse)
romanised ""
num n]
@gsrai
gsrai / better_impl.py
Last active May 20, 2022 13:39
Maximum Nesting Depth of the Parentheses in python, java, clojure, javascript and go
def max_depth(s):
arr = [0]
for char in s:
if char == "(":
arr.append(arr[-1] + 1)
if char == ")":
arr.append(arr[-1] - 1)
return max(arr)
s = "( ((X)) (((Y))) )"