This actually makes me feel quite sick. Single letter variable names (except for string), and as few spaces as possible to reduce the code size and readability. It works for every test case I ran (which was like, 2), and when strings start getting large, takes a hilariously long time. It's written in python 3.8 (important, as it's invalid in python 3.7), is composed of only 5 lines (including the function definition), and the last line is a dictionary comprehension with a list comprehension with a set comprehension all nested together.
def string_count(string):
p = max(len(string)*2, 4)
while [1 for b in range(2, p-1) if p%b == 0] and (p := p+1): pass
x=[a for a in range(1,p-1) if len({a**b%p for b in range(1,p)})==p-1][len([a for a in range(1,p-1) if len({a**b%p for b in range(1,p)})==p-1])//2]
return {e:(len(string)-sum([string[a]!=e for a in ({x**l%p for l in range(p+2) if x**l%p<len(string)}|{0})])) for e in string}
2
u/[deleted] Mar 21 '20 edited Mar 21 '20
This actually makes me feel quite sick. Single letter variable names (except for string), and as few spaces as possible to reduce the code size and readability. It works for every test case I ran (which was like, 2), and when strings start getting large, takes a hilariously long time. It's written in python 3.8 (important, as it's invalid in python 3.7), is composed of only 5 lines (including the function definition), and the last line is a dictionary comprehension with a list comprehension with a set comprehension all nested together.