From http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Using the implementation from Wikipedia: https://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation """ | |
def hamming_weight(n): | |
t1 = n - ((n>>1) & 0x55555555) | |
t2 = (t1 & 0x33333333) + ((t1 >> 2) & 0x33333333) | |
return ((((t2 + (t2 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24,n) | |
def hamming_sort(n): | |
return [x[1] for x in sorted(map(hamming_weight,xrange(1,n+1)))] |
I hereby claim:
- I am rrampage on github.
- I am raunak (https://keybase.io/raunak) on keybase.
- I have a public key ASBUeum7e_np2neA9YXi60A7YLGFHk9yI9n3VCNsKgQSIwo
To claim this, I am signing this object:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if two strings are permutations (anagrams) of each other | |
# For this example, using just the lowercase ASCII letters as the alphabet | |
# Step 1 : Generate k primes using Sieve of Erasthothenes | |
# Step 2 : Map characters in alphabet to prime | |
# Step 3 : Compare the products of the 2 strings | |
# Why does this work? | |
# - Multiplication is commutative i.e a * b * c = c * a * b | |
# - Fundamental theorem of arithmetic i.e every natural number can be expressed uniquely as either a prime or a product of primes (https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic) | |
from string import ascii_lowercase as ALPHABETS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Search Wayback machine for a particular URL | |
# Add to your ~/.bashrc or ~/.bash_profile | |
# Needs `jq` and `curl` to work | |
if [[ -z $1 ]]; then | |
echo "Usage: $0 URL"; | |
else | |
curl "http://web.archive.org/cdx/search/cdx?url=$1*&output=json&fl=original,timestamp" 2> /dev/null | jq '.[1:][] |"https://web.archive.org/web/" +.[1] + "/" + .[0]' 2> /dev/null; | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"regexp" | |
"strconv" | |
) | |
var polyRegex = regexp.MustCompile(`\s*(?P<coef>[+-]?[0-9]*)?\s*\*?((?P<var>[a-z])?\s*(\^(?P<pow>[+-]?[0-9]+))?)?\s*`) | |
var wsRegex = regexp.MustCompile(`\s+`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import os | |
os.environ["KMP_DUPLICATE_LIB_OK"] ="TRUE" | |
import sys | |
import re | |
import time | |
import torch as t | |
import numpy as np | |
from pathlib import Path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Compile with one of the following: | |
zig cc -v -s -Os -target aarch64-linux-musl -nostdlib -flto -static shell.c -o csh | |
zig cc -v -s -Os -target aarch64-linux-gnu -nostdlib -flto -static shell.c -o csh | |
CLANG: | |
clang -v -s -Oz -ffreestanding -nostdlib -fno-stack-protector -Wl,--entry=_start -Wl,--gc-sections -Wl,-z,now -flto -static -o csh shell.c | |
musl-clang -v -s -Os -nostdlib -nostartfiles -fno-stack-protector -Wl,--entry=_start -Wl,--gc-sections -Wl,-z,now -flto -nostdinc -static -o csh shell.c | |
GCC (some things DO NOT WORK like `pwd`): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snake for Linux terminal | |
// Fits in a QR code! | |
// Compile with: | |
// zig build-exe snek.zig -O ReleaseSmall -target x86_64-linux -fstrip -flto -fsingle-threaded -femit-bin=snek_x64 | |
// zig build-exe snek.zig -O ReleaseSmall -target aarch64-linux -fstrip -flto -fsingle-threaded -femit-bin=snek_aarch64 | |
// Run `sstrip -z` from https://www.muppetlabs.com/~breadbox/software/elfkickers.html to reduce binary size even further | |
// Currently, snek_x64 is 2616 bytes and snek_aarch64 is 2941 bytes | |
// Threshold for a binary QR code is 2953 bytes | |
// Encode using: | |
// qrencode -8 -r snek_aarch64 -o qr_aarch64.png |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.equ SYS_READ, 63 | |
.equ SYS_WRITE, 64 | |
.equ SYS_EXIT, 93 | |
.equ SYS_SOCKET, 198 | |
.equ SYS_BIND, 200 | |
.equ SYS_LISTEN, 201 | |
.equ SYS_ACCEPT, 202 | |
.equ SYS_CLOSE, 57 | |
.equ SYS_OPENAT, 56 | |
.equ SYS_SENDFILE, 71 |
OlderNewer