Scala Programming: Check a given list is a palindrome or not
Write a Scala program to check a given list is a palindrome or not.
Sample Solution:
Scala Code:
object Scala_List {
def is_Palindrome[A](list_nums: List[A]):Boolean = {
list_nums == list_nums.reverse
}
def main(args: Array[String]): Unit = {
println("Result: " + is_Palindrome(List(1,2,3,4,3,2,1)));
println("Result: " + is_Palindrome(List(1,2,3)));
}
}
Sample Output:
Result: true Result: false
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to reverse a given list.
Next: Write a Scala program to flatten a given List of Lists, nested list structure.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics