Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // We need your help! We have this list of words and we have to find the one with the smallest weight,
  2. //but we haven't a clue how to approach the problem.
  3.  
  4. // The weight is determined by summing all the letters in it. The letter value corresponds to the position
  5. //in the English alphabet - where a is 1 and z is 26. For example, the word book has a weight of 2 + 15 + 15 + 11 = 43.
  6.  
  7.  
  8. // Treat lower- and uppercase letters the same, so a and A both have the value 1.
  9.  
  10. // Your task is to create a program that finds the word with the smallest weight and prints its weight and the word itself to the standart output.
  11.  
  12. // Input
  13. // On the first line, N - the number of words to follow.
  14. // On the next N lines - a single word.
  15. // Output
  16. // The smallest weight and the word, separated by a space.
  17. // Constraints
  18. // 5 <= N <= 500
  19. // 3 <= word length<= 20
  20. // Sample tests
  21. // Input
  22. //
  23. // 5
  24. // acceptable
  25. // funny
  26. // slippery
  27. // scribble
  28. // nasty
  29. // Output
  30. //
  31. // 68 acceptable
  32. // Input
  33. //
  34. // 5
  35. // rot
  36. // expert
  37. // book
  38. // government
  39. // spiridon
  40. // Output
  41. //
  42. // 43 book
  43. const getGets = (arr) => {
  44.     let index = 0;
  45.  
  46.     return () => {
  47.         const toReturn = arr[index];
  48.         index += 1;
  49.         return toReturn;
  50.     };
  51. };
  52. // this is the test
  53. const test = [
  54.     '5', 'acceptable', 'funny', 'slippery', 'scribble', 'nasty'
  55. ];
  56.  
  57. const gets = this.gets || getGets(test);
  58. const print = this.print || console.log;
  59.  
  60. let numberWords = +gets()
  61. // masiv da pazim dumite
  62. let arrayOfWords = []
  63. // masiv da pazim sumite
  64. let arrayOfSums = []
  65.  
  66. for (let n = 1; n <= numberWords; n++) {
  67.     let sum = 0;
  68.     let word = gets();
  69.     arrayOfWords.push(word);
  70.    
  71.     for (let i = 0; i < word.length; i++) {
  72.         // tejesta na bukvata (cifra)
  73.         let letterWeight = word[i].charCodeAt() - 96;
  74.         sum += letterWeight;
  75.     }
  76.     arrayOfSums.push(sum)
  77. }
  78.  
  79. // namirame nai-malkoto
  80. let smallestWeight = Math.min(...arrayOfSums);
  81.  
  82. // namirame indeksa na nai-malkoto v masiva s cifrite
  83. let indexOfSmallest = arrayOfSums.indexOf(smallestWeight);
  84.  
  85. // indeksa koito namerihme gore suvpada s indeksa ot masiva s dumite
  86. print(arrayOfWords[indexOfSmallest]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement