javascript 检查数字是否在字符串描述的范围内

disbfnqx  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(130)

我把范围描述为字符串let ranges = "0,71-140,34,142-216,20-30,7"(未排序;一个数字例如34表示范围34-34)。

  • 如何检查数字num是否在某个范围内(给定范围)
  • 如何检查数字小于最小范围还是大于最大范围?

这是question的反转。

const isInRanges = (ranges, num) => {
  return false; // magic here
}

const isOutOfRanges = (ranges, num) => {
  return false; // magic here
}

// ------------------------------------------------
// TESTS - whe should get allways TRUE in console
// ------------------------------------------------

let myRanges = "0,71-140,34,142-216,20-30,7";

// is in tests
let casesIn = [
  [0, true],
  [25, true],
  [35, false],
  [200, true],
  [8, false]
];

for (const c of casesIn) {
  console.log(c[0], isInRanges(myRanges, c[0]) == c[1])
}

// is out tests
let casesOut = [
  [-2, true],
  [60, false],
  [300, true],
  [7, false]
];

for (const c of casesOut) {
  console.log(c[0], isOutOfRanges(myRanges, c[0]) == c[1])
}

解决方案将是两个函数(看代码片段),返回真/假-并通过所有测试用例(我们应该看到所有的'真'在控制台)。

5gfr0r5j

5gfr0r5j1#

最初由@KamilKiełczewski发布的代码可以稍微裁剪一下,这样看起来就像这样。

const isInRanges = (ranges, num) => {
  return ranges.split(',')
    .map(r => r.split('-')) // we're splitting right away
    .some(r => r.length == 1 ? num == +r[0] : num >= +r[0] && num <= +r[1]);
}

const isOutOfRanges = (ranges, num) => {
  // we're avoiding the sorting ...
  const sorted = ranges.match(/\d+/g).map(Number);
  // ... because we're going to use min and max
  return num < Math.min(...sorted) || num > Math.max(...sorted);
}

// ------------------------------------------------
// TESTS - we should get allways TRUE in console
// ------------------------------------------------

let myRanges = "0,71-140,34,142-216,20-30,7";

// is in tests
let casesIn = [
  [0, true],
  [25, true],
  [35, false],
  [200, true],
  [8, false]
];

for (const c of casesIn) {
  console.log(c[0], isInRanges(myRanges, c[0]) == c[1])
}

// is out tests
let casesOut = [
  [-2, true],
  [60, false],
  [300, true],
  [7, false]
];

for (const c of casesOut) {
  console.log(c[0], isOutOfRanges(myRanges, c[0]) == c[1])
}

我对性能很好奇,也很想知道扩展时是否会有问题,所以我决定在JSBench.me上运行一些测试(原始代码与此修订版),在运行了几个连续的测试后,看起来精简版更快一些。

daolsyd0

daolsyd02#

这是我的答案(为后代)-但可能有人有更好的?

const isInRanges = (ranges, num) => {
  return ranges.split(',')
    .map(r => r.split('-'))
    .some(r => r.length == 1 ? num == +r[0] : num >= +r[0] && num <= +r[1]);
}

const isOutOfRanges = (ranges, num) => {
  const sorted = ranges.match(/\d+/g).map(Number).sort((a, b) => a - b);
  return num < sorted.at(0) || num > sorted.at(-1);
}

// ------------------------------------------------
// TESTS - we should get allways TRUE in console
// ------------------------------------------------

let myRanges = "0,71-140,34,142-216,20-30,7";

// is in tests
let casesIn = [
  [0, true],
  [25, true],
  [35, false],
  [200, true],
  [8, false]
];

for (const c of casesIn) {
  console.log(c[0], isInRanges(myRanges, c[0]) == c[1])
}

// is out tests
let casesOut = [
  [-2, true],
  [60, false],
  [300, true],
  [7, false]
];

for (const c of casesOut) {
  console.log(c[0], isOutOfRanges(myRanges, c[0]) == c[1])
}

相关问题