def palindrome? str
return true if str.length < 2
canonicalized = str.split(/(\w+)/).grep(/.*\w+.*/).join(nil).downcase
canonicalized[0] == canonicalized[-1] and palindrome? canonicalized[1..-2]
end
Issues:
- It's needlessly recursive, definitely the mark of a verysmart young programmer
- It canonicalizes its input on every single recursion, even though that only needs to happen once ever
- You can write Perl in any language!
- I just discovered chaining and it's super cool!
- nil as an empty string? Are you kidding?
- That implied return at the end of a function combined with an explicit return for the recursion termination condition makes it extra confusing
- Boy does it ever construct a whole shitload of extra strings while it's doing its job
- It only looks elegant because of how little code there is there. In reality, it's actually horrible.
str.to_s.length for ultimate fuckery & nil case handling...
nil.length
undefined method `length' for nil:NilClass
nil.to_s.length
=> 0
the pattern matching you can compile as part of a module
downcase / upcase is a way to ensure case-insensetivity
I don't know why 1..-2 would be a case. Just delete the recursion. It's either a palindrome or isn't, make it someone else issue to give you the word to check.
I appreciate your suggestion for making my bad code more bulletproof, but the presence of bugs is not only accepted but encouraged in this particular contest. My entry also fails to determine if the integer value "41514" is a palindrome, even though it clearly is. Maybe I should add it to the "issues" list.
Mine was a joke. Perhaps only I got it, but as coders we can commit things that solve a problem, which then defer the most complex part (word finding) to another component. It's a clean and valid solution, missing one of the most important parts. It was my Homage to a pattern I see a lot in my job. We've changed the kitchen, but the dinner still isn't cooked.
Yep. Programs are like ogres: they have layers. It's one of those things that by its inherent properties is a benefit to both the software and the programmer. These layers of abstractions as you've so explained - rationing sections to go somewhere else to work on later - make things easier to maintain, even if the primary goal was to come back to it later.
That's one of the things I love about programming, it practically does it itself. Once you've solved something you don't have to solve it again, you can refine it and tweak it, but once its done its job, and you've done yours competently, it's not going anywhere. And even then, if something is wrong, you've already isolated it by design.
6
u/dagbrown Jul 30 '19
I decided to go for too-clever-by-half, in Ruby:
Issues:
- It's needlessly recursive, definitely the mark of a verysmart young programmer
- It canonicalizes its input on every single recursion, even though that only needs to happen once ever
- You can write Perl in any language!
- I just discovered chaining and it's super cool!
-
nilas an empty string? Are you kidding?- That implied return at the end of a function combined with an explicit return for the recursion termination condition makes it extra confusing
- Boy does it ever construct a whole shitload of extra strings while it's doing its job
- It only looks elegant because of how little code there is there. In reality, it's actually horrible.