Using SCALA programming aDevelop a function named convert th
Using SCALA programming
a.Develop a function, named convert, that takes a list of integers (you may assume only 0s and 1s) and returns the list of Boolean equivalents (i.e., 1 for true, 0 for false). For example, if you evaluate convert(List[1, 1, 0, 1]) it would return [true, true, false, true].
b. Develop a function, named howManyPassed, that takes a list of Strings (you may assume only “A”, “B”, “C”, “D” and “F” are contained in the list) and returns an integer for how many “C” or better grades are contained in the list. For example, if you evaluate howManyPassed(List[“A”, “A”, “C”, “D”, “B”, “F”]) it would return 4. Hint, you may use length function, with a syntax of aList.length, to calculate the length of a list.
c. Develop a function, named cipher, that takes a Char and returns a Char according to the following rules: ‘a; is replaced by ‘e’; ‘e is replaced by ‘a’; ‘k’ is replaced by ‘c’; ‘c’ is replaced by ‘k’; ‘s’ is replaced by ‘z’; ‘z’ is replaced by ‘s’. Additionally, develop a function, named jumble, that takes a String and returns a String with the cipher applied to each character. Hint, to convert a String to a List of Char, you can use toList and to convert a List of Char to a String, you can use mkString. For example, if you evaluate jumble(“This class is fun”) would return “Thiz klazz iz fun”.
d. Collatz. Develop a function, named collatz, tests the collatz conjecture. That is, your function should take an integer parameter and return an integer list of the collatz sequence starting with the integer parameter. A collatz sequence is constructed as follows: given an integer, n, if n is even, divide it by 2; if n is odd, multiple it by 3 and add 1 (i.e., 3n + 1); repeat this process until you reach
Solution
Please find below the answers to your assignment:
a.
scala> implicit def int2bool(i: Int) = i==0
int2bool: (i: Int)Boolean
b.
scala> import scala.util.Try
import scala.util.Try
scala> def tryToInt( s: String ) = Try(s.toInt).toOption
tryToInt: (s: String)Option[Int]
scala> tryToInt(\"789\")
res0: Option[Int] = Some(789)
scala> tryToInt(\"\")
res1: Option[Int] = None
c.
object Cipher {
private val alphaU=\'A\' to \'Z\'
private val alphaL=\'a\' to \'z\'
def encode(text:String, key:Int)=text.map{
case c if alphaU.contains(c) => rot(alphaU, c, key)
case c if alphaL.contains(c) => rot(alphaL, c, key)
case c => c
}
def decode(text:String, key:Int)=encode(text,-key)
private def rot(a:IndexedSeq[Char], c:Char, key:Int)=a((c-a.head+key+a.size)%a.size)
}
val text=\"Today is the Election Day\"
println(\"Plaintext => \" + text)
val encoded=Cipher.encode(text, 4)
println(\"Ciphertext => \" + encoded)
println(\"Decrypted => \" + Caesar.decode(encoded, 4))
d.
function collatz(n)
while n > 1
show n
if n is odd then
set n = 3n + 1
else
set n = n / 2
endif
endwhile

