"""Print all the Catalan numbers from 0 to n, n being the user input.* The Catalan numbers are a sequence of positive integers that* appear in many counting problems in combinatorics [1]. Such* problems include counting [2]:* - The number of Dyck words of length 2n* - The number well-formed expressions with n pairs of parentheses* (e.g., `()()` is valid but `())(` is not)* - The number of different ways n + 1 factors can be completely* parenthesized (e.g., for n = 2, C(n) = 2 and (ab)c and a(bc)* are the two valid ways to parenthesize.* - The number of full binary trees with n + 1 leaves* A Catalan number satisfies the following recurrence relation* which we will use in this algorithm [1].* C(0) = C(1) = 1* C(n) = sum(C(i).C(n-i-1)), from i = 0 to n-1* In addition, the n-th Catalan number can be calculated using* the closed form formula below [1]:* C(n) = (1 / (n + 1)) * (2n choose n)* Sources:* [1] https://brilliant.org/wiki/catalan-numbers/* [2] https://en.wikipedia.org/wiki/Catalan_number"""def catalan_numbers(upper_limit: int) -> "list[int]":"""Return a list of the Catalan number sequence from 0 through `upper_limit`.>>> catalan_numbers(5)[1, 1, 2, 5, 14, 42]>>> catalan_numbers(2)[1, 1, 2]>>> catalan_numbers(-1)Traceback (most recent call last):ValueError: Limit for the Catalan sequence must be ≥ 0"""if upper_limit < 0:raise ValueError("Limit for the Catalan sequence must be ≥ 0")catalan_list = [0] * (upper_limit + 1)# Base case: C(0) = C(1) = 1catalan_list[0] = 1if upper_limit > 0:catalan_list[1] = 1# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to ifor i in range(2, upper_limit + 1):for j in range(i):catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]return catalan_listif __name__ == "__main__":print("\n********* Catalan Numbers Using Dynamic Programming ************\n")print("\n*** Enter -1 at any time to quit ***")print("\nEnter the upper limit (≥ 0) for the Catalan number sequence: ", end="")try:while True:N = int(input().strip())if N < 0:print("\n********* Goodbye!! ************")breakelse:print(f"The Catalan numbers from 0 through {N} are:")print(catalan_numbers(N))print("Try another upper limit for the sequence: ", end="")except (NameError, ValueError):print("\n********* Invalid input, goodbye! ************\n")import doctestdoctest.testmod()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。