Last active
March 27, 2021 19:19
-
-
Save HaraldKorneliussen/bc544dcfcdd905a16d9b9a020ae8a7f5 to your computer and use it in GitHub Desktop.
Small program for generating two permutations suggested by Hugo van der Sanden on seqfan mailing list
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
#!/usr/bin/python | |
import re | |
# Encode a natural number (incl 0) as a bijective number (string) | |
def enc(num, symbols="01"): | |
ret = '' | |
slen = len(symbols) | |
while num > 0: | |
num -= 1 | |
ret = symbols[num % slen] + ret | |
num = num // slen | |
return ret | |
# Decode a bijective number (string) as a regular natural number | |
def dec(s, symbols="01"): | |
acc = 0 | |
for c in s: | |
acc *= len(symbols) | |
acc += (symbols.index(c) + 1) | |
return acc | |
def hugo_a(s): | |
if re.fullmatch('0+', s): | |
return '1'*(len(s) - 1) + '0' | |
else: | |
m = re.fullmatch('0(0*)1(.*)', s) | |
if m: | |
return (len(m[1]) * '1') + '0' + m[2] + '0' | |
else: | |
m = re.fullmatch('1(0*)1(.*)', s) | |
if m: | |
return m[2] + '0' + '1'*(len(m[1]) + 1) | |
else: | |
m = re.fullmatch('1(0*)', s) | |
if m: | |
return '1' * len(m[0]) | |
else: | |
return "Pattern not matched?" | |
def hugo_b(s): | |
if re.fullmatch('0+', s): | |
return '1'*(len(s) - 1) + '0' | |
else: | |
m = re.fullmatch('0(0*)1(.*)', s) | |
if m: | |
return m[2] + '0' + len(m[1]) * '1' + '0' | |
else: | |
m = re.fullmatch('1(.*)1(0*)', s) | |
if m: | |
return m[1] + '0' + len(m[2]) * '1' + '1' | |
else: | |
m = re.fullmatch('1(0*)', s) | |
if m: | |
return '1' * len(m[0]) | |
else: | |
return "Pattern not matched?" | |
def take_n(f, n): | |
return [dec(f(enc(i))) for i in range(1,n)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment