Skip to content

Instantly share code, notes, and snippets.

@hhimanshu
Created June 7, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhimanshu/d5498a2aeae7f28ccf7e to your computer and use it in GitHub Desktop.
Save hhimanshu/d5498a2aeae7f28ccf7e to your computer and use it in GitHub Desktop.
P06 (*) Find out whether a list is a palindrome.
package com.learner.s99
/**
* P06 (*) Find out whether a list is a palindrome.
* Example:
* scala> isPalindrome(List(1, 2, 3, 2, 1))
* res0: Boolean = true
*/
object P06 {
def isPalindrome[T](l:List[T]):Boolean = l == l.reverse
}
package com.learner.s99
import com.learner.s99.P06.isPalindrome
import org.scalatest.{MustMatchers, FlatSpec}
class P06Spec extends FlatSpec
with MustMatchers {
behavior of "A List"
it must "return 'true' for List(1,2,3,2,1) as palindrome" in {
isPalindrome(List(1, 2, 3, 2, 1)) mustBe true
}
it must "return 'true' for empty list as palindrome" in {
isPalindrome(List()) mustBe true
}
it must "return 'false' for List(1,2,3,2,1) as palindrome" in {
isPalindrome(List(1, 2, 1, 3, 2, 1)) mustBe false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment