Question

In: Computer Science

2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of...

2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of a word is defined as the sum of the values of the individual letters of the word with value of "a" being 1, "b" being 2 etc. Upper and lower case letters have the same value. You can assume that the word consists of just letters. Here is the signature of the function:

def wordValue(s: String): Int = ???

For example, wordValue("Attitude") = 100. No loops/iterations allowed. Use recursion. Submit just the code for the function and any helpers you develop. We will call your function with our test cases.

Solutions

Expert Solution

Here iam providing the answer.Hope this helps you.If you have any doubts comment me i will clarify you.Please upvote,If you like my work.Thank you :- )

Here is the Scala script:-

object MyClass {
    def wordValue(x:String): Int={
        var len=x.length(); // to find the length of the string
        if(len==0){ // if it is empty string return 0
            return 0;
        }
        else{ // else convert str into lower case
            var str = x.toLowerCase();
            var first=str.charAt(0);//finding the first position character
            var result=first.toInt-96; //find the result
            return result+wordValue(x.slice(1,len)); //function calling recursively with 1st index to last
        }
    };

    def main(args: Array[String]) {
        println(wordValue("Attitude"))
    }
}

Output:-


Related Solutions

Please write code in Scala Write a function which check that numbers in provided list are...
Please write code in Scala Write a function which check that numbers in provided list are correctly sorted. Please use tail recursion
Write a program to compute the root of the function f(x) = x3 + 2 x2...
Write a program to compute the root of the function f(x) = x3 + 2 x2 + 10 x - 20 by Newton method ( x0 =2 ). Stop computation when the successive values differ by not more than 0.5 * 10-5 . Evaluate f(x) and f '(x) using nested multiplication. The output should contain: (1) A table showing at each step the value of the root , the value of the function,and the error based upon successive approximation values...
In Scala, what is First-order Function, Recursion, and Immutability? How to write them? Please provide some...
In Scala, what is First-order Function, Recursion, and Immutability? How to write them? Please provide some examples and details to explain it. Also, what will be the benefits of using them when writing codes.
Write a function bracket_by_len that takes a word as an input argument and returns the word...
Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space...
Write a function checkvertical(board, word, row, col), which returns True if the word word can be...
Write a function checkvertical(board, word, row, col), which returns True if the word word can be added to the board starting at position board[row][col] and going down. The restrictions are (1) it has to fit entirely on the board, (2) it has to intersect and match with one or more non-blank letters on the board, and (3) it cannot change any letters on the board, other than blanks (that is, overlapping mismatches are not allowed). My program isn't working :(...
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word...
IN PYTHON 1) Pig Latin Write a function called igpay(word) that takes in a string word representing a word in English, and returns the word translated into Pig Latin. Pig Latin is a “language” in which English words are translated according to the following rules: For any word that begins with one or more consonants: move the consonants to the end of the word and append the string ‘ay’. For all other words, append the string ‘way’ to the end....
Compute the expenditure function for the perfect complements utility function. Then compute the expenditure function for...
Compute the expenditure function for the perfect complements utility function. Then compute the expenditure function for the perfect substitutes utility function. Do your results make sense?
7. Write a function that accepts a sentence as the argument and converts each word to...
7. Write a function that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT