提问人:dev99 提问时间:4/14/2023 最后编辑:mottodev99 更新时间:4/14/2023 访问量:71
按升序对字符串数组进行排序并检查顺序
sort array of string numbers in ascending order and check sequence
问:
我想按升序对字符串数组进行排序,而不会丢失序列并检查是否有重复项
这是我到目前为止的职能
const sortAndCheckSequence = async (value) => {
let data = [...value]; // suppose value is ['1','2','1.1','3','1.1.1','1.2','3.1']
sortedData = data.sort((a, b) => (a > b ? 1 : -1));
// sortedData ['1','1.1','1.1.1','1.2','2','3','3.1']
const CheckSequence = (sortedData) =>{
// check sequence return true if there is no missing sequence and no duplicates else return false
}
}
// assuming data is sorted
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','3','3.1']) // true
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','2.1','3','4','5']) // true
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','2.1','3','4','4.1','5','5.1.1']) // false '5.1' missing
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','2.1','3','4','5','1.2']) // false '1.2' is duplicate
sortAndCheckSequence(['1','1.1','1.1.1','1.2','1.4','2','3','3.1']) // false '1.3' missing
sortAndCheckSequence(['1','1.1','1.1.1','1.1.2','1.2','1.4','2','2.1','9','3','3.1']) //false '9' is not in sequence
答:
1赞
motto
4/14/2023
#1
给定一个值,它后面只有四个可能的值:x.y.z
x.y.z.1
x.y.(z+1)
x.(y+1)
(x+1)
如果我们通过删除每个值与前一个值共有的任何前缀来检查每个值,事情就会变得更容易:
x.y.z.1
截断为 (versus – 整个前值是通用前缀).1
""
x.y.(z+1)
截断为 (versus(z+1)
z
)x.(y+1)
截断为 (versus(y+1)
y.z
)(x+1)
没有通用前缀,因此保持与(x+1)
x.y.z
因此,我们只需要检查每个值是否是附加的前一个值,或者它是否具有下一个数字递增的通用前缀。.1
对于奖励积分,我们可以单独检测重复项,但目前尚不清楚您是否对精确的验证消息感兴趣。
const sortAndCheckSequence = (value) => {
let data = [...value];
sortedData = data.sort((a, b) => (a > b ? 1 : -1));
const removeCommonPrefix = (a, b) => {
const minLen = Math.min(a.length, b.length);
let i = 0;
while (i < minLen && a[i] === b[i]) ++i;
return [a.substr(0, i), a.substr(i), b.substr(i)];
}
const FindError = (sortedData) => {
for (let i = 1; i < sortedData.length; ++i) {
const [prevValue, value] = [sortedData[i - 1], sortedData[i]];
if (value === prevValue)
return `Duplicate ${value}`;
const [prefix, prevSuffix, suffix] = removeCommonPrefix(prevValue, value);
if (prevSuffix === "") {
if (suffix !== ".1")
return `Expected ${prevValue}.1, got ${value}`;
} else if (suffix !== String(parseInt(prevSuffix) + 1)) {
return `Expected ${prefix}${parseInt(prevSuffix) + 1}, got ${value}`;
}
}
return null; // no error
}
const error = FindError(sortedData);
console.log(error || "No error");
return error === null; // Does the sequence pass the check without any errors?
}
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '3', '3.1']); // true
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '2.1', '3', '4', '5']); // true
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.1.1.1', '1.1.1.2', '1.2', '3', '3.1']); // false: 2 is missing
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '2.1', '3', '4', '4.1', '5', '5.1.1']); // false '5.1' missing
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '2.1', '3', '4', '5', '1.2']); // false '1.2' is duplicate
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '1.4', '2', '3', '3.1']); // false '1.3' missing
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.1.2', '1.2', '1.3', '1.4', '2', '2.1', '9', '3', '3.1']); //false '9' is not in sequence
评论
0赞
dev99
4/14/2023
这个函数不返回任何内容?如果数字按顺序排列并且没有重复,我希望该函数简单地返回 true 否则为 false
0赞
motto
4/14/2023
很抱歉,似乎是片段编辑器过于热心的“整洁”功能破坏了代码。现在已修复。
0赞
dev99
4/14/2023
不,对不起,对于每个示例数据,它仍然在控制台上返回空白
0赞
motto
4/14/2023
好吧,我不知道该告诉你什么——你可以点击按钮,看看它的工作原理;您可以将整个内容复制粘贴到本地控制台并运行 .当然,代码本身不如算法重要——你觉得其中有什么部分很难理解吗?Run code snippet
sortAndCheckSequence(['1','1.1'])
1赞
motto
4/14/2023
很高兴知道。当然,您可以删除输出,或者根据需要更好地利用特定的验证消息。这是否完全解决了您的用例?console.log(..)
上一个:整数“x”的起始值应该是多少?
评论