Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. object Scoring {
  2.  
  3. def high(s: String): String = {
  4.  
  5. var max = 0
  6. var whichWord = 0
  7. var x = 0
  8. var y = new Array[Int](100)
  9.  
  10.  
  11. for(word <- s.split(" ")){
  12. for(letter <- word) letter match{
  13. case 'a' => y(x)+=1
  14. case 'b' => y(x)+=2
  15. case 'c' => y(x)+=3
  16. case 'd' => y(x)+=4
  17. case 'e' => y(x)+=5
  18. case 'f' => y(x)+=6
  19. case 'g' => y(x)+=7
  20. case 'h' => y(x)+=8
  21. case 'i' => y(x)+=9
  22. case 'j' => y(x)+=10
  23. case 'k' => y(x)+=11
  24. case 'l' => y(x)+=12
  25. case 'm' => y(x)+=13
  26. case 'n' => y(x)+=14
  27. case 'o' => y(x)+=15
  28. case 'p' => y(x)+=16
  29. case 'q' => y(x)+=17
  30. case 'r' => y(x)+=18
  31. case 's' => y(x)+=19
  32. case 't' => y(x)+=20
  33. case 'u' => y(x)+=21
  34. case 'v' => y(x)+=22
  35. case 'w' => y(x)+=23
  36. case 'x' => y(x)+=24
  37. case 'y' => y(x)+=25
  38. case 'z' => y(x)+=26
  39.  
  40. }
  41. x +=1
  42. }
  43.  
  44. for(x <- 0 until y.length){
  45.  
  46. if(y(x) > max)
  47. {
  48. max = y(x)
  49. whichWord = x
  50. }
  51. }
  52.  
  53. s.substring(whichWord-1, whichWord)
  54. }
  55. }
  56.  
  57. RandomTestSpec
  58. high("ykvhorsqve kfkq jhjibercdptf efevxax ccr vnsmumqby jwhxvamegupfcj lierziuopbcsutm") should return "lierziuopbcsutm"
  59. Test Failed
  60.  
  61. "[s]" was not equal to "[lierziuopbcsutm]"
  62.  
  63. Stack Trace
  64.  
  65. Completed in 34ms
  66. high("skwwwm") should return "skwwwm"
  67.  
  68. Test Failed
  69.  
  70. String index out of range: -1
  71. Stack Trace
  72. Completed in 1ms
  73. high("a") should return "a"
  74. Test Failed
  75.  
  76. String index out of range: -1
  77. Stack Trace
  78. Completed in 1ms
  79. high("gykoialocufuc wcdwuxksqk bvapztcnqwx") should return "bvapztcnqwx"
  80. Test Failed
  81.  
  82. "[y]" was not equal to "[bvapztcnqwx]"
  83. Stack Trace
  84. Completed in 1ms
  85. high("gdhfoonwtih xbvsiaqhsesl obrndpz nxt inkklyo lf oyoadxqhuys lbqr oxbqq bopalqknjxvpg") should return "oyoadxqhuys"
  86. Test Failed
  87.  
  88. "o[]" was not equal to "o[yoadxqhuys]"
  89. Stack Trace
  90. Completed in 1ms
  91. high("bbzlmqhsypato pfufsi ryu oboklfa iigha h m") should return "bbzlmqhsypato"
  92. Test Failed
  93.  
  94. String index out of range: -1
  95. Stack Trace
  96. Completed in 1ms
  97. high("dbtfwvhk kadarmvldjhkx dgxffryldcxodtn hoffibiayxriqe gtqzeuywpgc nqlgvudy") should return "dgxffryldcxodtn"
  98. Test Failed
  99.  
  100. "[b]" was not equal to "[dgxffryldcxodtn]"
  101. Stack Trace
  102. Completed in 1ms
  103. high("vhyxdefryeznlkz fcaenzsnoxsn phdqu zjbbbybjmdn dbfhvxwnusz dqbqskfbwuomzsl ogsdioilk") should return "vhyxdefryeznlkz"
  104. Test Failed
  105.  
  106. String index out of range: -1
  107. Stack Trace
  108. high("yqbzfuprmezbgee yxsewucg u") should return "yqbzfuprmezbgee"
  109. Test Failed
  110. String index out of range: -1
  111. Stack Trace
  112. Completed in 1ms
  113. high("zifha") should return "zifha"
  114. Test Failed
  115.  
  116. String index out of range: -1
  117. Stack Trace
  118. high("moroydyolj tcfpokvitzwzor rnzeacau") should return "tcfpokvitzwzor"
  119. Test Failed
  120.  
  121. "[m]" was not equal to "[tcfpokvitzwzor]"
  122. Stack Trace
  123. Completed in 1ms
  124. high("jhieih m") should return "jhieih"
  125. Test Failed
  126.  
  127. String index out of range: -1
  128. Stack Trace
  129. high("yn ounbzw wk eldpjyikbfs nzm") should return "eldpjyikbfs"
  130. Test Failed
  131.  
  132. "[ ]" was not equal to "[eldpjyikbfs]"
  133. Stack Trace
  134. Completed in 1ms
  135.  
  136. "hello".map(_.toInt).sum // 532
  137.  
  138. scala> List("a", "ab","ba").foldLeft("")((a,b)=> higher(a,b))
  139. res10: String = ab
  140.  
  141. def higher(a:String, b:String):String=
  142. if(a.map(_.toInt).sum >= b.map(_.toInt).sum) a else b
  143.  
  144. val str = "ykvhorsqve kfkq jhjibercdptf efevxax ccr vnsmumqby jwhxvamegupfcj lierziuopbcsutm"
  145.  
  146. val charMap = Map(
  147. 'a' -> 1,
  148. 'b' -> 2,
  149. 'c' -> 3,
  150. 'd' -> 4,
  151. 'e' -> 5,
  152. 'f' -> 6,
  153. 'g' -> 7,
  154. 'h' -> 8,
  155. 'i' -> 9,
  156. 'j' -> 10,
  157. 'k' -> 11,
  158. 'l' -> 12,
  159. 'm' -> 13,
  160. 'n' -> 14,
  161. 'o' -> 15,
  162. 'p' -> 16,
  163. 'q' -> 17,
  164. 'r' -> 18,
  165. 's' -> 19,
  166. 't' -> 20,
  167. 'u' -> 21,
  168. 'v' -> 22,
  169. 'w' -> 23,
  170. 'x' -> 24,
  171. 'y' -> 25,
  172. 'z' -> 26,
  173. )
  174.  
  175. def countOfWord(str : String): Int = str.map(charMap).sum
  176.  
  177. str.split(" ").map(word => (word, countOfWord(word))).maxBy(_._2)._1
  178.  
  179. res0: String = lierziuopbcsutm
  180.  
  181. val words = s.split(" ")
  182. val scores = words.map(w => (w, w.map(_ - 'a' + 1).sum))
  183. val max = scores.maxBy(_._2)._2
  184. scores.find(_._2 == max).get._1
  185.  
  186. object HighestWord extends App {
  187. val inputStr: String = "ykvhorsqve kfkq jhjibercdptf efevxax ccr vnsmumqby jwhxvamegupfcj lierziuopbcsutm"
  188.  
  189. val answer = inputStr.split(" ").map {
  190. str: String =>
  191. val sum = str.map { chr: Char =>
  192. chr.toInt
  193. }.sum
  194. (str, sum)
  195. }.maxBy(_._2)._1
  196.  
  197. println(answer)
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement