Palindrome pairs: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Slightly better palindrome detection. Ignore, case, punctuation and white-space. Same output) |
||
Line 18: | Line 18: | ||
<lang perl6>my @words = <abcd dcba lls s sssll>; |
<lang perl6>my @words = <abcd dcba lls s sssll>; |
||
sub is-palindrome (Str $s) { $ |
sub is-palindrome (Str $s) { $_ eq .flip given $s.lc.comb(/\w/).join } |
||
for @words.combinations(2) { |
for @words.combinations(2) { |
Revision as of 15:45, 27 July 2018
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
Input: ["abcd","dcba","lls","s","sssll"]
Output:
(abcd,dcba)
(dcba,abcd)
(s,lls)
(lls,sssll)
Palindrome pairs is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Perl 6
<lang perl6>my @words = <abcd dcba lls s sssll>;
sub is-palindrome (Str $s) { $_ eq .flip given $s.lc.comb(/\w/).join }
for @words.combinations(2) {
say "({.join: ','})" if .join.&is-palindrome; say "({.reverse.join: ','})" if .reverse.join.&is-palindrome;
}</lang>
- Output:
(abcd,dcba) (dcba,abcd) (s,lls) (lls,sssll)
Ring
<lang ring>
- Project : Palindrome pairs
- Date : 2018/07/27
- Author : Gal Zsolt (~ CalmoSoft ~)
- Email : <calmosoft@gmail.com>
load "stdlib.ring" pairs = ["abcd","dcba","lls","s","sssll"] for n = 1 to len(pairs)-1
for m = n + 1 to len(pairs) pal1 = pairs[n] + pairs[m] pal2 = pairs[m] + pairs[n] if palindrome(pal1) see "(" + pairs[n] + "," + pairs[m] + ")" + nl ok if palindrome(pal2) see "(" + pairs[m] + "," + pairs[n] + ")" + nl ok next
next </lang> Output:
(abcd,dcba) (dcba,abcd) (s,lls) (lls,sssll)