Last active
May 13, 2026 03:24
-
-
Save jpf/90ed38013790941ac14ea621bee109c1 to your computer and use it in GitHub Desktop.
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
| def franustring(n: int) -> str: | |
| """Generate a franustring of length n. | |
| A franustring is a self-measuring string that allows you to | |
| eyeball the length of a string by looking at the last part of | |
| the string and doing a little math. It's sort of like a self | |
| describing "ruler" for strings. | |
| e.g. | |
| n=1 len=1 '1' | |
| n=2 len=2 '12' | |
| n=4 len=4 '1234' | |
| n=8 len=8 '12345678' | |
| n=16 len=16 '12345678(10)3456' | |
| Positions are 1-indexed. At position p the character is p % 10 | |
| (rendered as a digit), except where a milestone marker "(N)" is | |
| inserted. A marker for milestone N (every positive multiple of 10) | |
| sits with "(" at position N-1, the digits of N starting at | |
| position N, and ")" immediately after. | |
| A marker that starts before n but would extend past it is | |
| truncated, so the returned string is always exactly n chars. | |
| """ | |
| chars: list[str] = [] | |
| p = 1 | |
| while p <= n: | |
| if (p + 1) % 10 == 0: | |
| for c in f"({p + 1})": | |
| if p > n: | |
| break | |
| chars.append(c) | |
| p += 1 | |
| else: | |
| chars.append(str(p % 10)) | |
| p += 1 | |
| return "".join(chars) | |
| if __name__ == "__main__": | |
| import sys | |
| if len(sys.argv) > 1: | |
| print(franustring(int(sys.argv[1]))) | |
| else: | |
| print(franustring.__doc__) | |
| for k in range(9): | |
| n = 2 ** k | |
| s = franustring(n) | |
| print(f"n={n:4} len={len(s):4} {s!r}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment