CodeWars のチャレンジをいくつかやり直しています。
私はこれに問題があります:
「一連の単語が与えられた場合、最高得点の単語を見つける必要があります。
a = 1、b = 2、c = 3 など、単語の各文字は、アルファベットの位置に従ってポイントを獲得します。
最高得点の単語を文字列として返す必要があります。
2 つの単語のスコアが同じ場合、元の文字列で最初に出現する単語を返します。
すべての文字が小文字になり、すべての入力が有効になります。」
私はすでにSOのソリューションを調べており、1人のアイデアを使用しています(少し変更しましたが)。
それでもうまくいきません。
何か案は?
object Scoring {
def high(s: String): String = {
var max = 0
var whichWord = 0
var x = 0
var y = new Array[Int](100)
for(word <- s.split(" ")){
for(letter <- word) letter match{
case 'a' => y(x)+=1
case 'b' => y(x)+=2
case 'c' => y(x)+=3
case 'd' => y(x)+=4
case 'e' => y(x)+=5
case 'f' => y(x)+=6
case 'g' => y(x)+=7
case 'h' => y(x)+=8
case 'i' => y(x)+=9
case 'j' => y(x)+=10
case 'k' => y(x)+=11
case 'l' => y(x)+=12
case 'm' => y(x)+=13
case 'n' => y(x)+=14
case 'o' => y(x)+=15
case 'p' => y(x)+=16
case 'q' => y(x)+=17
case 'r' => y(x)+=18
case 's' => y(x)+=19
case 't' => y(x)+=20
case 'u' => y(x)+=21
case 'v' => y(x)+=22
case 'w' => y(x)+=23
case 'x' => y(x)+=24
case 'y' => y(x)+=25
case 'z' => y(x)+=26
}
x +=1
}
for(x <- 0 until y.length){
if(y(x) > max)
{
max = y(x)
whichWord = x
}
}
s.substring(whichWord-1, whichWord)
}
}
テストは次のとおりです。
試験結果:
RandomTestSpec
high("ykvhorsqve kfkq jhjibercdptf efevxax ccr vnsmumqby jwhxvamegupfcj lierziuopbcsutm") should return "lierziuopbcsutm"
Test Failed
"[s]" was not equal to "[lierziuopbcsutm]"
Stack Trace
Completed in 34ms
high("skwwwm") should return "skwwwm"
Test Failed
String index out of range: -1
Stack Trace
Completed in 1ms
high("a") should return "a"
Test Failed
String index out of range: -1
Stack Trace
Completed in 1ms
high("gykoialocufuc wcdwuxksqk bvapztcnqwx") should return "bvapztcnqwx"
Test Failed
"[y]" was not equal to "[bvapztcnqwx]"
Stack Trace
Completed in 1ms
high("gdhfoonwtih xbvsiaqhsesl obrndpz nxt inkklyo lf oyoadxqhuys lbqr oxbqq bopalqknjxvpg") should return "oyoadxqhuys"
Test Failed
"o[]" was not equal to "o[yoadxqhuys]"
Stack Trace
Completed in 1ms
high("bbzlmqhsypato pfufsi ryu oboklfa iigha h m") should return "bbzlmqhsypato"
Test Failed
String index out of range: -1
Stack Trace
Completed in 1ms
high("dbtfwvhk kadarmvldjhkx dgxffryldcxodtn hoffibiayxriqe gtqzeuywpgc nqlgvudy") should return "dgxffryldcxodtn"
Test Failed
"[b]" was not equal to "[dgxffryldcxodtn]"
Stack Trace
Completed in 1ms
high("vhyxdefryeznlkz fcaenzsnoxsn phdqu zjbbbybjmdn dbfhvxwnusz dqbqskfbwuomzsl ogsdioilk") should return "vhyxdefryeznlkz"
Test Failed
String index out of range: -1
Stack Trace
high("yqbzfuprmezbgee yxsewucg u") should return "yqbzfuprmezbgee"
Test Failed
String index out of range: -1
Stack Trace
Completed in 1ms
high("zifha") should return "zifha"
Test Failed
String index out of range: -1
Stack Trace
high("moroydyolj tcfpokvitzwzor rnzeacau") should return "tcfpokvitzwzor"
Test Failed
"[m]" was not equal to "[tcfpokvitzwzor]"
Stack Trace
Completed in 1ms
high("jhieih m") should return "jhieih"
Test Failed
String index out of range: -1
Stack Trace
high("yn ounbzw wk eldpjyikbfs nzm") should return "eldpjyikbfs"
Test Failed
"[ ]" was not equal to "[eldpjyikbfs]"
Stack Trace
Completed in 1ms