我想使用不同的语法以方便调试。为什么它不起作用?
I would like to use a different syntax in order to facilitate debugging. Why doesn't it work?
这是一个我无法解决的问题,它来自codingGame。我看到了一个解决方案,但我尝试使用不同的语法以便更好地理解。
上下文
/** Horse Racing Duals (easy) https://www.codingame.com/training/easy/horse-racing-duals
* This puzzle shows that sometimes, the simplest solution is not performant
* enough. You will also learn to handle large arrays and gain experience
* with their processing time.
*
* STATEMENT:
* In this problem you have to find the two numbers that are closest to each
* other among a list of numbers. The list might be really large and force
* you to search for the best possible algorithmic complexity for your
* solution.
*
* STORY:
* To make a race between two horses interesting, you will have to find the
* horses with the closest strength.
**/
print(
new Array(+readline())
.fill()
.map(() => +readline())
.sort((a, b) => a > b)
.map((x, i, arr) => Math.abs(x - arr[i + 1]))
.sort((a, b) => a > b) [0]
);
为什么我不能这样做:
let tab = new Array(+readline())
tab.fill()
tab.map(() => +readline())
tab.sort((a, b) => a > b)
tab.map((x, i, arr) => Math.abs(x - arr[i + 1]))
tab
Why can't I do this:
因为某些 map
方法 return 是一个新数组,而不是就地进行更改。
在您的示例中,仅 sort
就地改变数组,因此您的代码实际上等同于:
let tab = new Array(+readline())
tab.sort((a, b) => a > b)
请注意,sort
回调应该是 return 正数、负数或 0
。 It should not return a Boolean.
为了使其相同,当方法未就地发生变化时,需要重新分配制表符。像这样:
let tab = new Array(+readline())
tab.fill()
tab = tab.map(() => +readline())
tab.sort((a, b) => a > b)
tab = tab.map((x, i, arr) => Math.abs(x - arr[i + 1]))
print(tab)
这是一个我无法解决的问题,它来自codingGame。我看到了一个解决方案,但我尝试使用不同的语法以便更好地理解。
上下文
/** Horse Racing Duals (easy) https://www.codingame.com/training/easy/horse-racing-duals
* This puzzle shows that sometimes, the simplest solution is not performant
* enough. You will also learn to handle large arrays and gain experience
* with their processing time.
*
* STATEMENT:
* In this problem you have to find the two numbers that are closest to each
* other among a list of numbers. The list might be really large and force
* you to search for the best possible algorithmic complexity for your
* solution.
*
* STORY:
* To make a race between two horses interesting, you will have to find the
* horses with the closest strength.
**/
print(
new Array(+readline())
.fill()
.map(() => +readline())
.sort((a, b) => a > b)
.map((x, i, arr) => Math.abs(x - arr[i + 1]))
.sort((a, b) => a > b) [0]
);
为什么我不能这样做:
let tab = new Array(+readline())
tab.fill()
tab.map(() => +readline())
tab.sort((a, b) => a > b)
tab.map((x, i, arr) => Math.abs(x - arr[i + 1]))
tab
Why can't I do this:
因为某些 map
方法 return 是一个新数组,而不是就地进行更改。
在您的示例中,仅 sort
就地改变数组,因此您的代码实际上等同于:
let tab = new Array(+readline())
tab.sort((a, b) => a > b)
请注意,sort
回调应该是 return 正数、负数或 0
。 It should not return a Boolean.
为了使其相同,当方法未就地发生变化时,需要重新分配制表符。像这样:
let tab = new Array(+readline())
tab.fill()
tab = tab.map(() => +readline())
tab.sort((a, b) => a > b)
tab = tab.map((x, i, arr) => Math.abs(x - arr[i + 1]))
print(tab)