Scala Programming: Check a given list is a palindrome or not
Scala Programming List Exercise-17 with Solution
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?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/scala-exercises/list/scala-list-exercise-17.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics