Question

in Scala programming,

CHALLENGE 1: This challenge uses the inbuilt List type in Scala k/ object Challenge1 f /ok A palindrome is a list that is the

I've done isPalindrome function, but I get stuck with following problem.

ok Make this function return true if every entry is bigger than its position in the list. So, List(1, 2, 3) would return true

let me know how to improve this, please in order to pass the test class following

it should determine if list entries are bigger than their index in t Challenge1.entriesBiggerThanIndex(List(1, 2, 3)) shoul

CHALLENGE 1: This challenge uses the inbuilt List type in Scala k/ object Challenge1 f /ok A palindrome is a list that is the same forwards as backwards. eg, List(1, 2, 2, 1) Make this function return true if a list is a palindrome. Note, you ARE permitted to use the inbuilt reverse method on Lists. k/ def isPalíndrome [T] (list: List [T]) : Boolean = { list matcht case Nil » true case head :: Nil => true case (list.headlist.last) && isPalindrome(list.tail.reverse.tail.reverse)
ok Make this function return true if every entry is bigger than its position in the list. So, List(1, 2, 3) would return true but List(1, 2, 2) would return false because the last entry is at position 2 (remember, lists are zero-indexed) but its value (exactly 2) is not bigger than 2 You will find list.zipwithIndex and list.forall useful def entriesBiggerThanIndex(list:List [Int]): Boolean val list-(1 to ) 50 List.zipwithIndex
it should "determine if list entries are bigger than their index" in t Challenge1.entriesBiggerThanIndex(List(1, 2, 3)) should be (true) Challenge1.entriesBiggerThanIndex(List()) should be (true) Challenge1.entriesBiggerThanIndex(List(0, 1, 2)) should be (false) Challenge1.entriesBiggerThanIndex(List(0, 0, 3)) should be (false) Challenge1.entriesBiggerThanIndex((1 to 50).toList ++ (1 to 50).reverse.toList) should be (false)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lets first see what will the function entriesBiggerThanIndex will do in your case:

def entriesBiggerThanIndex(list: List[Int]): Boolean val list(1 to 50) list.zipWithIndex It will return a List of tuple 2 in

Correct Implementation:

def entriesBiggerThanIndex(list: List[Int]): Boolean { ExampLe: list List(1,2,3) list.zipWithIndex will give List((1,0), (2,1

Output:

println(Challenge1.entriesBiggerThanIndex(List(1,2,3))) println(Challenge1.entriesBiggerThanIndex(List))) println(Challenge1.

Add a comment
Know the answer?
Add Answer to:
In Scala programming, I've done isPalindrome function, but I get stuck with following problem. ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in Scala programming, I need to solve the problem following, in order to pass the test...

    in Scala programming, I need to solve the problem following, in order to pass the test class following, could you please implement that function? sjok A little tricker. Make this function return true if every second entry in a list forms a palindrome. eg,List(99, 1,98, 2, 97, 2, 96, 2, 95, 1) is a palindrome because every second element forms List(1, 2, 2, 1) which is a palindrome You might find zipwithIndex and filter useful to extract every second element....

  • python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...

    python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as argument and returns True if that string (ignoring spaces) is a palindrome. A palindrome is a word or phrase that spells the same thing forwards as backwards (ignoring spaces). Your program should use a recursive process to determine the result, by comparing the first and last letters of the string, and if necessary, calling ispalindrome() on the rest of the string. Sample run: print(ispalindrome('never...

  • The code: def isPalindrome(text): """ >>> isPalindrome("alula") True...

    The code: def isPalindrome(text): """ >>> isPalindrome("alula") True >>> isPalindrome("love") False >>> isPalindrome("Madam") True >>> isPalindrome(12.5) False >>> isPalindrome(12.21) False >>> isPalindrome("Cigar? Toss it in a can.! It is so tragic.") True >>> isPalindrome("travel.. a town in Alaska") False """ # --- YOU CODE STARTS HERE if type(text) is not str: return False l = len(text) - 1 i = 0 while i < l: if text[i] in string.punctuation or text[i] == ' ': i += 1 elif text[l] in...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome...

    PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome is a string of characters that is read the same forwards as it is backwards. For example, racecar' is a palindrome; reversing the order of the characters leaves the string unchanged. Write a otherwise. function called isPalindrome that accepts one input and returns one output. The input str is a string of characters, and the output is 1 if str is a palindrome, For...

  • Write a function check palindrome, which takes a string x as argument and which returns True...

    Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....

  • Write the function deep_contains. It takes as input a number and a list. That list might...

    Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT