2016-10-31 9 views
1

で数学のロジックでリスト上の特定の要素を削除します。私はこのリストを持っている場合はScalaの

val aList = List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6) 

にはどうすればListの最初の頭の上に非重複の番号を排除することによって、リストを更新しますか?その結果は次のようになります。私たちは、リストの先頭に3を持っていないので、

val aRes = List(1,1,1), List(3,3), List(7,7,7)

List(2,3,3,2,6)も削除する必要があります。

val aRes = aList(1) map {case List(i) => List(aList.groupBy(_(1))}

しかし、それは、このロジックは有効ではありませんようだ:結果のための私の予想でした。その横に

、私はまた別のリストのメンバーにそれらの結果の値を変換する必要があります。

val aScore = List(
     /*score for 1*/ List(0, 0, 1500, 2500, 5000), 
     /*score for 2*/ List(0, 0, 500, 1000, 2000), 
     /*score for 3*/ List(0, 50, 100, 200, 500), 
     /*score for 4*/ List(0, 10, 50, 100, 150), 
     /*score for 5*/ List(0, 10, 50, 100, 150), 
     /*score for 6*/ List(0, 10, 50, 100, 150), 
     /*score for 7*/ List(0, 10, 50, 100, 150) 
    ) 

val score = ???

のでaList結果上記から、スコアは1*3=>1500から結果として1500+50+50 = 16003*2=>50とでなければなりません7*3=>50

+1

コードを読んでもう助けが必要ですか? – shaktimaan

+0

はい、私は 'x.groupBy'と' x.splitAt'で分割しようとしましたが、このロジックではうまくいかないようです。 –

+1

おそらく、あなたの教授にいくつかのヒントを聞いてもらえますか? – Dima

答えて

4

を与える:

def f(xs: List[Int]) = xs match { 
    case x0 :: x1 :: _ if x0 == x1 => Some(xs.takeWhile(_ == x0)) 
    case _ => None 
} 

その後flatMapあなたのリストをそのオプションビットを取り除くために:

aList.flatMap(f) 
第二部のために

def getScore(xs: List[Int]) = aScore(xs.head - 1)(xs.size - 1) 

はそうちょうど要素をマッピングして合計します。合計:

aList.flatMap(f).map(getScore).sum 
// result = 1600 
+0

私は、結果を「None」にして、すべてをフラットにすることができたことに気付きました。あなたのロジックは素晴らしいです。どうもありがとうございました! –

+1

あなたは大歓迎です。あなたが何かを学んだことをうれしい。 –

+0

2つの 'match'式を1つに減らすことができます: def f(xs:List [Int])= xs match { case x0 :: x1 :: _ if x0 == x1 => Some(xs.takeWhile() _ == x0)) case _ =>なし } – n4to4

1

私の最初のスタブは:

scala> val ls = List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6)) 
ls: List[List[Int]] = List(List(1, 1, 1, 3, 4), List(3, 3, 5, 6, 7), List(7, 7, 7, 6, 7), List(2, 3, 3, 2, 6)) 

scala> ls map { 
    _ groupBy identity filter { case (i, is) => is.length > 1 } flatMap { _._2 } 
} 
res2: List[List[Int]] = List(List(1, 1, 1), List(3, 3), List(7, 7, 7, 7), List(2, 2, 3, 3)) 

あなたが見ることができるように、それはあなたが望むものではありません。私は次の釘それを考える:

scala> ls map { l => 
     val (h,t) = (l.head, l.tail) 
     h :: t.takeWhile(_ == h) 
     } filter { _.length > 1 } 
res7: List[List[Int]] = List(List(1, 1, 1), List(3, 3), List(7, 7, 7)) 

しかし、通知を、List.emptyは、外側のリストの要素である場合には仕事に行くのではありません。

3
object parseData { 
    val inputList = List(List(1,1,1,3,4),List(3,3,5,6,7),List(7,7,7,6,7),List(2,3,3,2,6)) 
    val aScore = List(
    /*score for 1*/ List(0, 0, 1500, 2500, 5000), 
    /*score for 2*/ List(0, 0, 500, 1000, 2000), 
    /*score for 3*/ List(0, 50, 100, 200, 500), 
    /*score for 4*/ List(0, 10, 50, 100, 150), 
    /*score for 5*/ List(0, 10, 50, 100, 150), 
    /*score for 6*/ List(0, 10, 50, 100, 150), 
    /*score for 7*/ List(0, 10, 50, 100, 150) 
) 

    def isDuplicated(aList: List[Int]): Boolean = aList.head == aList.tail.head 

    def getRidOfNonDuplicates(aList: List[Int]): List[Int] = { 
    val opList = ListBuffer(aList.head) 
    def loop(aList: List[Int], opList: ListBuffer[Int]): Unit = { 
     if (aList.tail == Nil) return 
     if (aList.head == aList.tail.head) opList += aList.tail.head 
     loop(aList.tail, opList) 
    } 
    loop(aList, opList) 
    opList.toList 
    } 

    def printaScoreValue(aList: List[Int]): Unit = println(aScore(aList.head - 1)(aList.length - 1)) 

    val outputList = inputList.filter(isDuplicated(_)) 
    val opList = ListBuffer.empty[List[Int]] 
    for (aList <- outputList) 
    opList += getRidOfNonDuplicates(aList) 
    opList.foreach(printaScoreValue(_)) 
} 

あなたは重複がある場合は、何かを返し、何もない場合は、そうOptionを返す関数を作りたい

1500 
50 
50 
+0

論理に関する完全かつ正解です。しかし、他の答えも正しいので、重複していない値を新しいリストに取り除く方が速いのかどうかを知る必要があります。 –

+0

はい!また、アルゴリズムが機能しているという証明と、パフォーマンスを示す科学的なベンチマークを記入してください! OPはこれのようなたわごとの彼の貴重な時間を無駄にするために忙しい方法です。締め切りが近づいているので、あなたは早く仕事に就くことをお勧めします! 私はあなたの答えが-1であると思うので、このような質問はSOの内容の質を大きく低下させ、そのような質問はそのような行動を促します。 – Dima

+2

@Dima回答者に不利益を与えたり落胆させたりするよりも、質問にフラグを立てるのはなぜですか?私はあなたのポイントを取得し、私は+1が取り去られたことは気にしない。回答を失った回答者もまた別の問題を引き起こすでしょう、あなたは思いませんか? – shaktimaan

関連する問題