An integer is ascending if each digit is greater than or equal to the preceding digit. For example, the integers , and are ascending.
Your task is to count how many ascending integers of a given length can be formed using a given set of digits.
For example, when the length is and the allowed digits are , and , the desired answer is , because in this case the valid ascending integers are , , , , , , , , and .
In a file incnum.py
, implement the function count_numbers
that takes the length and the allowed digits as parameters. The function should return the number of ascending integers.
The function should be efficient when the length of the integer is in the range .
def count_numbers(length, numbers): # TODO if __name__ == "__main__": print(count_numbers(3, "123")) # 10 print(count_numbers(5, "1")) # 1 print(count_numbers(2, "137")) # 6 print(count_numbers(8, "25689")) # 495 print(count_numbers(1, "0")) # 1 print(count_numbers(2, "0")) # 0 print(count_numbers(10, "12")) # 11 print(count_numbers(10, "123456789")) # 43758
Notice that an ascending integer may not have leading zeros if the length is two or more. For example, and are not valid ascending integers, but is an ascending integer.