2016-11-12 5 views
0

Jede Zeile der Datei wird verwendet, um einen Karteneintrag mit dem Börsensymbol als Schlüssel und einer Liste von ganzen Zahlen als Wert zu erstellen. Der Typ der Struktur ist Map [String, List [Int]]. Derzeit kann mein System nach einem einzelnen Eintrag in der Liste suchen und verschiedene Ergebnisse anzeigen. Wie würde ich 2 in der Liste vergleichen? Wie würde ich 2 Artikel in dieser Liste vergleichen? Bitte finden Sie meinen Code unten, den ich derzeit habe, ich schätze, dass ich Code für Sie zur Verfügung stellen sollte, aber ich bin nicht sicher, wie man diesen beginnt. Jede und jede Hilfe wird geschätzt.Vergleichen Sie 2 Einträge in einer Liste

/** 
    * Created by Andre on 10/11/2016. 
    */ 
import scala.io.Source 
import scala.io.StdIn.readInt 
import scala.io.StdIn.readLine 
import scala.collection.immutable.ListMap 
import scala.util.Try 

object StockMarket extends App { 

    // ******************************************************************************************************************* 
    // application logic 

    // read data from file 
    val mapdata = readFile("data.txt") 
    // print data to check it's been read in correctly 
    //println(mapdata) 

    // define menu options as a Map of actions 
    // for each menu item: 
    // key is an Int, the value that will be read from the input 
    // value is a function() => Boolean, i.e. no params and returns Boolean 
    val actionMap = Map[Int,() => Boolean](1 -> handleOne, 2 -> handleTwo, 3 -> handleThree, 4 -> handleFour, 5 -> handleFive, 6 -> handleSix, 8-> handleEight) 

    // loop to read input and invoke menu option 
    // uses function readOption to show menu and read input 
    // uses function menu to invoke menu action 
    // will terminate if menu returns false 
    var opt = 0 
    do { 
    opt = readOption 
    } while (menu(opt)) 



    // ******************************************************************************************************************* 
    // FUNCTIONS FOR MENU 

    // shows menu and reads input 
    def readOption: Int = { 
    println(
     """|Please select one of the following: 
     | 1 - show All stock levels 
     | 2 - Show Selected Stock Level 
     | 3 - Show Highest Stock Level 
     | 4 - Show Lowest Stock Level 
     | 5 - Show Current Stock Level 
     | 6 - Show Average Stock Level 
     | 8 - quit""".stripMargin) 
    readInt() 
    } 

    // invokes selected menu option 
    // finds corresponding function to invoke in action map using get 
    // pattern matching used as get returns an Option 
    def menu(option: Int): Boolean = { 
    actionMap.get(option) match { 
     case Some(f) => f() 
     case None => 
     println("Sorry, that command is not recognized") 
     true 
    } 
    } 

    // handlers for menu options 
    def handleOne(): Boolean = { 
    mnuShowPoints(currentPoints) 
    true 
    } 

    def handleTwo(): Boolean = { 
    mnuShowPointsForStock(allStockLevel) 
    true 
    } 

    def handleThree(): Boolean = { 
    mnuShowSingleDataStock(highestStockLevel) 
    true 
    } 

    def handleFour(): Boolean = { 
    mnuShowSingleDataStock(lowestStockLevel) 
    true 
    } 

    def handleFive(): Boolean = { 
    mnuShowSingleDataStock(currentStockLevel) 
    true 
    } 

    def handleSix(): Boolean = { 
    mnuShowSingleDataStock(averageStockLevel) 
    true 
    } 

    def handleEight(): Boolean = { 
    println("selected quit") // returns false so loop terminates 
    false 
    } 





