如何获得数组序列? [关闭]
Posted
技术标签:
【中文标题】如何获得数组序列? [关闭]【英文标题】:How to get a array sequency? [closed] 【发布时间】:2022-01-05 20:52:09 【问题描述】:我正在尝试验证我的用户的最后一条消息,并验证他是否在我的应用程序中使用 selfbot。
var array = ["a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b"];
// ["a", "b", "c", "f", "b"] is the sequency, selfbot detected?
我试过用这个,但我现在不知道如何继续
var array = ["a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b"];
let counts = ;
array.forEach(x => counts[x] = (counts[x] || 0) + 1; );
console.log(counts);
【问题讨论】:
顺序是什么意思?提供 3 或 4 个样品 @TheMastervar lastCommands = ["balance", "pay", "bank", "balance", "pay", "bank", "balance", "pay", "bank", "balance", "pay", "bank", "balance", "pay", "bank"];
我正在尝试获取用户命令输入的序列,以验证他是否正在使用自我机器人。在这个例子中,sequency是["balance", "pay", "bank"]
【参考方案1】:
一种方法是一一检查(也许不是最有效的):
var sequence = ["a", "b", "c", "f", "b"]; // Sequence that shows whether user is bot
// Test cases
var array1 = ["a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b", "a", "b", "c", "f", "b"];
var array2 = ["b", "a", "b", "b", "f", "c", "a", "b", "c", "d", "e", "f", "a", "f", "c", "c", "c", "c", "f", "a", "a", "b", "b", "e", "e"];
function selfBotDetector(arr, seq)
// Function to check if sequence matches given range
var check = start =>
for(var t = start; t < seq.length + start; t++)
if(arr[t] !== seq[t - start])
return false;
return true;
;
for (var i = 0; i < arr.length; i++)
var result = check(i);
// If at any time the sequence matches return true
if(result)
return true;
// if true was never returned then the sequence did not match
return false;
console.log("Array 1:", selfBotDetector(array1, sequence) ? "Selfbot detected" : "OK");
console.log("Array 2:", selfBotDetector(array2, sequence) ? "Selfbot detected" : "OK");
【讨论】:
我如何获得序列? 你是什么意思“得到序列”? 名为“sequence”的变量,我如何生成她?我将它用于机器人,我需要验证用户消息以防止他使用自我机器人 嗯...我可能误解了你的问题。你能改写一下吗?【参考方案2】:您可以像这样使用递归循环:
/*<ignore>*/console.config(maximize:true,timeStamps:false,autoScroll:false);/*</ignore>*/
const array = [
'a',
'b',
'c',
'f',
'b',
'a',
'b',
'c',
'f',
'b',
'a',
'b',
'c',
'f',
'b',
'a',
'b',
'c',
'f',
'b',
'a',
'b',
'c',
'f',
'b',
];
let num = 0;
(function findSeq(seqStart = 0, seqEnd = 0, i = 0, j = -1)
while (++i < array.length && array[i] !== array[seqStart]);
seqEnd = i;
while (++j < seqEnd && array[i++] === array[j]);
if (seqEnd === j)
console.log(
`Sequence $++num found between`,
seqStart,
seqEnd,
array.slice(seqStart, seqEnd)
);
if (i < array.length) findSeq(seqEnd, 0, i - 1, j - 1);
)();
console.log(`$num sequences found`);
<!-- https://meta.***.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
【讨论】:
@SrNiix_ 考虑通过单击此帖子左侧的复选标记来接受答案。以上是关于如何获得数组序列? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章