![]() |
Python
1.0
|
Showing a range of rows of a Pascal triangle. More...
Functions | |
def | binomial_array (n) |
Compute n lines of the binomial array. More... | |
def | binomial_coefficient (n, m, method=2) |
Compute \(n\) choose \(m\) or Combination \((n,m)\). More... | |
def | binomial_cumulative_distribution (p, n, x=None) |
Compute the probability of getting at most \(x\) successes in \(n\) trials, which is the sum of the first \(x\) terms of the binomial expansion \((p+q)^n\). More... | |
def | exponent (val, type=False) |
Return a superscript string for the given value. More... | |
def | binomial_expansion (e) |
Compute the binomial expansion of \((x+y)^n\). More... | |
def | getNextRow (curr_row) |
Computes the next row of a Pascal triangle, given the current row. More... | |
def | fmtRow (last_level, r, pad=True) |
Formats a row of the Pascal triangle. More... | |
def | pascal (first_level, last_level, center=True) |
Creates the pascal triangle between two levels. More... | |
def | pascal2 (first_level, last_level, center=True) |
Creates the pascal triangle between two levels, using the binomial array. More... | |
def | main (argv=None) |
Main function for testing. More... | |
Variables | |
dictionary | superscript_map |
dictionary for mapping characters to superscript. More... | |
dictionary | subscript_map |
trans | |
character translation table. More... | |
sub_trans | |
Showing a range of rows of a Pascal triangle.
0 1 C(0,0) 1 1 1 C(1,0) C(1,1) 2 1 2 1 C(2,0) C(2,1) C(2,2) 3 1 3 3 1 C(3,0) C(3,1) C(3,2) C(3,3) 4 1 4 6 4 1 C(4,0) C(4,1) C(4,2) C(4,3) C(4,4) 5 1 5 10 10 5 1 C(5,0) C(5,1) C(5,2) C(5,3) C(5,4) C(5,5)
0 1 2 3 4 5
def _03d_pascal.binomial_array | ( | n | ) |
Compute n lines of the binomial array.
Complexity is \(1 + 2\ +\ ...\ +\ n\) elements. This is arithmetic progression that sums to \({n*(n+1)}\over{2}\), which is in \(O(n^2)\).
0 - 1 1 - 1 1 2 - 1 2 1 3 - 1 3 3 1 4 - 1 4 6 4 1 5 - 1 5 10 10 5 1
n | number of lines. |
Referenced by binomial_coefficient(), binomial_cumulative_distribution(), binomial_expansion(), and pascal2().
def _03d_pascal.binomial_coefficient | ( | n, | |
m, | |||
method = 2 |
|||
) |
Compute \(n\) choose \(m\) or Combination \((n,m)\).
n | number of objects to choose from. |
m | number of places. |
method | how to get the result. |
References binomial_array().
def _03d_pascal.binomial_cumulative_distribution | ( | p, | |
n, | |||
x = None |
|||
) |
Compute the probability of getting at most \(x\) successes in \(n\) trials, which is the sum of the first \(x\) terms of the binomial expansion \((p+q)^n\).
In other words, the probability of obtaining at most \(x\) successes in \(n\) independent trials, each of which has a probability \(p\) of success, and \(q = (1-p)\) of failure.
That is, if \(X\) denotes the number of successes, it returns:
p | probability of success. |
n | number of trials. |
x | number of successes. |
References binomial_array().
Referenced by main().
def _03d_pascal.binomial_expansion | ( | e | ) |
Compute the binomial expansion of \((x+y)^n\).
For e = 5, return:
e | exponent n. |
References binomial_array(), and exponent().
Referenced by main().
def _03d_pascal.exponent | ( | val, | |
type = False |
|||
) |
Return a superscript string for the given value.
val | a numeric string. |
type | select the superscript method. |
Referenced by binomial_expansion().
def _03d_pascal.fmtRow | ( | last_level, | |
r, | |||
pad = True |
|||
) |
def _03d_pascal.getNextRow | ( | curr_row | ) |
Computes the next row of a Pascal triangle, given the current row.
curr_row | current row. |
Referenced by pascal().
def _03d_pascal.main | ( | argv = None | ) |
Main function for testing.
argv | first and last levels of the Pascal triangle. |
References binomial_cumulative_distribution(), binomial_expansion(), _01d_dec2bin.input, pascal(), and pascal2().
def _03d_pascal.pascal | ( | first_level, | |
last_level, | |||
center = True |
|||
) |
Creates the pascal triangle between two levels.
first_level | first level to be printed. |
last_level | height of the Pascal triangle. |
center | whether to center the lines. |
References fmtRow(), and getNextRow().
Referenced by main().
def _03d_pascal.pascal2 | ( | first_level, | |
last_level, | |||
center = True |
|||
) |
Creates the pascal triangle between two levels, using the binomial array.
The lines are centered in respect to the last line of the triangle. The output is written to a docx file.
first_level | first level to be printed. |
last_level | height of the Pascal triangle. |
center | whether to center the lines. |
References binomial_array(), and fmtRow().
Referenced by main().
dictionary _03d_pascal.sub_trans |
dictionary _03d_pascal.subscript_map |
dictionary _03d_pascal.superscript_map |
dictionary for mapping characters to superscript.