Semordnilap: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured, made p2js compatible) |
Keithpinson (talk | contribs) (Updated for compatibility with Scala 3) |
||
Line 3,112: | Line 3,112: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang scala>val wordsAll = |
|||
⚫ | |||
scala.io.Source. |
|||
⚫ | |||
getLines().map(_.toLowerCase).toIndexedSeq |
|||
/** |
/** |
||
Line 3,119: | Line 3,123: | ||
* words are different. |
* words are different. |
||
*/ |
*/ |
||
def semordnilap( words: |
def semordnilap( words:IndexedSeq[String] ) : IndexedSeq[(String,String)] = { |
||
words. |
|||
zipWithIndex. // index will be needed to eliminate duplicate |
zipWithIndex. // index will be needed to eliminate duplicate |
||
filter { |
filter { |
||
case (w,i) => |
case (w,i) => |
||
val j = words.indexOf(w.reverse) // eg. (able,62) and (elba,7519) |
val j = words.indexOf(w.reverse) // eg. (able,62) and (elba,7519) |
||
i < j && w != w.reverse // save the matches which are not palindromes |
i < j && w != w.reverse // save the matches which are not palindromes |
||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
} |
} |
||
Line 3,137: | Line 3,140: | ||
{ |
{ |
||
println( ss.size |
println( s"${ss.size} matches, including: \n" ) |
||
println( ss.take(5).mkString( "\n" ) ) |
println( ss.take(5).mkString( "\n" ) ) |
||
}</lang> |
}</lang> |