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/ArchCypher Feb 22 '21 edited Feb 22 '21
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.