admin管理员组文章数量:1025244
The question is:
"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.
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.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will e 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?"
Example
"56 65 74 100 99 68 86 180 90" ordered by numbers weights bees: "100 180 90 56 65 74 68 86 99" When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) 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 es before as a string.
All numbers in the list are positive numbers and the list can be empty.
This is the code I have so far:
function sumOfParts(num) {
return num.split('').reduce((a, b) => parseInt(a) + parseInt(b), 0)
}
function orderWeight(string) {
return string.split(' ').sort().sort((a,b) => sumOfParts(a) - sumOfParts(b)).join(' ')
}
The code words on strings even with two consecutive numbers with the same value but when 3+ numbers with the same sum are added it starts to break.... Here are some strings that broke it:
Expected: '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984', instead got: '112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'
Expected: '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858', instead got: '200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'
Been stuck on this one longer then I would like to admit lol
Thanks
The question is:
"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.
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.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will e 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?"
Example
"56 65 74 100 99 68 86 180 90" ordered by numbers weights bees: "100 180 90 56 65 74 68 86 99" When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) 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 es before as a string.
All numbers in the list are positive numbers and the list can be empty.
This is the code I have so far:
function sumOfParts(num) {
return num.split('').reduce((a, b) => parseInt(a) + parseInt(b), 0)
}
function orderWeight(string) {
return string.split(' ').sort().sort((a,b) => sumOfParts(a) - sumOfParts(b)).join(' ')
}
The code words on strings even with two consecutive numbers with the same value but when 3+ numbers with the same sum are added it starts to break.... Here are some strings that broke it:
Expected: '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984', instead got: '112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'
Expected: '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858', instead got: '200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'
Been stuck on this one longer then I would like to admit lol
Thanks
Share Improve this question asked Sep 19, 2020 at 16:42 Nadav JuliusNadav Julius 3113 silver badges17 bronze badges1 Answer
Reset to default 6You need a single sort and a sorting by string for same sums.
function sumOfParts(num) {
return num.split('').reduce((a, b) => a + +b, 0)
}
function orderWeight(string) {
return string
.split(' ')
.sort((a, b) => sumOfParts(a) - sumOfParts(b) || a > b || -(a < b))
.join(' ');
}
console.log('out', orderWeight('112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'));
console.log('exp', '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984');
console.log('out', orderWeight('200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'));
console.log('exp', '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858');
The question is:
"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.
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.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will e 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?"
Example
"56 65 74 100 99 68 86 180 90" ordered by numbers weights bees: "100 180 90 56 65 74 68 86 99" When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) 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 es before as a string.
All numbers in the list are positive numbers and the list can be empty.
This is the code I have so far:
function sumOfParts(num) {
return num.split('').reduce((a, b) => parseInt(a) + parseInt(b), 0)
}
function orderWeight(string) {
return string.split(' ').sort().sort((a,b) => sumOfParts(a) - sumOfParts(b)).join(' ')
}
The code words on strings even with two consecutive numbers with the same value but when 3+ numbers with the same sum are added it starts to break.... Here are some strings that broke it:
Expected: '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984', instead got: '112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'
Expected: '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858', instead got: '200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'
Been stuck on this one longer then I would like to admit lol
Thanks
The question is:
"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.
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.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will e 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?"
Example
"56 65 74 100 99 68 86 180 90" ordered by numbers weights bees: "100 180 90 56 65 74 68 86 99" When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) 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 es before as a string.
All numbers in the list are positive numbers and the list can be empty.
This is the code I have so far:
function sumOfParts(num) {
return num.split('').reduce((a, b) => parseInt(a) + parseInt(b), 0)
}
function orderWeight(string) {
return string.split(' ').sort().sort((a,b) => sumOfParts(a) - sumOfParts(b)).join(' ')
}
The code words on strings even with two consecutive numbers with the same value but when 3+ numbers with the same sum are added it starts to break.... Here are some strings that broke it:
Expected: '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984', instead got: '112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'
Expected: '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858', instead got: '200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'
Been stuck on this one longer then I would like to admit lol
Thanks
Share Improve this question asked Sep 19, 2020 at 16:42 Nadav JuliusNadav Julius 3113 silver badges17 bronze badges1 Answer
Reset to default 6You need a single sort and a sorting by string for same sums.
function sumOfParts(num) {
return num.split('').reduce((a, b) => a + +b, 0)
}
function orderWeight(string) {
return string
.split(' ')
.sort((a, b) => sumOfParts(a) - sumOfParts(b) || a > b || -(a < b))
.join(' ');
}
console.log('out', orderWeight('112 14 170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984'));
console.log('exp', '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435 143275 335392 477504 460549 96194 281479 347984');
console.log('out', orderWeight('200 41 113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830 432353 274292 320986 371567 29858'));
console.log('exp', '200 113 41 114 25 52 109 155 83 76 161330 59 450231 274111 93131 440830 432353 274292 320986 371567 29858');
本文标签: javascriptWeight for weight from CodeWarsStack Overflow
版权声明:本文标题:javascript - Weight for weight from CodeWars - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745615021a2159236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论