First of all, what about the one liner? Your supposed to write bad code. However the only think wrong is that you are not using string.ascii_lowercase, which is too little.
Now for the math: Your approach is actually quite interesting, turning O(n^2) into O(n+26) with constant memory usage.
To make it work, you just need to generate the first 255 primes and index with ord(c.lower()) into the primes list. Then this would actually be good code. Again you failed the challenge.
I'll admit the one-liner is probably pretty alright 😅
As for the math, it's 'bad code' because -- in an attempt to be very clever -- it creates code that can break on random and difficult to predict strings. Bury this in a library somewhere, and you have a recipe for very confusing errors.
If I combined your primes idea (nice one!), then this code might be a genuinely clever solution for writing this algorithm in ~O(n). But seeing as a haven't implemented it, I think this safely falls under code that's too clever for its own good.
Edit:
Actually, thinking about it more, there are other hidden problems with this solution. For instance, long enough text will quickly cause an integer overflow ( image a thousand a's -- that's 97**1000!!) -- breaking the algorithm. Additionally, I think it's possible to come up with a algorithm that completes this task in < On(n) on average, so O(n) isn't that great of a target to begin with.
View all comments
Show parent comment
1
u/Oshgnacknak Feb 22 '21
First of all, what about the one liner? Your supposed to write bad code. However the only think wrong is that you are not using
string.ascii_lowercase, which is too little.Now for the math: Your approach is actually quite interesting, turning
O(n^2)intoO(n+26)with constant memory usage.To make it work, you just need to generate the first 255 primes and index with
ord(c.lower())into the primes list. Then this would actually be good code. Again you failed the challenge.