File size: 5,393 Bytes
389d072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'''This module is supposed to contain all the combinatorics related stuff which can be performed by VisualMath (VisMa)

Note: Please try to maintain proper documentation
'''

from visma.io.tokenize import tokenizer
from visma.simplify.simplify import simplify
from visma.io.parser import tokensToString
from visma.functions.constant import Constant


def factorial(tokens):
    '''Used to get factorial of tokens provided

    Argument:
        tokens {list} -- list of tokens

    Returns:
        result {list} -- list of result tokens
        {empty list}
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    '''
    tokens, _, _, _, _ = simplify(tokens)
    animation = []
    comments = []
    if (isinstance(tokens[0], Constant) & len(tokens) == 1):
        value = int(tokens[0].calculate())
        if value == 0:
            result = [Constant(1)]
            comments += [['Factorial of ZERO is defined to be 1']]
            animation += [tokenizer('f = ' + str(1))]
        else:
            resultString = ''
            for i in range(1, value + 1):
                resultString += (str(i) + '*')
            resultString = resultString[:-1]
            resultTokens = tokenizer(resultString)
            comments += [['Expanding the factorial as']]
            animation += [resultTokens]
            result, _, _, _, _ = simplify(resultTokens)
        token_string = tokensToString(result)
        comments += [['Hence result: ']]
        animation += [tokenizer('f = ' + token_string)]
    return result, [], token_string, animation, comments


def permutation(nTokens, rTokens):
    '''Used to get Permutation (nPr)

    Argument:
        nTokens {list} -- list of tokens of "n" in nPr
        rTokens {list} -- list of tokens of "r" in nPr

    Returns:
        result {list} -- list of result tokens
        {empty list}
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    '''
    nTokens, _, _, _, _ = simplify(nTokens)
    rTokens, _, _, _, _ = simplify(rTokens)
    animation = []
    comments = []
    if (isinstance(nTokens[0], Constant) & len(nTokens) == 1) & (isinstance(rTokens[0], Constant) & len(rTokens) == 1):
        comments += [['nCr is defined as (n!)/(r!)*(n-r)!']]
        animation += [[]]
        comments += [['Solving for n!']]
        animation += [[]]
        numerator, _, _, animNew1, commentNew1 = factorial(nTokens)
        commentNew1[1] = ['(n)! is thus solved as: ']
        animation.extend(animNew1)
        comments.extend(commentNew1)
        denominator = nTokens[0] - rTokens[0]
        comments += [['Solving for (n - r)!']]
        animation += [[]]
        denominator, _, _, animNew2, commentNew2 = factorial([denominator])
        commentNew2[1] = ['(n - r)! is thus solved as: ']
        comments.extend(commentNew2)
        animation.extend(animNew2)
        result = [numerator[0] / denominator[0]]
        comments += [['On placing values in (n!)/(n-r)!']]
        animation += [tokenizer('r = ' + tokensToString(result))]
    token_string = tokensToString(result)
    return result, [], token_string, animation, comments


def combination(nTokens, rTokens):
    '''Used to get Combination (nCr)

    Argument:
        nTokens {list} -- list of tokens of "n" in nCr
        rTokens {list} -- list of tokens of "r" in nCr

    Returns:
        result {list} -- list of result tokens
        {empty list}
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    '''
    nTokens, _, _, _, _ = simplify(nTokens)
    rTokens, _, _, _, _ = simplify(rTokens)
    animation = []
    comments = []
    if (isinstance(nTokens[0], Constant) & len(nTokens) == 1) & (isinstance(rTokens[0], Constant) & len(rTokens) == 1):
        comments += [['nCr is defined as (n!)/(r!)*(n-r)!']]
        animation += [[]]
        comments += [['Solving for n!']]
        animation += [[]]
        numerator, _, _, animNew1, commentNew1 = factorial(nTokens)
        commentNew1[1] = ['(n)! is thus solved as: ']
        animation.extend(animNew1)
        comments.extend(commentNew1)
        denominator1 = nTokens[0] - rTokens[0]
        comments += [['Solving for (n - r)!']]
        animation += [[]]
        denominator1, _, _, animNew2, commentNew2 = factorial([denominator1])
        commentNew2[1] = ['(n - r)! is thus solved as: ']
        comments.extend(commentNew2)
        animation.extend(animNew2)
        comments += [['Solving for r!']]
        animation += [[]]
        denominator2, _, _, animNew3, commentNew3 = factorial([rTokens[0]])
        commentNew3[1] = ['r! is thus solved as: ']
        comments.extend(commentNew3)
        animation.extend(animNew3)
        denominator = denominator1[0] * denominator2[0]
        result = [numerator[0] / denominator]
        comments += [['On placing values in (n!)/(r!)*(n-r)!']]
        animation += [tokenizer('r = ' + tokensToString(result))]
    token_string = tokensToString(result)
    return result, [], token_string, animation, comments