Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////
- ///////
- object ListMapHigherOrder{
- def main(args :Array[String]){
- def tos(arg : String) : Int = arg.toInt
- //val input = args(0).toInt
- val intList1 :List[String] = args.toList
- // for(a <- intList1){
- //}
- val intList :List[Int] = intList1.map(tos) //write code to convert args to list of integers
- //println(intList)
- def isPerfectNumber(i : Int) {
- var a =0
- var sum=0
- for(a <- 1 to i/2 if i%a == 0 ){
- sum =sum + a
- }
- if(sum == i)
- {
- "true"
- }else{
- "false"
- }
- }//write code to find is a number is perfect or not
- //isPerfectNumber(6)
- def myHigherOrderFunction(argFn: Int => String, n : List[Int] ): List[String] =
- {
- var resultList = n.map(argFn)
- resultList
- }
- //println(myHigherOrderFunction(isPerfectNumber, intList))
- }
- }
- /*
- def isPerfectNumber //write code to find is a number is perfect or not
- }
- def myHigherOrderFunction //complete the higher order function
- }
- println(myHigherOrderFunction(isPerfectNumber, intList))
- object ListHigherOrderFunction{
- def main(args :Array[String]){
- val input = args(0).toInt
- var fac=1
- val intList :List[Int]= List(1,2,3,4,5,6,7,8,9,10)
- val numberList = intList.slice(0,input) //complete the code
- def factorial(i : Int): Unit ={
- //var fac=1
- if( i > 1 ){
- fac = fac*i
- factorial(i-1)
- }
- if(i==1){
- println(fac)
- fac =1
- }
- } //complete the code
- //factorial(input)
- def myHigherOrderFunction(argFn: Int => Unit, n : List[Int] ): Unit =
- {
- val resultList = n.map(argFn)
- //println(resultList)
- }
- myHigherOrderFunction(factorial, numberList)
- //println(factorial(input))
- }
- }
- var fac=1;
- for( a <- 1 to i){
- fac = fac*a
- }
- fac
- trait ArithmeticOperations {
- def x = 0//: Int = { 0 }
- def y = 0//: Int = { 0 }
- def add() : Int = { 0 }
- def subract() : Int = { 0 }
- def multiply() : Int = { 0 }
- def divide() : Int = { 0 }
- }//complete the code
- class Variables(i : Int , j : Int) extends ArithmeticOperations {
- override def x = i
- override def y = j
- override def add() : Int = { i+j }
- override def subract() : Int = { i-j }
- override def multiply() : Int = { i*j }
- override def divide() : Int = { i/j }
- } //complete the code
- object TraitExample extends App{
- val input1 = args(0).toInt
- val input2 = args(1).toInt
- var variable = new Variables(input1, input2)
- println(variable.add())
- println(variable.subract())
- println(variable.multiply())
- println(variable.divide())
- }
- abstract class Expression
- case class Var(name: String) extends Expression
- case class Number(num: Double) extends Expression
- case class UnOp( var operator: String, arg: Expression) extends Expression
- case class BinOp( var operator: String, left: Expression, right: Expression) extends Expression
- class b(a :Int , b :Int){
- def bb() : String = { "blaah"}
- }
- object CaseClassMatching extends App {
- def describe(o: Any) : Unit ={
- o match {
- case i: String if i == i + 0 => println("It is a number")
- //case i: Int if i >= 0 => println("It is a number")
- case d: Unit if d.toString == d => println("It is a string expression")
- //case d: Double if d >= 0.0 => println("Double is given")
- //case d: Unit if o.operator != => println("It is a unary operation")
- //case d: Unit if o.left != null => println("It is a binary operation")
- case _ => println("Invalid Expression")
- }
- val op = BinOp("+", Number(1), Number(4))
- println(op)
- val a = new b(1,2)
- a.bb()
- //describe(op)
- }
- //write your code here
- }
- ////////////////////////////////////
- class Rectangle(input11 : Int , input22 : Int){
- //write your code here
- def input1 = input11
- def input2 = input22
- def area() : Unit = println(input1*input2)
- def perimeter() : Unit = println(2*(input1+input2))
- }
- object RectangleDemo extends App{
- val input1 :Int = args(0).toInt
- val input2 :Int = args(1).toInt
- //def main(args :Array[String]){ //write your code here
- val h1=new Rectangle(input1,input2)//.area(input1,input2)
- h1.area()
- h1.perimeter()
- //}
- }
- object PatternMatchingType{
- def main(args :Array[String]){
- val input1 = args(0).toInt
- val initial = "12345"
- var change : Any = 0
- input1 match {
- case i if i == 1 => initial.toInt
- case i if i == 2 => initial.toDouble
- case _ => change = initial
- }
- def bigger(o: Any): Any = {
- o match {
- case i: Int if i < 0 => println("Integer is given")
- case i: Int if i >= 0 => println("Integer is given")
- case d: Double if d < 0.0 => println("Double is given")
- case d: Double if d >= 0.0 => println("Double is given")
- case _ => println("String is given")
- }
- }
- bigger(change)
- //print Integer is given if integer, Double is given if double and String is given if String.
- println(change.getClass)
- }
- }
- /*
- object PatternMatching{
- def main(args :Array[String]){
- val input :Int = args(0).toInt
- input match {
- case i if i%2 == 0 => println("Even number is given")
- case i if i%2 != 0 => println("Odd number is given")
- case i if i <= 0 => println("Negative/Zero is input")
- }
- times match {
- case i if i == 1 => "one"
- case i if i == 2 => "two"
- case _ => "some other number"
- }
- //write your code here
- }
- }
- object flatMapUsage{
- def main(args :Array[String]){
- val input :Int = args(0).toInt
- val sampleList :List[Int] = List(1,2,3,4,5,6,7,8,9,10)
- val numberList = sampleList.slice(0,input)//write your code
- def listGenerator(arg: Int): List[Int] = { sampleList.slice(0,arg) } //write your code
- val resultList = numberList.flatMap(listGenerator)//write your code
- println(resultList)
- println(resultList.length)
- }
- }
- object mapFunction{
- def main(args :Array[String]){
- val input = args(0).toInt
- val sampleList :List[Int] = List(0,1,2,3,4,5,6,7,8,9,10)
- val numberList = sampleList.slice(0,input) //complete the code
- def incrementer(arg: Int): Int = { arg + 5 } //complete the code
- val incrementList =numberList.map(incrementer) //complete the code
- println(incrementList)
- }
- }
- object ListFilter{
- def main(args :Array[String]){
- val input = args(0).toInt
- val sampleList :List[Int] = List(0,1,2,3,4,5,6,7,8,9,10)
- val numberList = sampleList.slice(0,input) //complete the code
- val evenList = numberList.filter(i => i%2 == 0)//complete the code
- println(evenList)
- }
- }
- object JESUS{
- def main(args :Array[String]){
- println("GOD is an AMAZING FATHER")
- def addOne(arg: Int): Int = { arg + 1 }
- object FuncLiterals{
- def main(args : Array[String]){
- val a : Int = args(0).toInt
- val b : Int = args(1).toInt
- var multiply = (x : Int,y :Int) => x*y //write your function literal here
- println(multiply(a,b))
- }
- def addOne(a: Int , b : Int): Int = { a*b }
- }
- }
- }
- object HigherOrderFunction{
- def main(args :Array[String]){
- val input : Int = args(0).toInt
- println(myHigherOrderFunction(addOne, input))
- def addOne(arg: Int): Int = { arg * 9 }
- def myHigherOrderFunction(fn: Int => Int,arg : Int): Int = { fn(arg)} //complete the code
- }
- }
- object ListFunctions{
- def main(args :Array[String]){
- val sampleList :List[Int] = List(1,2,3,4,5,6,7,8,9,10)
- val input :Int = args(0).toInt
- var newlist: List[Int] = sampleList.slice(0,input)
- println(newlist)
- def tos(arg : Int) : String = arg.toString
- var nlist : List[String] = newlist.map(tos)
- println(nlist)
- //write your code here
- }
- }
- */
- /////////////////////////////////
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement