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

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 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...
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 :(...
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?
Scala: Write and test a Program as elegantly as possible Given a piece of text, create...
Scala: Write and test a Program as elegantly as possible Given a piece of text, create a histogram of letter pairs (order from high to low). For instance, for the text, “this is a good thing”, the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a) The histogram will be: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1 Sample Input/Output: Enter text: this is...
Scala: Write and test a program as elegantly as possible You are given a puzzle like...
Scala: Write and test a program as elegantly as possible You are given a puzzle like this: 7 __ 10 __ 2 Each blank may be filled with a ‘+’ (plus) or ‘-’ (minus), producing a value k. For all combinations of plus and minus, find the value of k that is closest to 0. In the above case, there are 4 combinations, each producing a different value: 7 + 10 + 2 = 19 7 + 10 - 2...
Please write code in SCALA Thanks in advance Use curried representation and write system which convert...
Please write code in SCALA Thanks in advance Use curried representation and write system which convert pressure units. Input: pressure in atm (atmosphere) Outputs: PSI, bar, torr 1 atm = 14.6956 psi = 760 torr = 101325 Pa = 1.01325 bar.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT