gpt4 book ai didi

javascript - 来自 CodeWars 的重量重量

转载 作者:行者123 更新时间:2023-12-04 14:11:21 25 4
gpt4 key购买 nike

问题是:

"My friend John and I are members of the "Fat to Fit Club (FFC)". Johnis worried because each month a list with the weights of members ispublished and each month he is the last on the list which means he isthe heaviest.

I am the one who establishes the list so I told him: "Don't worry anymore, I will modify the order of the list". It was decided toattribute a "weight" to numbers. The weight of a number will be fromnow on the sum of its digits.

For example 99 will have "weight" 18, 100 will have "weight" 1 so inthe list 100 will come before 99. Given a string with the weights ofFFC members in normal order can you give this string ordered by"weights" of these numbers?"

例子

"56 65 74 100 99 68 86 180 90" ordered by numbers weights becomes: "100 180 90 56 65 74 68 86 99"When two numbers have the same "weight", let us class them as if theywere strings (alphabetical ordering) and not numbers: 100 is before180 because its "weight" (1) is less than the one of 180 (9) and 180is before 90 since, having the same "weight" (9), it comes before as astring.

All numbers in the list are positive numbers and the list can beempty.

这是我目前的代码:

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(' ')
}

字符串上的代码字即使有两个具有相同值的连续数字,但是当添加 3+ 个具有相同总和的数字时它开始中断....这里有一些破坏它的字符串:

Expected: '112 14 170 233100 63 29 65 138 156 67 77 79 324612 144435143275 335392 477504 460549 96194 281479 347984', instead got: '112 14170 63 233100 29 65 156 138 67 77 79 324612 144435 143275 335392477504 460549 96194 281479 347984'

Expected: '200 113 41 114 25 52 109 155 83 76 161330 59 450231 27411193131 440830 432353 274292 320986 371567 29858', instead got: '200 41113 114 52 25 109 83 155 76 59 161330 450231 274111 93131 440830432353 274292 320986 371567 29858'

被困在这个问题上的时间比我承认大声笑

谢谢

最佳答案

对于相同的总和,您需要单次排序和按字符串排序。

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');

关于javascript - 来自 CodeWars 的重量重量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63971132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com