我如何创建(在Javascript中)一个工具来识别一系列数字中最长的重复模式?

发布于 2024-12-07 01:49:01 字数 219 浏览 1 评论 0原文

好吧,长话短说,我总体上想做的就是测试一系列数千个“先前生成的看似“随机”的数字的随机性水平。

我已经写了一些东西来测试数字的概率然而,取得了巨大成功,下一步是识别重复或重复出现的模式,

我更愿意用 javascript 完成这部分,以避免暂时自学另一种语言。

现在,显然,我可以。使用正则表达式并输入一些随机序列我自己,但这并不理想,并且需要无限的时间才能得到我想要的结果。

Alright, long story short, what I overall am attempting to do, is test the level of randomness in a series of multiple thousands of " previously generated seemingly "random" numbers.

I've already written something that will test for the probability of numbers with great success, however, the next step is identifying repeating or recurring patterns.

I'd prefer to get this part done in javascript, so as to avoid having to teach myself another language for the time being.

Now, obviously, I could just use regex and punch in some random sequences myself, but that is not ideal, and would take an infinite amount of time to get the results I'm after.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-12-14 01:49:01

啊,我错过了你上面的一些评论。我相信这就是您正在寻找的:

function findLongestMatch(StringOfNumbers) {
    var matches = StringOfNumbers.match(/(.{2,})(?=.*?\1)/g);
    if (!matches) { return null; }

    var longestMatch = matches[0];
    var longestMatchLength = longestMatch.length;
    for (matchIndex = 1; matchIndex < matches.length; matchIndex++) {
      if (matches[matchIndex].length > longestMatchLength) {
        longestMatch = matches[matchIndex];
        longestMatchLength = longestMatch.length;
      }
    }
    return longestMatch;
}

它会很慢,但它会完成工作。

Ah, I missed a number of your comments above. I believe this is what you're looking for:

function findLongestMatch(StringOfNumbers) {
    var matches = StringOfNumbers.match(/(.{2,})(?=.*?\1)/g);
    if (!matches) { return null; }

    var longestMatch = matches[0];
    var longestMatchLength = longestMatch.length;
    for (matchIndex = 1; matchIndex < matches.length; matchIndex++) {
      if (matches[matchIndex].length > longestMatchLength) {
        longestMatch = matches[matchIndex];
        longestMatchLength = longestMatch.length;
      }
    }
    return longestMatch;
}

It'll be slow, but it'll get the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文