    // ******************************************************************************************************************* 
    // UTILITY FUNCTIONS 
    //GETS THE DATA FROM THE DATA.TXT 
    def readFile(filename: String): Map[String, List[Int]] = { 
    processInput(Source.fromFile(filename).getLines) 
    } 
    def processInput(lines: Iterator[String]): Map[String, List[Int]] = { 
    Try { 
     lines.foldLeft(Map[String, List[Int]]()) { (acc, line) => 

     val splitline = line.split(",").map(_.trim).toList 
     acc.updated(splitline.head, splitline.tail.map(_.toInt)) 
     } 
    }.getOrElse { 
     println("Sorry, an exception happened.") 
     Map() 
    } 
    } 





    // ******************************************************************************************************************* 
    // FUNCTIONS THAT INVOKE ACTION AND INTERACT WITH USER 
    // each of these functions accepts user input if required for an operation, 
    // invokes the relevant operation function and displays the results 

    def mnuShowPoints(f:() => Map[String,List[Int]]) = { 
    f() foreach {case (x,y) => println(s"$x: $y")} 
    } 
//Returns a list value 
    def mnuShowPointsForStock(f: (String) => (String,List[Int])) = { 
    print("Stock > ") 
    val data = f(readLine) 
    println(s"${data._1}: ${data._2}") 
    } 

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

    //functionality to find the last tail element, the "Current" stock price 
    def findLast(list:List[Int]) = list.last 


    //Function to find the average 
    def average(list:List[Int]): Double = list.sum.toDouble/list.size 



    // ******************************************************************************************************************* 
    // OPERATION FUNCTIONS 
    // each of these performs the required operation on the data and returns 
    // the results to be displayed - does not interact with user 

    def currentPoints():Map[String,List[Int]] = { 
    // sort map by value in descending order - 
    ListMap(mapdata.toSeq.sortWith(_._1 < _._1):_*) 
    } 


    def allStockLevel(team: String): (String, List[Int]) = 
    (team, mapdata.get(team).getOrElse(List.empty)) 


    //Shows Highest Stock 
    def highestStockLevel(stock: String): (String, Int) = 
    (stock, mapdata.get(stock).map(_.max).getOrElse(0)) 

    //Shows the Lowest Stock 
    def lowestStockLevel(stock: String): (String, Int) = 
    (stock, mapdata.get(stock).map(_.min).getOrElse(0)) 


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

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



} 

Antwort

0

Herausgegeben

Wie im Kommentar angefordert.

„zum Beispiel‚SK1‘und dann einen anderen Wert,‚SK2‘und das wird die 2 Listen vergleichen und zurück, die größer ist“

def largestList(a: String, b: String): Option[List[Int]] = { 
    (map.get(a), map.get(b)) match { 
    case (Some(alist), Some(blist)) => Some(List(alist, blist).maxBy(_.size)) 
    case _ => None 
    } 
} 

val map = Map("stock1" -> List(1, 2), "stock2" -> List(3, 4, 5)) 

scala> largestList("stock1", "stock2") 
res24: Option[List[Int]] = Some(List(3, 4, 5)) 

Alte Antwort

Ich gehe davon aus, dass angesichts eine Karte Map[String, List[Int]] möchten Sie die Karte mit Werten im Wert (Liste [Int])

suchen, zum Beispiel, wenn Sie Schlüssel, Wertpaar mit Wert mit zwei Nummer 1, 2 dann tun möchten

map.filter { case (k, v) => v.exists(Set(1, 2)) } 

Scala REPL

scala> val map = Map("stock1" -> List(1, 2), "stock2" -> List(3, 4, 5)) 
map: Map[String, List[Int]] = Map("stock1" -> List(1, 2), "stock2" -> List(3, 4, 5)) 

scala> map.filter { case (k, v) => v.exists(Set(3, 4))} 
res20: Map[String, List[Int]] = Map("stock2" -> List(3, 4, 5)) 
+0

, die eine hilfreiche Antwort ist, aber ich brauche den Benutzer zur Eingabe der Lage sein, ein Schlüsselwert, zum Beispiel „SK1“ und dann ein anderer Wert, „SK2“ und dies wird die 2 Listen vergleichen und zurück, die größer ist –

+0

@AndreQueen die Antwort bearbeitet. Bitte prüfe – pamu

Verwandte Themen