简体   繁体   English

检查数字中的数字是否按升序排列

[英]Check if the digits in a number are in ascending order

I have a string that represents a number as such:我有一个代表数字的字符串:

445123966682

In that number there are 3 digits that are in ascending order: 123 I want to write a rule that checks any numerical string I give it to see if there are 3 or more numbers in ascending or descending order.在该数字中,有 3 个数字按升序排列:123 我想编写一个规则来检查我给它的任何数字字符串,以查看是否有 3 个或更多数字按升序或降序排列。

True: 445123 or 445987 False: 192837 or 97531正确:445123 或 445987 错误:192837 或 97531

I presume the best way is to use a RegEx check, but I am not the best at RegEx.我认为最好的方法是使用 RegEx 检查,但我在 RegEx 方面不是最好的。 The only other option I can think of is to either iterate the characters and check or cast the number to an integer and use modulo + division to grab each digit off of the number and compare with the next number in the series.我能想到的唯一其他选择是迭代字符并检查或将数字转换为 integer 并使用模+除法从数字中提取每个数字并与系列中的下一个数字进行比较。

Edit Sorry, I meant contiguous order.编辑对不起,我的意思是连续的顺序。 123 is valid, 135 is not. 123 有效,135 无效。

With regexp, it's kind of trivial:使用正则表达式,这有点微不足道:

/012|123|234|345|456|567|678|789|987|876|765|654|543|432|321|210/

It's dumb, but the problem is simple enough that the dumb solution is the best one.这很愚蠢,但问题很简单,愚蠢的解决方案是最好的解决方案。

I am not too sure you can use RegExp here, as the problem is more complex as it may appear.我不太确定您是否可以在此处使用 RegExp,因为问题可能看起来更复杂。 You need 3 numbers in direct or reverse order.您需要 3 个直接或相反顺序的数字。 It seems like you can have a lot of combinations to put into the regexp.似乎您可以将很多组合放入正则表达式中。

I suggest to design a simple buffer and a lookout window.我建议设计一个简单的缓冲区和了望 window。 Buffer size may equal to the lookout windows size.缓冲区大小可能等于监视 windows 大小。 You put a symbol in a buffer and check the lookout window for the next few symbols.您将一个符号放在缓冲区中,然后检查了望台 window 是否有接下来的几个符号。 Step-by-step you can add complexity to such algorithm to remember the place, length etc.您可以逐步增加此类算法的复杂性以记住位置、长度等。

I think this is how I would approach the problem:我认为这就是我解决问题的方法:

public bool TestForAscending()
    {
        var regex = new Regex(@"(\d)(?=(\d{2}))");

        var target = "120847212340876";

        var matches = regex.Matches(target);

        List<string> groups = new List<string>();

        foreach( Match m in matches )
        {
            groups.Add(m.Groups[1].Value + m.Groups[2].Value);
        }

        return groups.Any(x => IsAscending(x));
    }

    public static bool IsAscending(string x)
    {
        if (x.Length == 1)
        {
            return true;
        }

        int last = int.Parse(x.Last().ToString());
        int prev = int.Parse(x[x.Length - 2].ToString());

        return last == prev + 1 && IsAscending(x.Substring(0, x.Length - 1));
    }

Here you have a regex that will return every 3 digit group from the target string (you can change how many consecutive digits you want to test by adjusting the quantifier in the regex).在这里,您有一个正则表达式,它将从目标字符串中返回每 3 位数字组(您可以通过调整正则表达式中的量词来更改要测试的连续数字的数量)。 The catch with this regex is that it breaks up the pair among 2 groups, so to get the full number, you have to concat group 1 and group 2. But that's easy.这个正则表达式的问题是它把这对分成两组,所以要得到完整的数字,你必须连接第 1 组和第 2 组。但这很容易。 After that, you just write a little method to test if the number is ascending (I chose recursive).之后,您只需编写一个小方法来测试数字是否在升序(我选择了递归)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM