 # Exercise 10 - Develop simple scala program using scala interpreter

Problem Statement

• Develop simple function (you can define your problem statement as well), otherwise min function
• Take list as input parameter
• Loop through the list and return the minimum value
• Invoke the function

Please provide the code of the function, input, function invocation and sample output.

2 Likes

use case sorting a array of integers:

``````scala> val input:Array[Int]=Array(34,62,356,13412,8472,134,73,35,4,62)
input: Array[Int] = Array(34, 62, 356, 13412, 8472, 134, 73, 35, 4, 62)
scala> def sorting(input: Array[Int]): Array[Int] = {
|     val limit = input.size - 1
|     for (a <- 1 to limit) {
|       for (b <- limit to a by -1) {
|         if (input(b) < input(b - 1)) {
|           val x = input(b)
|           input(b) = input(b - 1)
|           input(b - 1) = x
|         }
|       }
|     }
|     input
|   }
sorting: (input: Array[Int])Array[Int]

scala> sorting(input)
res1: Array[Int] = Array(4, 34, 35, 62, 62, 73, 134, 356, 8472, 13412)``````

#The code of the function,

``````scala> def minOfList(list: List[Int]):Int = {
|       list.reduceLeft(_ min _)
| }
minOfList: (list: List[Int])Int
``````

#Input function invocation

`println(minOfList(List(9,2,3,4,5)))`

#And sample output.
scala> println(minOfList(List(9,2,3,4,5)))
2

3 Likes

scala> val input = Array(2,5,5,7,8,5,7,3,6,3,2,1)
input: Array[Int] = Array(2, 5, 5, 7, 8, 5, 7, 3, 6, 3, 2, 1)

scala> def min(input:Array[Int]):Int={
| var minEle=100
| for(ele<-input)
| if(ele<minEle)
| minEle=ele
| minEle
| }
min: (input: Array[Int])Int

scala> print(min(input))
1
scala>

val i:List[Int]= List(10,20,30, 40);

def min(p:List[Int]):Int=
{
var s =p(0)
for(ele<-p)
if(ele<s)
s=ele
s
} | | | | | | |
min: (p: List[Int])Int

scala> print(min(i))
10
scala>

scala> def min (p: List[Int]):Int =
| {
| var min= p(0)
| for(ele<-p)
| if (ele<min)
| min=ele
| min
| }
min: (p: List[Int])Int
println(min(List(1,2,3,4)))
1

## Min of List using function:

scala> val i: List[Int] = List(33,11,66,22,7,88,77);
i: List[Int] = List(33, 11, 66, 22, 7, 88, 77)

scala> def min_list(input: List[Int]):Int= {
| var t = input(0)
| for(ele <- input)
| if(ele < t)
| t = ele
| t
| }
min_list: (input: List[Int])Int

scala> println(min_list(input));
7

scala> val i: List[Int] = List(1,2,3,4,8);
i: List[Int] = List(1, 2, 3, 4, 8)

scala> def min(e: List[Int]) : Int = {
| var min =e(0)
| for (ele <- e )
| if (ele < min) {
| min = ele
| }
| min
| }
min: (e: List[Int])Int

scala> println(min(i))
1

scala> val i:List[Int]=List(1,2,3,4,5);
i: List[Int] = List(1, 2, 3, 4, 5)

scala> def getMin(input:List[Int]):Int={
| var t=input(0)
| for(ele<-input)
| if(ele < t)
| t=ele
| t
| }
scala> println(getMin(i));
1

Code for finding Minimum

def minNum(in:List[Int]): Int = {
| var minN, x, y =0;
| val len = in.length-1
| for(x <- 1 to len) {
| for(y <- 1 to len) {
| if (in(y) < in(x))
| minN = in(y)
| }
| }
| minN
| }

Input List:
scala> val list: List[Int] = List(1, 5, 3, 9, 0, 6, -6)
list: List[Int] = List(1, 5, 3, 9, 0, 6, -6)

Function invocation and output

scala> minNum(list)
res16: Int = -6

@Farhan_Misarwala Nicely done in a Scala way 1 Like