4 ms·
Easy job for a regex: while ($string =~ /((\w+)\w?(??{reverse $2}))/g) { print "$1\n"; }
by staunch 16y ago
Easy job for a regex:
while ($string =~ /((\w+)\w?(??{reverse $2}))/g) { print "$1\n"; }
- dantle 16y agofuckin nice. now I feel like a doofus for using ruby, when I thought it was straightforward.
- manveru 16y agoI've done it ruby this way: text.scan(/(.)(.)(.)(.)(\3)(\2)(\1)/)
- Natsu 16y agoThat's pretty frail. It won't catch palindromes with an even number of letters (e.g. ababbaba) and it'll only find those of at most 7 characters. Granted, you can use those matches to work outwards and search for more, but I didn't think the general solution was all that hard. Oddly enough, it didn't even occur to me to try a regex and I like using them quite a bit.