簡體   English   中英

Python 檢查 integer 是否都是相同的數字

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

我的目標是獲取一個序列號並嘗試拒絕它。 我需要拒絕任何不是數字的東西。 我還需要拒絕每個數字都是相同數字的任何東西。 我知道除了列出 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

將字符串轉換為一個集合,看看它是否有超過 1 個元素:

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

整個 function 可以是:

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

這假設s是一個字符串,正如您原始帖子中的int()調用所暗示的那樣。 如果s已經是一個int那么你不需要檢查它是否是一個數字,並且 function 將只是:

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

如果s的類型完全未知,並且您希望 function 接受數字或數字的字符串表示形式並簡單地為其他輸入類型返回 True,那么您可以將其轉換為字符串,然后應用此檢查的字符串版本:

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

如果您將隨機類型(如None或列表)傳遞給它,這將返回 True ,因為它們的字符串表示將無法通過isdecimal()檢查。

如果您希望 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
    )

您可以使用set來實現這一點。

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

    return False

@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

在 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