PROGRM-1

 Write a Scala program to create a new string which is n copies of a given string.

object demo {
def test(str1: String, n: Int): String = {
str1 * n;
}

def main(args: Array[String]): Unit = {
println("Result: " + test("Scala", 2));
println("Result: " + test("Python",1));
println("Result: " + test("JS",6));
}
}

Result: ScalaScala

Result: Python

Result: JSJSJSJSJSJS

Write a Scala program to check whether a given string starts with 'Sc' or not.

object demo{
def test(str1:String): Unit =
{
val str2 = str1.take(2)
println(str2)
val res= str2=="Sc"
println(res)
}
def main(args: Array[String])
{
test("Scala")
}
}

 Write a Scala program print odd and even number of an Array

object prg1 {

 def main(args: Array[String]): Unit = {     

   var intList = List(1,2,3,4,5)

   val res1 = intList.filter(x => x % 2 == 0)

   val res2 = intList.filter(x => x % 2 != 0)

   println(res1) 

   println(res2)

 }

}

Find the missing number in a given integer array

object prg1 {

 def main(args: Array[String]): Unit = {     

   var intList = List(1, 2, 3, 4, 5, 7, 8, 9, 10)

   val n = intList.length

   val m = n + 1

   val total = m * (m + 1) / 2

   val missing_number = total - intList.sum

   println(missing_number)

    }

}

object demo {
def test(str1: String): Boolean =
{
str1.startsWith("Sc")
}
def main(args: Array[String]): Unit = {
println("Result: " + test("Scala"));
println("Result: " + test("abcd"));
println("Result: " + test("sc"));
println("Result: " + test("a"));
}
}

SumOfSquaresOfOdd – Functional Way (Scala)

import scala.collection.immutable.List
object demo
{
def main(args:Array[String]):Unit =
{
var intList = List(1,2,3,4,5)
def sum = intList.filter(x => x % 2 ==0).map(x => x * x).reduce((x,y) => x+y)
println(sum)
}
}


object demo {
def main(args: Array[String]) {
var a = 0;
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for loop execution with a yield
var retVal = for{ a <- numList
if a != 3; if a < 8
}yield a
// Now print returned values using another loop.
for( a <- retVal){
println( "Value of a: " + a );
}
}
}


object demo {
def main(args: Array[String]): Unit = {
val lst=List(1,2,3,4,5,6,7,8,9,10)
for ( i <- lst )
if (i > 5)
println(i)
}
}


class Rectangle( l: Int,  b: Int) {
val length: Int = l
val breadth: Int = b
def getArea: Int = l * b
override def toString = s"This is rectangle with length as $length and breadth as $breadth"
}
object demo {
def main(args: Array[String]) {
val rect = new Rectangle(4, 5)
println(rect.toString)
println(rect.getArea)
}
}

To check wether a given character persent in a string between 2 to 4 times.

object prg1 {

  def test(str1: String): Boolean = {

      val count_char = str1.count{ n => n == 'z' }

      count_char >= 2 && count_char <= 4

    }

   def main(args: Array[String]): Unit = {

      println("Result: " + test("frizz"));

      println("Result: " + test("zane"));

      println("Result: " + test("Zazz"));

      println("Result: " + test("false"));

    }

}


Capitalize first and last letter of each word in a line

object prg1 {

     def main(args: Array[String]): Unit = {

     val str="is There any other WAY".capitalize

     println(str)

     val str1= "is There any other WAY".split(' ').map(_.capitalize).mkString(" ")

     println(str1)

     val str2 = "is There any other WAY".split(" ").map(_.capitalize).mkString(" ")

     println(str2)

     val str3 = raw"\b((?<!\b')\w+)".r.replaceAllIn("this is a test, y'all! 'test/test'.", _.group(1).capitalize)

     println(str3)

    }

}

How to generate Random Number

object Demo {
def main (args: Array [String]) {
var i=0
while (i<10) {
var str = scala.util.Random.nextInt(10).toString
println(str)
i = i + 1
}
}
}

outPut

0

4

1

5

2

To Check whether two given positive integers have the same last digit
object scala_basic {
def test(x: Int, y: Int): Boolean = {
Math.abs(x % 10) == Math.abs(y % 10);
}
def main(args: Array[String]): Unit = {
println("Result: " + test(123, 456));
println("Result: " + test(12, 512));
println("Result: " + test(7, 87));
println("Result: " + test(12, 45));
}
}

No comments:

Post a Comment