Created
November 1, 2024 02:13
-
-
Save gmart7t2/de52e33e679bc8a7ebf3726ec6208be6 to your computer and use it in GitHub Desktop.
Generate a list of all uniform palinceptions
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
#!/usr/bin/env python3 | |
# generate all the uniform palinceptions | |
# it generates some multiple times (111-111-111-111, 1111-1111-1111, and 111111-111111 all look the same without the dashes) | |
# (111-111-121-111-111 and 11111-11211-11111 too) | |
# so pipe the output through sort -un | |
import itertools | |
min_length = 11; max_length = 16 | |
def generate_palindromes(): | |
def generate_palindromes_of_length(length): | |
r = range(10 ** (length // 2 - 1), 10 ** (length // 2)) | |
return [f"{i}{str(i)[::-1]}" for i in r] if length % 2 == 0 else [f"{i}{j}{str(i)[::-1]}" for i in r for j in range(10)] | |
symmetry_templates = [ | |
lambda *x: f"{x[0]}{x[0]}", | |
lambda *x: f"{x[0]}{x[1]}{x[0]}", | |
lambda *x: f"{x[0]}{x[1]}{x[1]}{x[0]}", | |
lambda *x: f"{x[0]}{x[1]}{x[2]}{x[1]}{x[0]}", | |
] | |
for subsequence_length in range(3, max_length // 2 + 1): | |
for number_of_subsequences in range(2, max_length // subsequence_length + 1): | |
length = subsequence_length * number_of_subsequences | |
if length < min_length: continue | |
for combo in itertools.product(generate_palindromes_of_length(subsequence_length), repeat = (number_of_subsequences + 1) // 2): | |
num = symmetry_templates[number_of_subsequences - 2](*combo) | |
if length < max_length or int(num) < 2100000000000000: print(num) | |
generate_palindromes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment