Skip to content

Instantly share code, notes, and snippets.

@mwtoews
Last active April 9, 2024 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwtoews/a2b199b97ba0d938e49349365ed8676b to your computer and use it in GitHub Desktop.
Save mwtoews/a2b199b97ba0d938e49349365ed8676b to your computer and use it in GitHub Desktop.
digital root
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