Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // We need your help! We have this list of words and we have to find the one with the smallest weight,
- //but we haven't a clue how to approach the problem.
- // The weight is determined by summing all the letters in it. The letter value corresponds to the position
- //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.
- // Treat lower- and uppercase letters the same, so a and A both have the value 1.
- // 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.
- // Input
- // On the first line, N - the number of words to follow.
- // On the next N lines - a single word.
- // Output
- // The smallest weight and the word, separated by a space.
- // Constraints
- // 5 <= N <= 500
- // 3 <= word length<= 20
- // Sample tests
- // Input
- //
- // 5
- // acceptable
- // funny
- // slippery
- // scribble
- // nasty
- // Output
- //
- // 68 acceptable
- // Input
- //
- // 5
- // rot
- // expert
- // book
- // government
- // spiridon
- // Output
- //
- // 43 book
- const getGets = (arr) => {
- let index = 0;
- return () => {
- const toReturn = arr[index];
- index += 1;
- return toReturn;
- };
- };
- // this is the test
- const test = [
- '5', 'acceptable', 'funny', 'slippery', 'scribble', 'nasty'
- ];
- const gets = this.gets || getGets(test);
- const print = this.print || console.log;
- let numberWords = +gets()
- // masiv da pazim dumite
- let arrayOfWords = []
- // masiv da pazim sumite
- let arrayOfSums = []
- for (let n = 1; n <= numberWords; n++) {
- let sum = 0;
- let word = gets();
- arrayOfWords.push(word);
- for (let i = 0; i < word.length; i++) {
- // tejesta na bukvata (cifra)
- let letterWeight = word[i].charCodeAt() - 96;
- sum += letterWeight;
- }
- arrayOfSums.push(sum)
- }
- // namirame nai-malkoto
- let smallestWeight = Math.min(...arrayOfSums);
- // namirame indeksa na nai-malkoto v masiva s cifrite
- let indexOfSmallest = arrayOfSums.indexOf(smallestWeight);
- // indeksa koito namerihme gore suvpada s indeksa ot masiva s dumite
- print(arrayOfWords[indexOfSmallest]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement