Code Monkey home page Code Monkey logo

catalan's Introduction

catalan

Here's a simple Python script to calculate the Catalan numbers using a recursive function:

def catalan(n):
    if n <= 1:
        return 1

    res = 0
    for i in range(n):
        res += catalan(i) * catalan(n-i-1)

    return res

# Calculate and print the first 10 Catalan numbers
for n in range(10):
    print(catalan(n))

This script defines a function catalan that takes an integer n as input and returns the n-th Catalan number. The function uses a recursive formula to compute the Catalan number, which is defined as follows:

Catalan(n) = (2n)! / (n! * (n+1)!)

The script then calculates and prints the first 10 Catalan numbers using a for loop.

You can run this script in a Python environment or in a command line by saving the code in a file with a .py extension and running it using python filename.py command.

Here's a test case for the catalan function in the script above:

def test_catalan():
    assert catalan(0) == 1
    assert catalan(1) == 1
    assert catalan(2) == 2
    assert catalan(3) == 5
    assert catalan(4) == 14
    assert catalan(5) == 42
    assert catalan(6) == 132
    assert catalan(7) == 429
    assert catalan(8) == 1430
    assert catalan(9) == 4862

This test function defines multiple assertions that test the catalan function for different input values. Each assertion checks whether the output of the catalan function for a specific input value is equal to the expected value. The expected values are taken from a known sequence of Catalan numbers.

You can run this test function in a Python environment or in a command line by calling test_catalan() function. If all assertions pass, the function will execute without any output. If one or more assertions fail, an AssertionError will be raised indicating which assertion(s) failed and what was the actual output.

catalan's People

Contributors

emmahone avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.