Palindrome pairs: Difference between revisions

Same wording appears on leetcode.
m (→‎{{header|Perl 6}}: Slightly better palindrome detection. Ignore, case, punctuation and white-space. Same output)
(Same wording appears on leetcode.)
 
(7 intermediate revisions by 4 users not shown)
Line 1:
Task deleted pending original task writer giving some indication that the same task on leetcode and here have no plagiarism issues.
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.
<br>
Input: ["abcd","dcba","lls","s","sssll"]
<br>
Output:
<br>
(abcd,dcba)
<br>
(dcba,abcd)
<br>
(s,lls)
<br>
(lls,sssll)
<br>
{{draft task}}
 
=={{header|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>
{{Out}}
<pre>(abcd,dcba)
(dcba,abcd)
(s,lls)
(lls,sssll)</pre>
 
=={{header|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:
<pre>
(abcd,dcba)
(dcba,abcd)
(s,lls)
(lls,sssll)
</pre>
Anonymous user