0

My current system can find the last value of a list of integers, shown in the first file below. Then it displays this as the "Current" value. I need to be able to find all of the "Current" values when I enter a search and return all of the results higher than the one I searched for.The list is saved as a Map(String, List[Int]).

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

This is what I am trying to get; using the data file from above, If I searched for "SK5" the system would return;

Figures higher than SK5 - 3

SK4 - 8

SK6 - 9

SK8 - 6

SK9 - 6

Here is my current code to find the last number in the list

  //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 higher than stocks
  def higherThan(stock: String): List[(String, List[Int])] = {
    mapdata.toList.sortWith(_._2.last > _._2.last).takeWhile(row => row._2.last > mapdata.get(stock).map(_.last).getOrElse(0))
  }
2
  • What do mean by figures higher than SK5 and what is of - 3 in SK5 - 3 Nov 20, 2016 at 13:22
  • @pamu As I said in my question, the value "- 3" is the "Current" value of SK5, this is the last element in the list. So by figures higher than, I am referring to all the other "Current" values, so the last elements of the list. Nov 20, 2016 at 13:23

1 Answer 1

1

Sort based on the last value and then take while current stock value is greater than the searched stock value

def higherThan(stock: String): List[(String, List[Int])] = {
  mapdata.toList.sortWith(_._2.last > _._2.last).takeWhile(row => row._2.last > mapdata.get(stock).map(_.last).getOrElse(0))
}
3
  • Can you take a look at my question, I have made the changes but I am getting another error using your answer. I have currently been using "String, List(Int)", you however use list, string, list, int Nov 20, 2016 at 15:11
  • @AndreQueen corrected the answer. Please check...Parenthesis missing in return type of the function List[(String, List[Int])] Nov 20, 2016 at 15:13
  • Thats great, that works fine now, im having trouble selecting the number to search for, in my answer i have added the error. The search function itself seems to be getting errors with the new function @pamu Nov 20, 2016 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.