Created
June 7, 2015 21:28
-
-
Save hhimanshu/d5498a2aeae7f28ccf7e to your computer and use it in GitHub Desktop.
P06 (*) Find out whether a list is a palindrome.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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