Question: NEED HELP ASAP!!!!!! Problem 1 (5 points) Write a scala function titled isPalindrome (s) that tests whether a given input string s is a palindrome.

NEED HELP ASAP!!!!!!

Problem 1 (5 points)

Write a scala function titled isPalindrome (s) that tests whether a given input string s is a palindrome.

The following string operations will be helpful.

  1. s.size gives you the size of the string.
  2. s(i) gives you the ith character for i = 0, ... , s.size-1
  3. For loops in scala are described here: https://www.tutorialspoint.com/scala/scala_for_loop.htm Pay careful attention between the to and until forms of the for loop.

In [ ]:

def isPalindrome(s: String): Boolean = { 
 ??? // YOUR CODE HERE
 } 
 

In [ ]:

//BEGIN TEST
assert(isPalindrome("hellolleh"), "Test 1 Failed")
assert(!isPalindrome("bababablacksheep"), "Test 2 Failed")
assert(isPalindrome(""), "Test 3 Failed")
assert(isPalindrome("a"), "Test 4 Failed")
assert(isPalindrome("xxxxxxxx"), "Test 5 Failed")
assert(isPalindrome("ablewasiereisawelba"), "Test 6 Failed")
passed(5)
//END TEST

Problem 2 (10 points)

Implement a isPalindromeRec function that checks if a given string is a palindrome, but without using mutable var or without using for/while loops. Your recursive function should use the following high level algorithm:

  • Strings of length 0 (empty string) or 1 (single char) are by default palindrones. (This is your base case).
  • If the first and last characters of the string are the same, the string if a palindrome if the substring between second and last but one characters is itself a palindrome (use the recursive call to check).
  • Or else, the string cannot be a palindrome.

Hint: You should consult Scala documentation on strings to figure out: https://www.tutorialspoint.com/scala/scala_strings.htm

  1. How do we get first and last characters of a string.
  2. How do we get substring between first and last character.
  3. How to find out if a string is empty or a single character.

In [ ]:

def isPalindromeRec(s: String): Boolean = {
 ??? // YOUR CODE HERE
}

In [ ]:

//BEGIN TEST
testWithMessage(isPalindromeRec("hellolleh") == true, "1")
testWithMessage(isPalindromeRec("bababablacksheep") == false, "2")
testWithMessage(isPalindromeRec("") == true, "3")
testWithMessage(isPalindromeRec("a") == true, "4")
testWithMessage(isPalindromeRec("xxxxxxxx") == true, "5")
testWithMessage(isPalindromeRec("ablewasiereisawelba") == true, "6")
passed(5)
//END TEST

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!