added catalan sequence
parent
5b028ab3c3
commit
bbef37eefb
|
@ -1,28 +1,34 @@
|
||||||
# Python
|
# Python
|
||||||
|
|
||||||
## Arrays
|
## Arrays
|
||||||
|
|
||||||
1. [Count Inversions](arrays/counting_inversions.py)
|
1. [Count Inversions](arrays/counting_inversions.py)
|
||||||
2. [Majority Element](arrays/majority_element.py)
|
2. [Majority Element](arrays/majority_element.py)
|
||||||
3. [Rotate Array](arrays/rotate_array.py)
|
3. [Rotate Array](arrays/rotate_array.py)
|
||||||
4. [Missing Number](arrays/missing_number.py)
|
4. [Missing Number](arrays/missing_number.py)
|
||||||
|
|
||||||
## Linked Lists
|
## Linked Lists
|
||||||
|
|
||||||
1. [Doubly](linked_lists/doubly.py)
|
1. [Doubly](linked_lists/doubly.py)
|
||||||
2. [Singly](linked_lists/singly.py)
|
2. [Singly](linked_lists/singly.py)
|
||||||
|
|
||||||
## Multiplication
|
## Multiplication
|
||||||
|
|
||||||
1. [Karatsuba](multiplication/karatsuba.py)
|
1. [Karatsuba](multiplication/karatsuba.py)
|
||||||
|
|
||||||
## Scheduling
|
## Scheduling
|
||||||
|
|
||||||
1. [Interval Scheduling](scheduling/interval_scheduling.py)
|
1. [Interval Scheduling](scheduling/interval_scheduling.py)
|
||||||
|
|
||||||
## Searching
|
## Searching
|
||||||
|
|
||||||
1. [Binary Search](searching/binary_search.py)
|
1. [Binary Search](searching/binary_search.py)
|
||||||
2. [Jump Search](searching/jump_search.py)
|
2. [Jump Search](searching/jump_search.py)
|
||||||
3. [Linear Search](searching/linear_search.py)
|
3. [Linear Search](searching/linear_search.py)
|
||||||
4. [Ternary Search](searching/ternary_search.py)
|
4. [Ternary Search](searching/ternary_search.py)
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
|
|
||||||
1. [Bubble Sort](sorting/bubble_sort.py)
|
1. [Bubble Sort](sorting/bubble_sort.py)
|
||||||
2. [Comb Sort](sorting/comb_sort.py)
|
2. [Comb Sort](sorting/comb_sort.py)
|
||||||
3. [Insertion Sort](sorting/insertion_sort.py)
|
3. [Insertion Sort](sorting/insertion_sort.py)
|
||||||
|
@ -33,6 +39,7 @@
|
||||||
8. [Shell Sort](sorting/shell-sort.py)
|
8. [Shell Sort](sorting/shell-sort.py)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
|
|
||||||
1. [Is Good Str](strings/is_good_str.py)
|
1. [Is Good Str](strings/is_good_str.py)
|
||||||
2. [Palindrome](strings/palindrome.py)
|
2. [Palindrome](strings/palindrome.py)
|
||||||
3. [Word Count](strings/word_count.py)
|
3. [Word Count](strings/word_count.py)
|
||||||
|
@ -41,7 +48,8 @@
|
||||||
6. [Longest Common Subsequence](strings/longest_common_subsequence.py)
|
6. [Longest Common Subsequence](strings/longest_common_subsequence.py)
|
||||||
|
|
||||||
## Dynamic Programming
|
## Dynamic Programming
|
||||||
|
|
||||||
1. [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
1. [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
||||||
2. [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py)
|
2. [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py)
|
||||||
3. [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
3. [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
||||||
|
4. [Catlan Sequence](dynamic_programming/cataln_sequence.py)
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
# A dynamic programming based function to find nth
|
||||||
|
# Catalan number
|
||||||
|
|
||||||
|
"""
|
||||||
|
Output:
|
||||||
|
The first 10 terms of Catalan sequence are :
|
||||||
|
1 1 2 5 14 42 132 429 1430 4862
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def catalan(n):
|
||||||
|
# Base Case
|
||||||
|
if n == 0 or n == 1:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# To store the result of subproblems
|
||||||
|
|
||||||
|
cat_num = [0] * (n + 1)
|
||||||
|
|
||||||
|
cat_num[0] = 1
|
||||||
|
cat_num[1] = 1
|
||||||
|
|
||||||
|
for i in range(2, n + 1):
|
||||||
|
for j in range(i):
|
||||||
|
cat_num[i] += cat_num[j] * cat_num[i - j - 1]
|
||||||
|
return cat_num[n]
|
||||||
|
|
||||||
|
|
||||||
|
n = 10
|
||||||
|
|
||||||
|
if n<0:
|
||||||
|
print("Please add a valid number")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
print(f"The first {n} terms of Catalan sequence are : ")
|
||||||
|
for i in range(n):
|
||||||
|
print(catalan(i), end=" ")
|
Loading…
Reference in New Issue