2016-11-25 1 views
2

私は関数型プログラミングを全く新しくしており、スカラで作業しています。私は現在、私の大学の授業のためのプログラムを書いています。マップ内で値が関数を満たすキーを見つける

val mapdata = Map(
    "SK1" -> List(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" -> List(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), 
    "SK3" -> List(8, 7, 1, 8, 0, 5, 8, 3, 5, 9, 7, 5, 4, 7, 9, 8, 1, 4, 6, 5, 6, 6, 3, 6, 8, 8, 7, 4, 0, 6), 
    "SK4" -> List(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, 9), 
    "SK5" -> List(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" -> List(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" -> List(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" -> List(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" -> List(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) 
    ) 

は私が出力するキー(株記号)をしようとしている最大の増加が含まれています

私は、次のマップに入力されています。

私は、次のコードを使用して最大の増加を見つけるために管理している:

def mostRecent(Is:List[Int]): Int = { 
     if (Is.tail == Nil) 
     return Is.head 
     else 
     mostRecent(Is.tail) 
    } 

    def penultimate(x: List[Int]): Int = x(x.length - 2) 

    //this definition allow me to subtract the mostRecentValues and the penultimate values 
    def subtractLast2(pen: Iterable[Int], last: Iterable[Int]): Iterable[Int] = { 
     pen zip last map(x => x._1 - x._2) 
    } 

    //outputs a list with containing the last values 
    val MostRecentPrices = mapdata.values.map(x => mostRecent(x)) 

    //outputs a list with containing the second last values 
    val penultimatePrices = mapdata.values.map(x => penultimate(x)) 

    //determines the maximum increase 
    val maxIncrease = (subtractLast2(MostRecentPrices, penultimatePrices)).max 


    //output the stock that has increased the most in the last day of the period 
    println("MaxIncrease = " + maxIncrease) 

私は私が出力に計算された最大の増加に対応するキーを持っていたが判明するまで、私はちょうど約あったと思いました。

getOrElseを使用することを考えていましたが、私はスカラと関数型プログラミングの初心者ではありません。

これは意味があると思いますが、何かを明確にする必要がある場合はお知らせください。

おかげ

+0

ああ:

val increases = for { (k, v) <- mapdata (a, b) = mostRecent(v) } yield (k, b - a) 

あなたがmaxByメソッドを使用して最大値を見つけることができる最後のステップとしては、私たちは最近、多くのSKnデータを見てきました。それがコースであることを知っておいてよかった。私はコードを提供することができますが、あなたがAPIのドキュメントをちょっと調べた方が良いでしょう(例えば、あなたの 'mostRecent(x)'が 'x.last'、' '最後から二番目' 'x.init.last')。また、あなたの必要とされる増加の定義は本当に最終日の増加だけですか?代わりに、それがいつでも最大の増加であったならば、あなたは「滑り」を見なければならない。もう一つの 'pen zip last map(x => x._1 - x._2)'は 'last-pen'です(' 'Iterable''で囲まれています)。 。 –

答えて

3

あなたはパターンマッチングを使用することにより、2つの最後の要素を計算することができますが:

def mostRecent(is: List[Int]): (Int, Int) = 
    is match { 
    case a +: b +: Nil => (a, b) 
    case head +: tail => mostRecent(tail) 
    case Nil => throw new Exception("Can't calculate for an empty list") 
    } 

まずそれが尾をリストの末尾であれば二つの第一の要素を抽出しa +: b +: Nilに一致します。このケースにマッチできない場合は、head +:tailとしてリストを分解しようとします。そのため、tailを再帰的に呼び出すことができます。最初に空のリストで呼び出された場合、例外がスローされます。あなたは最近の2つの要素を計算し、それらを引くことができますmapdata内のすべてのエントリのためのその後

、:

val max = increases.maxBy(_._2) 
max: (String, Int) = ("SK4", 7) 
関連する問題