|
| 1 | + |
| 2 | +## Description |
| 3 | + |
| 4 | +My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest. |
| 5 | + |
| 6 | +I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits. |
| 7 | + |
| 8 | +For example `99` will have "weight" `18`, `100` will have "weight" `1` so in the list `100` will come before `99`. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers? |
| 9 | + |
| 10 | +### Examples |
| 11 | +`"56 65 74 100 99 68 86 180 90"` ordered by numbers weights becomes: `"100 180 90 56 65 74 68 86 99"` |
| 12 | + |
| 13 | +When two numbers have the same "weight", let us class them as if they were strings and not numbers: `100` is before `180` because its "weight" (1) is less than the one of `180` (9) and `180` is before `90` since, having the same "weight" (9) it comes before as a string. |
| 14 | + |
| 15 | +All numbers in the list are positive numbers and the list can be empty. |
| 16 | + |
| 17 | +### Notes |
| 18 | +* it may happen that the input string have leading, trailing whitespaces and more than a unique whitespace between two consecutive numbers |
| 19 | +* Don't modify the input |
| 20 | +* For C: The result is freed. |
| 21 | + |
| 22 | +**Kata's link**: [Valid Parentheses](https://www.codewars.com/kata/weight-for-weight) |
| 23 | + |
| 24 | +## Best Practices |
| 25 | + |
| 26 | +**First:** |
| 27 | +```js |
| 28 | +function orderWeight(strng) { |
| 29 | + return strng |
| 30 | + .split(" ") |
| 31 | + .map(function(v) { |
| 32 | + return { |
| 33 | + val: v, |
| 34 | + key: v.split("").reduce(function(prev, curr) { |
| 35 | + return parseInt(prev) + parseInt(curr); |
| 36 | + }, 0) |
| 37 | + }; |
| 38 | + }) |
| 39 | + .sort(function(a, b) { |
| 40 | + return a.key == b.key |
| 41 | + ? a.val.localeCompare(b.val) |
| 42 | + : (a.key - b.key); |
| 43 | + }) |
| 44 | + .map(function(v) { |
| 45 | + return v.val; |
| 46 | + }) |
| 47 | + .join(" "); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +**Second:** |
| 52 | +```js |
| 53 | +function orderWeight(strng) { |
| 54 | + const sum = (str)=>str.split('').reduce((sum,el)=>(sum+(+el)),0); |
| 55 | + function comp(a,b){ |
| 56 | + let sumA = sum(a); |
| 57 | + let sumB = sum(b); |
| 58 | + return sumA === sumB ? a.localeCompare(b) : sumA - sumB; |
| 59 | + }; |
| 60 | + return strng.split(' ').sort(comp).join(' '); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## My solutions |
| 65 | +```js |
| 66 | +function sumOfWeightDigits (weight) { |
| 67 | + return ('' + weight).split('').reduce((prev, next) => { |
| 68 | + return ~~prev + ~~next; |
| 69 | + }, 0); |
| 70 | +} |
| 71 | + |
| 72 | +function compare (a, b) { |
| 73 | + return a < b ? -1 : 1; |
| 74 | +} |
| 75 | + |
| 76 | +function orderWeight(strng) { |
| 77 | + // your code |
| 78 | + const weights = strng.trim().split(/\s{1,}/).map((v, i) => { |
| 79 | + return { |
| 80 | + key: '' + v, |
| 81 | + val: sumOfWeightDigits(v) |
| 82 | + } |
| 83 | + }).sort((a, b) => { |
| 84 | + const diff = a.val - b.val; |
| 85 | + return diff === 0 ? compare(a.key, b.key) : diff; |
| 86 | + }); |
| 87 | + |
| 88 | + return weights.map(item => item.key).join(' '); |
| 89 | +} |
| 90 | +``` |
0 commit comments