Last active
July 16, 2022 05:42
-
-
Save apc518/8057d164d9ab2e07d55c7fa9b6dc251a to your computer and use it in GitHub Desktop.
Jord
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 hashlib | |
from math import ceil, log | |
BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | |
def is_jord(n): | |
""" | |
A useless music theory concept, the 'jord' is a sequence of notes where if | |
the scale degrees of each note are concatenated as base 10 digits, and the | |
resultant number is passed into this function, the result will be True | |
""" | |
# number cannot be negative | |
if n <= 0: | |
return False | |
# no scale degree 0 | |
if '0' in str(n): | |
return False | |
# calculate the minimum number of bytes necessary to encode the number | |
encoding_length = ceil(log(n, 256)) | |
# for powers of 256 and rounding errors | |
if not 256 ** encoding_length > n: | |
encoding_length += 1 | |
hash_result = int(hashlib.sha256(int(n).to_bytes(encoding_length, 'big', signed=False)).hexdigest(), 16) | |
return BASE64_CHARS[hash_result % 64].lower() == 'j' # this could just check for `hash_result % 64 in [9, 35]` but the reasoning is more clear this way |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment