給你timestamps(list)、scale_min(number)、scale_max(number)、interval(string)和curr_interval(string)。
時間戳按從oldest: 0到newest: timestamps.length - 1可以添加到無窮大。
使用刻度的最小值、最大值和間隔,生成一個串列。
示例 1
Input: timestamps=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'], scale_min=.5, scale_max=1.5, interval='.1', curr_interval='1.5'
Output: 10
Explanation: The generated list of scales = ['0.5', '0.6', '0.7', '0.8', '0.9', '1', '1.1', '1.2', '1.3', '1.4', '1.5']
with the current interval being '1.5', the index of that interval on the timestamps is 10
示例 2
Input: timestamps=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'], scale_min=.5, scale_max=1.5, interval='.1', curr_interval='1.5'
Output: 12
Explanation: The generated list of scales = ['0.5', '0.6', '0.7', '0.8', '0.9', '1', '1.1', '1.2', '1.3', '1.4', '1.5']
with the current interval being '1.5', the index of that interval on the timestamps is 12
示例 3
Input: timestamps=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'], scale_min=.5, scale_max=1.5, interval='.1', curr_interval='1'
Output: 5
Explanation: The generated list of scales = ['0.5', '0.6', '0.7', '0.8', '0.9', '1', '1.1', '1.2', '1.3', '1.4', '1.5']
with the current interval being '1' the middle, the middle index on timestamps is 'f'(5)
示例 4
Input: timestamps=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'], scale_min=.5, scale_max=1.5, interval='.1', curr_interval='1'
Output: 6
約束
timestamps.length > 0
scale_min < curr_interval < scale_max
據我所知
var values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'];
const minimumScaleValue = .5;
const maximumScaleValue = 1.5;
const scaleInterval = .1;
let currentScale;
var scaleRange = [];
for (let i = minimumScaleValue; i < maximumScaleValue; i = scaleInterval) {
scaleRange.push(i.toFixed(2));
}
scaleRange.push(maximumScaleValue.toFixed(2));
currentScale = 1.2;
const solution1 = values[scaleRange.indexOf(currentScale.toFixed(2))]
// scaleRange = ['0.50', '0.60', '0.70', '0.80', '0.90', '1.00', '1.10', '1.20', '1.30', '1.40', '1.50']
// index = scaleRange.indexOf(currentScale.toFixed(2)) = 7
// values[7] = h, correct
console.log(scaleRange, solution1)
currentScale = 1.5;
const solution2 = values[scaleRange.indexOf(currentScale.toFixed(2))]
// scaleRange = ['0.50', '0.60', '0.70', '0.80', '0.90', '1.00', '1.10', '1.20', '1.30', '1.40', '1.50']
// index = scaleRange.indexOf(currentScale.toFixed(2)) = 10
// values[10] = k, correct
console.log(scaleRange, solution2)
// adding more data
values.push('l');
currentScale = 1.5;
const solution3 = values[scaleRange.indexOf(currentScale.toFixed(2))]
// scaleRange = ['0.50', '0.60', '0.70', '0.80', '0.90', '1.00', '1.10', '1.20', '1.30', '1.40', '1.50']
// index = scaleRange.indexOf(currentScale.toFixed(2)) = 10
// values[10] = k, incorrect, should be l
console.log(scaleRange, solution3)
uj5u.com熱心網友回復:
獲取索引或實際陣列值似乎相對簡單,代碼如下:
const getIndex = (xs, min, max) => (x) =>
Math.round((xs .length - 1) * (x - min) / (max - min))
const getValue = (xs, min, max) => (x) =>
xs [Math.round((xs .length - 1) * (x - min) / (max - min))]
const tests = [
{timestamps: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'],
scale_min: .5, scale_max: 1.5, curr_interval: 1.5},
{timestamps: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'],
scale_min: .5, scale_max: 1.5, curr_interval: 1.5},
{timestamps: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'],
scale_min: .5, scale_max: 1.5, curr_interval: 1},
{timestamps: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'],
scale_min: .5, scale_max: 1.5, curr_interval: 1},
]
tests .forEach (({timestamps, scale_min, scale_max, curr_interval}) =>
console .log (`${timestamps .join ('')} - [${scale_min} - ${scale_max}] - ${curr_interval} --> '${
getValue(timestamps, scale_min, scale_max) (curr_interval)
}' (index ${getIndex(timestamps, scale_min, scale_max) (curr_interval)})`)
)
但是我擔心這可能會在您curr_interval以字串形式提供并包含完全可推導的interval(我忽略的)時丟失某些內容。此解決方案缺少什么?
這確實取決于min小于max和curr_interval落在該范圍內。但如果任一失敗,它應該簡單地 return undefined,這似乎是合理的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312882.html
標籤:javascript 数组 算法 数学
上一篇:在python中使用Newton-Raphson求解雙曲函式
下一篇:用C語言改變計數
