Skip to content

Instantly share code, notes, and snippets.

View mizhi's full-sized avatar
🦍

Mitch Peabody mizhi

🦍
View GitHub Profile
@mizhi
mizhi / hamming.py
Last active November 25, 2021 00:48
# From a daily programming challenge on the Operation Code slack
def hamming(x: int, y: int) -> int:
"""Compute number of bits where x and y differ.
"""
z = x ^ y
count = 0
while z > 0:
count += (z & 0x1)
z >>= 1
return count
@mizhi
mizhi / single-method-dispatch-on-__init__.md
Last active January 31, 2025 04:04
singledispatchmethod on __init__

I've been doing a bit of refresher on Python, learning about the new capabilities added in Python 3.

Tonight, I learned about @singledispatchmethod alongside typehinting.

You can use it wth __init__ on classes. Honestly, there's no reason why that shouldn't be the case since object allocation and initialization are separated in Python. But it's still nice to confirm.

from functools import *

class Foo:
@mizhi
mizhi / otp.py
Last active February 2, 2019 20:31
An implementation of OATH-HOTP and OATH-TOTP as a learning exercise
import base64
from hashlib import sha1
import hmac
import time
class OTP:
"""Implemets the HOTP and TOTP algorithms.
https://tools.ietf.org/html/rfc4226 (HOTP)
and
##*=== Thursday January 24th 2019 - Daily Programmer ===*
##
##*[XOR Decryption]*
##
##Each character on a computer is assigned a unique code and the preferred standard
##is ASCII (American Standard Code for Information Interchange). For example,
##uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.
##
##A modern encryption method is to take a text file, convert the bytes to ASCII,
##then XOR each byte with a given value, taken from a secret key. The advantage
@mizhi
mizhi / sieve.c
Last active January 16, 2019 00:33
/* *=== Tuesday Jan 15th 2019 - Daily Programmer ===*
*[Sum of Primes]*
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
#include <stdio.h>
#include <stdlib.h>
/* *[Condencing Sentences]*
Compression makes use of the fact that repeated structures are redundant, and
it's more efficient to represent the pattern and the count or a reference to
it. Siimilarly, we can condense a sentence by using the redundancy of
overlapping letters from the end of one word and the start of the next. In this
manner we can reduce the size of the sentence, even if we start to lose meaning.
For instance, the phrase "live verses" can be condensed to "liverses".
# Happy New Year! Here are some simple recursion exercises to get the year
# started! These are great questions for someone learning how to code. Try to
# understand exactly what is happening when you solve these.
# *[Ice Cream Shop]*
# Write a function `hasFlavor(flavors, favorite)` that takes in an array of ice
# cream flavors available at the ice cream shop, as well as the user's favorite
# ice cream flavor. *Recursively* find out whether or not the shop offers their
# favorite flavor.
# *=== Monday Nov 19th 2018 - Daily Programmer ===*
# *[Message Grouping]*
# * This is a real life problem that @Julian Flynn tried to solve last week for his app.
# There are two types of messages: `sub` and `entry`.
# The `sub` entries group the following `sub` entries within its `group`.
# Examples:
# *=== Friday Nov 16th 2018 - Daily Programmer ===*
#
# *[Name Scores]*
#
# Using the provided `names.txt`, a 46K text file containing over five-thousand
# first names, begin by sorting it into alphabetical order. Then working out the
# alphabetical value for each name, multiply this value by its alphabetical
# position in the list to obtain a name score.
#
# For example, when the list is sorted into alphabetical order, COLIN, which is
@mizhi
mizhi / rpn2.sc
Created November 14, 2018 06:14
A terser implementation.
//*=== Tuesday Nov 13th 2018 - Daily Programmer ===*
//
//*[RPN Calculator]*
//
//"RPN" stands for "Reverse Polish Notation". In an RPN world, instead of using normal "infix" notation, e.g. `2 + 2`, you use "postfix" notation, e.g. `2 2 +`
//```1 2 + 3 * => (1 + 2) * 3 => 9
//1 2 3 * + => 1 + (2 * 3) => 7```
//
//Write an RPN calculator with the following methods:
// ```calculator.push() // Adds the element to the stack