Andre Queen Andre Queen - 16 days ago 5
Scala Question

Search through last 2 integers and compare in a map

My system currently has the functionality to search through a mapped list of ints, each with key values, "sk1" - "sk10". I need to modify what I have now so that I can search through the list and compare the last 2 numbers in the list and return which is the highest.

Currently I can search for 1 and return the current, I need to search through all of the values and find the "Highest Increasing"

Below is my data file, "mapdata" I will give an example of what I need. This is stored as a Map[String, List[Int]]. String being the first value and the list of ints afterwards.

SK1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1
SK2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1
SK4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8
SK5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3
SK6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9
SK7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0
SK8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6
SK9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6


Each lists last numbers will be found and compared, so SK1 would be -3, SK2 would be -3, SK4 would be 6. The only value returned should be the key value "SKnumber" and its value and the increase.

So if SK 4 was the highest the result would be "SK4 - 8, INCREASE 6"

Currently, I can search through and find the end int on each tail, below is my code, how would I modify for the changes I have stated above?

val mapdata = readFile("data.txt")

//5 - Show Current Stock Level (W)
def handleFive(): Boolean = {
mnuShowSingleDataStock(currentStockLevel)
true
}

// Returns a single result, not a list
def mnuShowSingleDataStock(stock: (String) => (String,Int)) = {
print("Stock > ")
val data = stock(readLine)
println(s"${data._1}: ${data._2}")
}

//Show last element in the list, most current
def currentStockLevel (stock: String): (String, Int) = {
(stock, mapdata.get (stock).map(findLast(_)).getOrElse(0))
}

Answer

Not sure how your code is related to what you requested, but you can use takeRight to get last N elements from a list - in this case you can take the last two and perform a simple mapping over the map values to compute the "increase", then find the element with the maximal increase using maxBy:

val result: (String, (Int, Int)) = mapdata
  .mapValues(_.takeRight(2)) // take only last two ints from each list
  .mapValues { case List(i1, i2) => (i2, i2 - i1) } // compute "increase"
  .maxBy { case (s, (last, increase)) => increase } // maximum by increase value

// result is (Key, (last value, increase)) 

println(s"${result._1} - ${result._2._1}, INCREASE ${result._2._2}")
// SK4 - 8, INCREASE 6
Comments