Unable to divide the second element with third element in Scala execrise problem number : 4

val s = p.map( x => (x._1,x._2.map(x => x._2).reduce((a,b)=> a + b),x._2.map(x => x._3).reduce((a,b) => a+b))).toList

This statement will create list :
(TDP,1.409923E7,4.6075334E7)
(APM,372.0,1795776.0)
(jhspt,27122.0,8604620.0)

how can divide the 2 element with 3 element in the list and produce a list : which contain groupby element and division value :
(TDP,1.4E7)

@lokesh_mvs please check if this is helpful

val mylist = List(("TDP",1.409923E7,4.6075334E7),("APM",372.0,1795776.0),("jhspt",27122.0,8604620.0))

import scala.collection.mutable.ListBuffer
var listBuffer = new ListBuffer[String]()
mylist.foreach(x =>   
  listBuffer += x._1+ "," + x._2/x._3
)
val createdList = listBuffer.toList
createdList.foreach(println(_)) 

The results would be as below

TDP,0.306003858810877
APM,2.071527852026088E-4
jhspt,0.003152027631667639

@ashok_singamaneni Thanks for answering , but i am still unable to execute, could u please look over this .
import scala.io.Source
import scala.collection.mutable.ListBuffer
object Second {
def main(args: Array[String]): Unit = {
val path = “/home/lokeshmvs/Desktop/scala/ls2014.tsv"
val results = Source.fromFile(path).getLines()
val resultsp =results.drop(1).map(x=> (x.split(”\t")(6),x.split("\t")(10).toDouble,x.split("\t")(13).toDouble)).toList
val group = resultsp.groupBy( x=> x._1)
val s = group.map( x => (x._1,x._2.map(x => x._2).reduce((a,b)=> a + b),x._2.map(x => x._3).reduce((a,b) => a+b))).toList
val listbuffer = new ListBufferString
s.foreach( x => listbuffer += x._1 + “,” + x._2/x.3)
val z = s.toList
z.foreach(println(
))
}
}

Thanks
This is from Scala exercise problem number : 4

You are trying to change the variable s into list instead of listBuffer.

Please use val z = listBuffer.toList

Try this code:

import scala.io.Source
import scala.collection.mutable.ListBuffer

object Second {
def main(args: Array[String]): Unit = {
val path = “C:\Data\electionresults\ls2014.tsv"
val results = Source.fromFile(path).getLines()
val resultsp =results.drop(1).map(x=> (x.split(”\t")(6),x.split("\t")(10).toDouble,x.split("\t")(13).toDouble)).toList
val group = resultsp.groupBy(x => x._1)
val s = group.map(x => (x._1, x._2.map(x => x._2).reduce((a,b)=> a + b), x._2.map(x => x._3).reduce((a,b) => a+b))).toList
var listBuffer = new ListBufferString
s.foreach( x => listBuffer += x._1 + “,” + x._2/x._3)
//listBuffer.foreach(println)
val z = listBuffer.toList
z.foreach(println)
}
}

Output:

TDP,0.306003858810877
APM,2.071527852026088E-4
jhspt,0.003152027631667639

Thanks… @manikandanra. Can u help me. what is the meaning of the listBuffer what is the purpose of the listBuffer

Because a List is immutable, if you need to create a list that is constantly changing, the preferred approach is to use a ListBuffer while the list is being modified, then convert it to a List when a List is needed.

The ListBuffer Scaladoc states that a ListBuffer is “a Buffer implementation backed by a list. It provides constant time prepend and append. Most other operations are linear.” So, don’t use ListBuffer if you want to access elements arbitrarily, such as accessing items by index (like list(10000)); use ArrayBuffer instead.

Use the Scala ListBuffer class, and convert the ListBuffer to a List when needed. The following example is mentioned Scala CookBook and demonstrates how to create a ListBuffer, and then add and remove elements, and then convert it to a List when finished:

import scala.collection.mutable.ListBuffer

var fruits = new ListBufferString

// add one element at a time to the ListBuffer
fruits += "Apple"
fruits += "Banana"
fruits += “Orange”

// add multiple elements
fruits += (“Strawberry”, “Kiwi”, “Pineapple”)

// remove one element
fruits -= “Apple”

// remove multiple elements
fruits -= (“Banana”, “Orange”)

// remove multiple elements specified by another sequence
fruits --= Seq(“Kiwi”, “Pineapple”)

// convert the ListBuffer to a List when you need to
val fruitsList = fruits.toList

1 Like