Advertisement
joelnazarene

joescal

Sep 18th, 2024
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 7.71 KB | Source Code | 0 0
  1. //////////
  2. ///////
  3.  
  4. object ListMapHigherOrder{
  5.  def main(args :Array[String]){
  6.    
  7. def tos(arg : String) : Int = arg.toInt
  8.     //val input = args(0).toInt
  9.   val intList1 :List[String] = args.toList
  10.  //  for(a <- intList1){
  11.  
  12.    
  13.  
  14. //}
  15.  
  16.   val intList :List[Int] = intList1.map(tos) //write code to convert args to list of integers
  17.   //println(intList)
  18.     def isPerfectNumber(i : Int)  {  
  19.         var a =0
  20.         var sum=0
  21.         for(a <- 1 to  i/2 if i%a == 0 ){
  22.            
  23.             sum =sum + a
  24.  
  25.         }
  26.     if(sum == i)
  27.         {
  28.           "true"
  29.  
  30.         }else{
  31.  
  32.         "false"
  33.         }
  34.  
  35.     }//write code to find is a number is perfect or not
  36. //isPerfectNumber(6)
  37. def myHigherOrderFunction(argFn: Int => String, n :  List[Int] ): List[String] =
  38. {
  39. var resultList = n.map(argFn)
  40.  resultList
  41. }
  42. //println(myHigherOrderFunction(isPerfectNumber, intList))
  43. }
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. /*
  59.  
  60. def isPerfectNumber //write code to find is a number is perfect or not
  61.   }
  62.   def myHigherOrderFunction //complete the higher order function
  63.   }
  64.   println(myHigherOrderFunction(isPerfectNumber, intList))
  65.  
  66.  
  67.  
  68.  
  69. object ListHigherOrderFunction{
  70.  def main(args :Array[String]){
  71.   val input = args(0).toInt
  72.     var fac=1
  73.  val intList :List[Int]= List(1,2,3,4,5,6,7,8,9,10)
  74.   val numberList = intList.slice(0,input) //complete the code
  75.   def factorial(i : Int): Unit ={
  76.     //var fac=1
  77.     if( i > 1 ){
  78.         fac = fac*i  
  79.         factorial(i-1)
  80.       }
  81.    
  82.     if(i==1){
  83.         println(fac)
  84.          fac =1
  85.        }
  86. } //complete the code
  87. //factorial(input)
  88.  
  89.  
  90.  
  91. def myHigherOrderFunction(argFn: Int => Unit, n :  List[Int] ): Unit =
  92. {
  93. val resultList = n.map(argFn)
  94. //println(resultList)
  95. }
  96. myHigherOrderFunction(factorial, numberList)
  97. //println(factorial(input))
  98. }
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  var fac=1;
  106.     for( a <- 1 to i){
  107.          fac = fac*a  
  108.       }
  109.  
  110.     fac
  111.  
  112.  
  113. trait ArithmeticOperations {
  114. def x = 0//: Int = { 0 }
  115. def y = 0//: Int = { 0 }
  116. def add() : Int = { 0 }
  117. def subract() : Int = { 0 }
  118. def multiply() : Int = { 0 }
  119. def divide() : Int = { 0 }
  120. }//complete the code
  121.  
  122.  
  123.  
  124. class Variables(i : Int , j : Int) extends ArithmeticOperations {
  125. override def x = i
  126. override def y = j
  127. override def add() : Int = { i+j }
  128. override def subract() : Int = { i-j }
  129. override def multiply() : Int = { i*j }
  130. override def divide()  : Int = { i/j }
  131. } //complete the code
  132.  
  133. object TraitExample extends App{
  134.  val input1 = args(0).toInt
  135.  val input2 = args(1).toInt
  136.  var variable = new Variables(input1, input2)
  137.  println(variable.add())
  138.  println(variable.subract())
  139.  println(variable.multiply())
  140.  println(variable.divide())
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. abstract class Expression
  148. case class Var(name: String) extends Expression
  149. case class Number(num: Double) extends Expression
  150. case class UnOp( var operator: String, arg: Expression) extends Expression
  151. case class BinOp( var operator: String, left: Expression, right: Expression) extends Expression
  152. class b(a :Int , b :Int){
  153.  
  154. def bb() : String = { "blaah"}
  155.  
  156.  
  157.  
  158. }
  159. object CaseClassMatching extends App {
  160.    
  161.    
  162.   def describe(o: Any) : Unit ={
  163.  
  164.     o match {
  165.     case i: String if i  == i + 0 => println("It is a number")
  166.     //case i: Int if i >= 0 => println("It is a number")
  167.     case d: Unit if d.toString == d => println("It is a string expression")
  168.     //case d: Double if d >= 0.0 => println("Double is given")
  169.    
  170.  
  171.  //case d: Unit if o.operator !=   => println("It is a unary operation")
  172.  //case d: Unit if o.left != null => println("It is a binary operation")
  173.  
  174.     case _ => println("Invalid Expression")
  175.   }
  176. val op = BinOp("+", Number(1), Number(4))
  177. println(op)
  178. val a = new b(1,2)
  179. a.bb()
  180.  
  181. //describe(op)
  182.  
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  //write your code here
  192.      }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. ////////////////////////////////////
  199.  
  200. class Rectangle(input11 : Int , input22 : Int){
  201.  //write your code here
  202. def input1 = input11
  203. def input2 = input22
  204. def area() : Unit = println(input1*input2)
  205. def perimeter() : Unit = println(2*(input1+input2))
  206.  
  207.  
  208.  }
  209.  
  210. object RectangleDemo extends App{
  211.  val input1 :Int = args(0).toInt
  212.  val input2 :Int = args(1).toInt
  213.    //def main(args :Array[String]){ //write your code here
  214.  
  215.  
  216. val h1=new Rectangle(input1,input2)//.area(input1,input2)
  217.     h1.area()
  218.     h1.perimeter()
  219.  
  220.  
  221. //}
  222. }
  223.  
  224.  
  225. object PatternMatchingType{
  226.  def main(args :Array[String]){
  227.   val input1 = args(0).toInt
  228.   val initial = "12345"
  229.   var change : Any = 0
  230.  input1 match {
  231.   case i if i == 1 => initial.toInt
  232.   case i if i == 2 => initial.toDouble
  233.   case _ => change = initial
  234.  
  235. }
  236.  
  237. def bigger(o: Any): Any = {
  238.   o match {
  239.     case i: Int if i < 0 => println("Integer is given")
  240.     case i: Int if i >= 0 => println("Integer is given")
  241.     case d: Double if d < 0.0 => println("Double is given")
  242.     case d: Double if d >= 0.0 => println("Double is given")
  243.     case _ => println("String is given")
  244.   }
  245. }
  246.  
  247. bigger(change)
  248.  
  249. //print Integer is given if integer, Double is given if double and String is given if String.
  250.  
  251.  
  252.  
  253.  
  254.   println(change.getClass)
  255. }
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. /*
  269.  
  270.  
  271. object PatternMatching{
  272.  def main(args :Array[String]){
  273.   val input :Int = args(0).toInt
  274.   input match {
  275.   case i if i%2 == 0 => println("Even number is given")
  276.   case i if i%2 != 0 => println("Odd number is given")
  277.   case i if i <= 0 => println("Negative/Zero is input")
  278. }
  279. times match {
  280.   case i if i == 1 => "one"
  281.   case i if i == 2 => "two"
  282.   case _ => "some other number"
  283. }
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. //write your code here
  291. }
  292. }
  293.  
  294.  
  295.  
  296. object flatMapUsage{
  297.  def main(args :Array[String]){
  298.   val input :Int = args(0).toInt
  299.   val sampleList :List[Int] = List(1,2,3,4,5,6,7,8,9,10)
  300.  
  301.   val numberList = sampleList.slice(0,input)//write your code
  302.   def listGenerator(arg: Int): List[Int] = { sampleList.slice(0,arg) } //write your code
  303.   val resultList = numberList.flatMap(listGenerator)//write your code
  304.   println(resultList)
  305.   println(resultList.length)
  306.  }
  307. }
  308.  
  309.  
  310.  
  311. object mapFunction{
  312.  def main(args :Array[String]){
  313.   val input = args(0).toInt
  314. val sampleList :List[Int] = List(0,1,2,3,4,5,6,7,8,9,10)
  315.   val numberList = sampleList.slice(0,input) //complete the code  
  316.  
  317.  
  318.  
  319.   def incrementer(arg: Int): Int = { arg + 5 } //complete the code
  320.   val incrementList =numberList.map(incrementer) //complete the code
  321.   println(incrementList)
  322.  }
  323. }
  324.  
  325.  
  326. object ListFilter{
  327.  def main(args :Array[String]){
  328.   val input = args(0).toInt
  329. val sampleList :List[Int] = List(0,1,2,3,4,5,6,7,8,9,10)
  330.   val numberList = sampleList.slice(0,input) //complete the code
  331.   val evenList = numberList.filter(i => i%2 == 0)//complete the code
  332.   println(evenList)
  333.  }
  334. }
  335.  
  336.  
  337.  
  338.  
  339. object JESUS{
  340.  def main(args :Array[String]){
  341.   println("GOD is an AMAZING FATHER")
  342. def addOne(arg: Int): Int = { arg + 1 }
  343. object FuncLiterals{
  344.  def main(args : Array[String]){
  345.   val a : Int = args(0).toInt
  346.   val b : Int = args(1).toInt
  347.   var multiply = (x : Int,y :Int) => x*y  //write your function literal here
  348.   println(multiply(a,b))
  349.  }
  350. def addOne(a: Int , b : Int): Int = { a*b }
  351. }
  352.  }
  353. }
  354. object HigherOrderFunction{
  355.  def main(args :Array[String]){
  356.   val input : Int = args(0).toInt
  357.   println(myHigherOrderFunction(addOne, input))
  358.  def addOne(arg: Int): Int = { arg * 9 }
  359.     def myHigherOrderFunction(fn: Int => Int,arg : Int): Int = { fn(arg)} //complete the code
  360.  }
  361. }
  362.  
  363. object ListFunctions{
  364.  def main(args :Array[String]){
  365.   val sampleList :List[Int] = List(1,2,3,4,5,6,7,8,9,10)
  366.     val input :Int = args(0).toInt
  367.     var newlist: List[Int] = sampleList.slice(0,input)
  368.    
  369.    
  370.     println(newlist)  
  371.    
  372.  
  373.     def tos(arg : Int) : String = arg.toString
  374.     var nlist : List[String] = newlist.map(tos)
  375.     println(nlist)
  376.  
  377.     //write your code here
  378.  }
  379. }
  380.  
  381.  
  382. */
  383. /////////////////////////////////
  384.  
  385. */
  386.  
  387.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement