Last active
August 7, 2025 23:57
-
-
Save GiwbyAlbatross/632602a3b7350083d8d25f58184d4f94 to your computer and use it in GitHub Desktop.
Some algorithm thing
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 sys | |
HELP = """ | |
python3 algorithm1.py -h | |
show this very help page | |
python3 algorithm1.py | |
work out what's special about 495 | |
python3 algorithm1.py <number> | |
run the algorithm on <number> | |
""" | |
def smallest(digits: list) -> int: | |
digits.sort() | |
return int(''.join(digits)) | |
def largest(digits: list) -> int: | |
digits.sort() | |
return int(''.join(reversed(digits))) | |
def once(digits: list, exit=lambda: None, print=lambda *a, **k: None) -> list: | |
s = smallest(digits) | |
l = largest(digits) | |
r = l - s | |
print(f"{l} - {s} = {r}") | |
if r == 0: print("\033[1mGot to zero, exiting\033[0m"); exit() | |
return list(str(r)) | |
def whats_special_about_495() -> None: | |
for num in range(1000): | |
ln = list(str(num)) | |
if set(n := once(ln)) == set(ln): | |
print("\033[1mOther number like 495:", int(''.join(n)), end='\033[0m\n') | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: whats_special_about_495() | |
if sys.argv[-1] in {'-h', '--help'}: print(HELP) | |
digits = list(sys.argv[-1][:3]) | |
while 1: | |
digits = once(digits, sys.exit, print) | |
print(f"Next digits {digits!r}") | |
if digits == list('495'): | |
print("\033[1mWE GOT TO 495\033[0m"); exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Provide number to process as command-line argument. TODO: if no arguments are passed, process all 3-digit numbers.