Skip to content

Instantly share code, notes, and snippets.

View spencerbyw's full-sized avatar
🚀

Spencer Bywater spencerbyw

🚀
View GitHub Profile
// "Given a number n, return the number of 1s in the binary representation of n."
// Assumptions:
// - n is an integer
function countOnesInBinaryOfNumber(number) {
let num = number;
if (num < 0) {
// This makes it so the binary conversion later for negative numbers actually
// uses the correct two's complement format
// See https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript
num = num >>> 0;
function nIsAPowerOfTwo(num) {
return (Math.log2(num) % 1) === 0;
}
// > nIsAPowerOfTwo(0);
// false
// > nIsAPowerOfTwo(1);
// true
// > nIsAPowerOfTwo(2);
// true
@spencerbyw
spencerbyw / is_prime.py
Created January 4, 2017 23:42
Check if number is prime using fancy regex
#!/usr/bin/env python
import re
import sys
def main():
num_str = '1' * int(sys.argv[1])
match = re.match(r'^1?$|^(11+?)\1+$', num_str)
print('not prime' if match else 'is prime')
@spencerbyw
spencerbyw / sample.json
Created November 28, 2016 05:01
Sample cure-all API JSON
{
"email": "sgarcia0@wordpress.org",
"hiredate": "Wed, 01 Oct 2014 00:00:00 GMT",
"name": "Stephen Garcia",
"role": "VP Sales",
"team": {
"board": {
"backlog": [
{
"description": "Duis bibendum.",