mardi 25 août 2020

Boolean True or False upon finding two consecutive digits in a given integer without using any built-in functions

To find two consecutive digits(D) in a given integer(N) without using any built-in functions and returning True or False, the following code seems to be exiting when coming across one D, however it works if there are two Ds. Why is it not working as it is and how to fix it? Thanks!

def double_digits(n, d):
    """Return True if N has two Ds in a row otherwise return False.

    int, int -> Boolean

    >>> double_digits(91019, 1)
    False
    >>> double_digits(88, 8)
    True
    >>> double_digits(2772, 7)
    True
    >>> double_digits(88108, 0)
    False
    >>> double_digits(12345, 4)
    False
    >>> double_digits(81811081, 1)
    True
    """
    while n > 0:
        remainder = n % 10
        n = n // 10
        if remainder == d:
            if n % 10 == d:
                return True
            else:
                remainder, n = n % 10, n // 10
        return False

Aucun commentaire:

Enregistrer un commentaire