简体   繁体   English

Python 检查 integer 是否都是相同的数字

[英]Python Check if an integer is all the same digit

My goal here is to take a serial number and attempt to reject it.我的目标是获取一个序列号并尝试拒绝它。 I need to reject anything that is not a number.我需要拒绝任何不是数字的东西。 I also need to reject anything where each digit is the same number.我还需要拒绝每个数字都是相同数字的任何东西。 I know there has to be a way other than listing out 111111111 - 999999999. I need to mention that s is an integer with 9 digits such as 123456789. the set() method will not work here as an int object is not iterable.我知道除了列出 111111111 - 999999999 之外,还必须有其他方法。我需要提一下,s 是一个 integer,有 9 位数字,例如 123456789。set() 方法在这里不能作为 int ZA8CFDE6331BD49EB2AC96F891 工作。

def rejectSerial(s):
    try:
        int(s)
    except:
        return True
    rejectList = [111111111],[222222222],[333333333],[444444444],[555555555],[666666666]
    if s in rejectList:
        return True

    return False

Convert the string to a set and see if it has more than 1 element in it:将字符串转换为一个集合,看看它是否有超过 1 个元素:

return (len(set(s)) == 1)

The entire function could be:整个 function 可以是:

def rejectSerial(s: str) -> bool:
    return (
        not s.isdecimal()
        or len(set(s)) == 1
    )

This assumes that s is a string, as implied by the int() call in your original post.这假设s是一个字符串,正如您原始帖子中的int()调用所暗示的那样。 If s is already an int then you don't need to check whether it's a number and the function would simply be:如果s已经是一个int那么你不需要检查它是否是一个数字,并且 function 将只是:

def rejectSerial(s: int) -> bool:
    return len(set(str(s))) == 1

If the type of s is completely unknown and you want the function to accept either numbers OR string representations of numbers and simply return True for other input types, then you could just convert it to a string and then apply the string version of this check:如果s的类型完全未知,并且您希望 function 接受数字或数字的字符串表示形式并简单地为其他输入类型返回 True,那么您可以将其转换为字符串,然后应用此检查的字符串版本:

def rejectSerial(s) -> bool:
    return (
        not str(s).isdecimal()
        or len(set(str(s))) == 1
    )

This will return True if you pass random types like None or a list to it, since the string representation of those will fail the isdecimal() check.如果您将随机类型(如None或列表)传递给它,这将返回 True ,因为它们的字符串表示将无法通过isdecimal()检查。

If you want the function be be able to accept arbitrary (non-int) values without raising an exception, but only return False for int values (not string representations of int values), and you want to enforce the 9-digit positive number requirement, THEN you might want:如果您希望 function 能够在不引发异常的情况下接受任意(非 int)值,但只为 int 值返回False (不是 int 值的字符串表示),并且您希望强制执行 9 位正数要求, 那么你可能想要:

def rejectSerial(s) -> bool:
    return not (
        isinstance(s, int)
        and 100000000 <= s <= 999999999
        and len(set(str(s))) != 1
    )

you can use set to achieve this.您可以使用set来实现这一点。

def rejectSerial(s):
    try:
        int(s)
    except:
        return True
    if len(set(s)) == 1:
        return True

    return False

@Maxwell: A possible solution could be implemented as in the follows: @Maxwell:可能的解决方案可以如下实现:

def rejectSerial(data):
    """Reject data is an integer with different digits.

    Parameters
    ----------
    data: int
        A serial number

    Returns:
    --------
    True if data is not and integer or if it is an integer with the same digits
    False otherwise (this means the data is conforming and must not be rejected)."""
    throw = not isinstance(data, int)   # False is the data is an integer
    datastr = str(data)
    similar = None,
    if len(datastr) == 1: # is the data an integer < 10
        similar = False   # it is a good serial number, and must not be rejected
    else:
        # Create and iterable (here a generator) for testing where the digits are the
        # same
        similar = (datastr[0] == item for item in datastr)
     result = throw or all(similar)
     return result

#------ Test ------
rejectSerial(1)            # -> False
rejectSerial(11111)        # -> True
rejectSerial(11112)        # -> False

in C language:在 C 语言中:

int CheckDigits(int givenNumber)
{
    int m = givenNumber; // spare to number to digits
    int count = 0;
    while (m != 0) 
    {
        m = m / 10;
        count++;
    }
    //printf("digit number :%d \n", count);
    
    if(count == 1)
    {
         return 2; // check one digit
    }

    int tempArray[count];
    for (int i = 0; i < count; i++)
    {
        tempArray[i] = givenNumber%10;
        givenNumber=givenNumber/10;
        //printf("Digit %d = %d, ",count-i, tempArray[i]);
    }
    
    for (int i = 0; i < count; i++)
    {
        if(tempArray[0] != tempArray[i])
        {
            return 0;
        }
    }
    return 1;
}

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

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