Guest User

Untitled

a guest
May 16th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3.  
  4. def is_prime(n):
  5.     """
  6.    >>> is_prime(2)
  7.    True
  8.    >>> is_prime(3)
  9.    True
  10.    >>> is_prime(4)
  11.    False
  12.    >>> is_prime(20)
  13.    False
  14.    >>> is_prime(21)
  15.    True
  16.    >>> is_prime(43243432)
  17.    False
  18.    """
  19.     if n < 3:
  20.         return True
  21.     elif n % 2 == 0:
  22.         return False
  23.     else:
  24.         return True
  25.  
  26.  
  27. def num_digits(n):
  28.     """
  29.    >>> num_digits(12345)
  30.    5
  31.    >>> num_digits(0)
  32.    1
  33.    >>> num_digits(-12345)
  34.    5
  35.    """
  36.     count = 0
  37.     if n == 0: return 1
  38.     while n:
  39.         count = count + 1
  40.         n = abs(n) / 10
  41.     return count
  42.  
  43.  
  44. def num_even_digits(n):
  45.     """
  46.    >>> num_even_digits(123456)
  47.    3
  48.    >>> num_even_digits(2468)
  49.    4
  50.    >>> num_even_digits(1357)
  51.    0
  52.    >>> num_even_digits(2)
  53.    1
  54.    >>> num_even_digits(20)
  55.    2
  56.    """
  57.     count = 0
  58.     while n:
  59.         i = n % 10
  60.         num = (n - i) / 10
  61.         if i % 2 == 0:
  62.             count += 1
  63.         n = num
  64.     return count
  65.  
  66. if __name__ == '__main__':
  67.     import doctest
  68.     doctest.testmod()
Add Comment
Please, Sign In to add comment