2012-12-31 79 views
-5

可能重複:
Zebra puzzle in Scala愛因斯坦的謎題斯卡拉

這個問題一直困擾着我在過去兩天。

斯卡拉解決這個難題的方法是什麼?我試圖在下面的Scala中實現python中顯示的嵌套for循環,但它不起作用。循環已經在第二個約束中給我0個結果。這可能是由於我對Scala陌生而錯過了一些細節。

如果有人有任何想法,請幫助我可憐的靈魂,謝謝。

enter image description here

繼承人我的代碼,

val houses = List(1, 2, 3, 4, 5) 
    val orderings = houses.permutations 
    val List(first, _, middle, _, _) = houses 

    def imright(h1: Int, h2: Int) = h1 - h2 == 1 

    def nextto(h1: Int, h2: Int) = math.abs(h1 - h2) == 1 

    for (List(red, green, ivory, yellow, blue) <- orderings if imright(green, ivory)) 
    for(List(englishman, spaniard, ukrainian, japanese, norwegian) <- orderings; 
     if englishman == red; 
     if norwegian == first) println(orderings.length) 
+0

同樣的問題被我問,它的明顯不同。 – adsisco

+0

你拼寫錯了烏克蘭語。所以我並不感到驚訝^) – idonnie

+3

你的Scala代碼中也有血跡。這讓我很感興趣...... – idonnie

回答

0
val houses = List(1, 2, 3, 4, 5) 
    val orderings = houses.permutations.toList 
    val List(first, _, middle, _, _) = houses 

    def imright(h1: Int, h2: Int) = h1 - h2 == 1 

    def nextto(h1: Int, h2: Int) = math.abs(h1 - h2) == 1 

    for (List(red, green, ivory, yellow, blue) <- orderings if imright(green, ivory)) 
    for(List(englishman, spaniard, ukranian, japanese, norwegian) <- orderings 
     if (englishman == red) 
    ) println(Map(
     "englishman" -> englishman 
     ,"red" -> red 
     ,"norwegian" -> norwegian 
     ,"first" -> first 
    )) 
+0

嗯......不工作? – idonnie

+0

更改'val orderings = houses.permutations.toList' – idonnie