Skip to content

Instantly share code, notes, and snippets.

View crazyoptimist's full-sized avatar
🐌
crawling

crazyoptimist crazyoptimist

🐌
crawling
View GitHub Profile
@crazyoptimist
crazyoptimist / linux-cheatsheet.md
Last active April 8, 2024 00:49
crazyoptimist's linux cheatsheet

Run the previous command as root

sudo !!

Reverse search command history

<Ctrl+r>

@crazyoptimist
crazyoptimist / nvm-install.sh
Last active March 28, 2024 07:39
Install the latest version of nvm, node, npm
#!/bin/bash
# Check the latest version of NVM
curl https://github.com/nvm-sh/nvm/releases/latest | cut -d \/ -f 8 | cut -d \" -f 1
# Run the install script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# Paste in this scripts into your .bashrc or .bash_profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Install node and npm
@crazyoptimist
crazyoptimist / prompt.sh
Created March 14, 2024 07:39
Minimal Bash Prompt
# new line + workdir + $
export PS1="\n\w $ "
export PROMPT_DIRTRIM=2
@crazyoptimist
crazyoptimist / minimum-reorient.go
Created February 28, 2024 08:11
There is a network with N roads and N + 1 cities. The cities are labeled with distinct integers within the range [O..N]. Roads connect the cities in such a way that there is exactly one way to travel between any two of the cities. In other words, the network forms a tree.
package main
import (
"strconv"
)
func solution(A []int, B []int) int {
n := len(A)
graph := make([][]int, n+1)
// track visited nodes when traversing the graph
@crazyoptimist
crazyoptimist / semaphore.go
Last active February 4, 2024 03:46
Semaphore Concurrency Pattern with Buffered Channel in Go
package main
import (
"fmt"
"time"
)
const (
NUM_WORKERS = 100
NUM_JOBS = 1000
@crazyoptimist
crazyoptimist / workerpool.go
Last active February 3, 2024 04:05
Worker Pool Concurrency Pattern in Go
package main
import (
"fmt"
"sync"
)
const (
NUM_WORKERS = 10
NUM_JOBS = 10000
@crazyoptimist
crazyoptimist / keybase.md
Last active January 19, 2024 18:23
Identify myself

Keybase proof

I hereby claim:

  • I am crazyoptimist on github.
  • I am crazyoptimist (https://keybase.io/crazyoptimist) on keybase.
  • I have a public key ASAhECPtn-axR1XmbtP2Tvjlpe7x_UHOenDNdDxXSEJ2Swo

To claim this, I am signing this object:

@crazyoptimist
crazyoptimist / verify-cognito-jwt.ts
Last active August 11, 2023 16:43
Verify Cognito JWT tokens
import {
CognitoJwtVerifier,
CognitoJwtVerifierSingleUserPool,
} from 'aws-jwt-verify/cognito-verifier';
const verifier: CognitoJwtVerifierSingleUserPool<{
userPoolId: string;
tokenUse: 'access' | 'id';
clientId: string;
}> = CognitoJwtVerifier.create({
@crazyoptimist
crazyoptimist / build_spa.yml
Created June 7, 2023 10:52
Build SPA in GHA
name: Build SPA
on:
pull_request:
types: [opened, edited, synchronize, reopened, closed]
branches:
- main
jobs:
build:
@crazyoptimist
crazyoptimist / monitor-memory-usage.ts
Created June 6, 2023 07:43
Monitor Memory Usage in NodeJS
function formatMemoryUsage(data: number) {
return `${Math.round((data / 1024 / 1024) * 100) / 100} MB`;
}
function getCurrentMemoryUsage(): Object {
const memoryData = process.memoryUsage();
return {
rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,