Last active
April 9, 2024 02:56
-
-
Save mwtoews/a2b199b97ba0d938e49349365ed8676b to your computer and use it in GitHub Desktop.
digital root
This file contains 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 re | |
good = {1, 4, 7} | |
bad = {3, 6, 9} | |
neutral = {2, 8, 5} | |
re_digits = re.compile(r"([\d\:\.\,]+)") | |
re_digit = re.compile(r"\d") | |
def digital_root(p): | |
p = str(p) | |
if len(p) == 1 and p.isdigit(): | |
return int(p) | |
elif len(p) > 1: | |
return digital_root(sum(int(d) for d in re_digit.findall(p))) | |
else: | |
raise Value(f"cannot evaluate {p!r}") | |
def find_digital_roots(inp): | |
"""Find 0 or more digital roots from a string""" | |
i = -1 | |
for i, p in enumerate(re_digits.findall(inp)): | |
r = digital_root(p) | |
if r in good: | |
b = "good" | |
elif r in bad: | |
b = "bad" | |
else: | |
b = "neutral" | |
print(f"{i + 1}: {p!r}: {r} => {b}") | |
if i == -1: | |
print("no digits found") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment