About
object Limfilt {
def limfiltA: List[A] = (limit, lst) match { case (_, Nil) => Nil case (0, _) => lst case (_, head :: tail) => if (f(head)) head :: limfilt(f, limit, tail) else limfilt(f, limit - 1, tail) }
def main(args: Array[String]): Unit = { println("Test cases:") println(limfilt((x: Int) => x > 0, 10, (-3 to 3).toList)) // should return List(1, 2, 3) println(limfilt((x: Int) => x > 0, 0, (-5 to 5).toList)) // should return List.range(-5, 6) println(limfilt((x: Int) => x > 0, 3, List(-2, 2, -3, 3, -4, 4, -5, 5))) // should return List(2, 3, 4, -5, 5) println(limfilt((x: Int) => x == 0, 3, List.empty)) // should return List.empty println(limfilt((x: Int) => x % 2 == 0, 2, (1 to 9).toList)) // should return List(2, 4, 5, 6, 7, 8, 9) } }
object SumLessGen {
def sumLessGen(a: Int, b: Int, fn: Int => Int, next: Int => Int): Int = a match { case x if x<b => 0 case x => fn(x) + sumLessGen(next(x), b, fn, next) }
def main(args: Array[String]): Unit = { // Define the square function def square(x: Int): Int = x * x
// Define the cube function
def cube(x: Int): Int = x * x * x
// Define the decrement function
def decrement(x: Int): Int = x - 1
// Test sum-less-gen with square function and decrement
val result1 = sumLessGen(3, 1, square, decrement)
println("Result with square function: " + result1) // Output: 14
// Test sum-less-gen with cube function and decrement
val result2 = sumLessGen(3, 1, cube, decrement)
println("Result with cube function: " + result2) // Output: 36
} }
object ScrabbleScore {
def score(word: List[Char]): Int = word match { case Nil => 0 case head :: tail => letterScore(head.toUpper) + score(tail) }
def letterScore(letter: Char): Int = letter match { case 'A' | 'E' | 'I' | 'O' | 'U' | 'L' | 'N' | 'R' | 'S' | 'T' => 1 case 'D' | 'G' => 2 case 'B' | 'C' | 'M' | 'P' => 3 case 'F' | 'H' | 'V' | 'W' | 'Y' => 4 case 'K' => 5 case 'J' | 'X' => 8 case 'Q' | 'Z' => 10 case _ => 0 }
def main(args: Array[String]): Unit = { val word = "cabbage".toList println(s"The Scrabble score for '$word' is: ${score(word)}") } }
object OtherMap {
def otherMapA: List[A] = lst match { case Nil => Nil case x :: Nil => List(f1(x)) case x :: y :: tail => f1(x) :: f2(y) :: otherMap(f1, f2, tail) }
def main(args: Array[String]): Unit = { println(otherMap((x: Int) => x + 10, (x: Int) => x + 100, List(0, 1, 2, 3, 4))) // Output: List(10, 101, 12, 103, 14)
println(otherMap((x: Int) => x + 5, (x: Int) => x - 10, List(100, 200, 300, 400, 500)))
// Output: List(105, 190, 305, 390, 505)
} }
object higherorder{ def main(args:Array[String]):Unit={ println(filters(s=>s>0,List(1,-1,2,-2))) println(maps(_ + 2 ,List(1,2,3))) println(zipWith(_+_ ,List(1,2,3),List(3,4,5)))
}
def filters(f: Int => Boolean ,l:List[Int]):List[Int]={
l match{
case Nil => Nil
case head::tail =>
if (f (head)) head::filters(f,tail)
else filters(f,tail)
}
}
def maps(f:Int =>Int,l:List[Int]):List[Int]={
l match{
case Nil => Nil
case head::tail => f(head)::maps(f,tail)
}
}
def zipWith(f:((Int,Int) => Int),l:List[Int],m :List[Int]):List[Int]={
(l,m) match{
case (Nil,_ ) => Nil
case (_,Nil) => Nil
case (x::xs,y::ys) => f(x,y):: zipWith(f,xs,ys)
}
}
}
def countdigitsum(x: String): Int = { var parts = x.split(" ") var sum = 0 for (part <- parts) { var l = part.length var temp = "" for (i <- 0 until l) { if (Character.isDigit(part(i))) { temp += part(i) } } if (temp.nonEmpty) { // Check if temp is not empty sum += Integer.parseInt(temp) } } sum }
object sor { def main(args: Array[String]): Unit = { println(twoSame(List(1, 3, 1, 2))) }
def mycontains(l: List[Int], x: Int): Boolean = { l match { case Nil => false case head :: tail => if (head == x) true else mycontains(tail, x) } }
def twoSame(l: List[Int]): Boolean = { l match { case Nil => false case x :: xs => mycontains(xs, x) || twoSame(xs) } } }
}object r1{ def main(args:Array[String])={ println(othermap(_ + 2,_ + 10,List(1,2,3,4))) println(limfilt(_ > 0,List(1,2,3,4),2)) } def limfilt(f:Int=>Boolean,l:List[Int],n:Int):List[Int]={ (l,n) match{ case (Nil,_) => Nil case (_,0) => Nil case (head::tail,_) => if (f(head)) head:: limfilt(f,tail,n-1) else limfilt(f,tail,n-1) } } def othermap(f:Int=>Int,g:Int=>Int,l:List[Int]):List[Int]={ l match{ case Nil => Nil case head::Nil => f(head)::Nil case head::head2::tail => f(head)::g(head2) :: othermap(g,f,tail) } } }
def countdigitsum(x: String): Int = { var parts = x.split(" ") var sum = 0 for (part <- parts) { var l = part.length var temp = "" for (i <- 0 until l) { if (Character.isDigit(part(i))) { temp += part(i) } } if (temp.nonEmpty) { // Check if temp is not empty sum += Integer.parseInt(temp) } } sum }
object scala1 { def main(args: Array[String]): Unit = { // println(sum(2, 3)) // println("Enter an integer:") // var x = scala.io.StdIn.readInt() // println(absolute(x)) // println("enter a String: ") // var x = scala.io.StdIn.readLine() // println(removeChar(x,3)) // var x = scala.io.StdIn.readInt() // print(palindrome(x)) // var x = scala.io.StdIn.readInt() // print(isArmstrong(x))
}
// def sum(x: Int, y: Int): Int = { // if (x == y) { // (x + y) * 3 // } else { // x + y // } // }
// def absolute(x: Int): Int = { // Math.abs(x - 51) // } // def removeChar(s:String,x:Int):String={ // var ans ="" // for (i<- 0 until s.length){ // if (i!=x){ // ans += s(i) // } // } // ans
// }
// def palindrome(x:Int):Boolean={
// var temp = x
// var rev = 0
// while (temp!=0){
// rev = rev*10 +(temp%10)
// temp=temp/10
// }
// if (rev == x)
// {
// true
// }else{
// false
// }
// }
// def isArmstrong(x:Int):Boolean={
// val l = x.toString.length
// var sum = 0
// var temp = x
// while(temp!=0){
// sum += Math.pow((temp%10),l).toInt
// temp = temp/10
// }
// sum==x
// }
}
object object2 { def lengths(l: List[Int]): Int = { l match { case Nil => 0 case _ :: tail => 1 + lengths(tail) } } def firstElem(l:List[Int]):Int={ l match{ case Nil => throw new Exception("this is j") case (head::_) => head } } def exceptfist(l:List[Int]):List[Int]={ l match{ case Nil => throw new Exception("empty list") case head::Nil => Nil case head::tail => tail } } def exceptlast(l:List[Int]):List[Int]={ l match{ case Nil => Nil case head::Nil => Nil case head::tail => head:: exceptlast(tail) } } def reversey(l:List[Int]):List[Int]={ l match{ case Nil => Nil case head::tail => reversey(tail) ++ List(head) }
} def elem (l:List[Int],x:Int):Boolean = { l match{ case Nil => false case head::tail => if (x== head) true else elem(tail,x) } } def startEnd(l:List[Int]):(Int,Int)={ l match{ case head::Nil => head case head::tail => (head,startEnd (tail)) } }
def main(args: Array[String]): Unit = {
val myList = List(1, 2, 3, 4, 5)
println(lengths(myList)) // Output: 5
println(firstElem(myList))
println(exceptfist(myList))
println(exceptlast(myList))
println(reversey(myList))
println(elem(myList,12))
println(startEnd(myList))
}
}
object object2 { def main(args: Array[String]) = { // Create an array of strings with length 3 val arraystring = new ArrayString
// Assign values to elements of the array
arraystring(0) = "hello"
arraystring(1) = "word"
arraystring(2) ="none"
// Print the elements of the array
for (i <- 0 until arraystring.length) {
println(arraystring(i))
}
// Filter the array to select elements with length equal to 4
var filteredarray = arraystring.filter(s => s.length == 4)
// Print the filtered array
filteredarray.foreach(print)
val L1 = List(2,3)
val newlist = List(1) ++ L1
newlist.foreach(println)
}
}
object s4 { def main (args:Array[String])={ var input = "hello" println(reversey(input.toList).mkString) } def reversey(l:List[Char]):List[Char]={ l match{ case Nil => Nil case head::tail => reversey(tail) ++ List(head) } } }