Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def tfSeq(size):
- ''' Generates a list of the given size containing all combinations
- of True and False. Starts with all Trues and ends with all
- Falses.
- '''
- if size > 1:
- for head in tfSeq(size-1):
- yield head + [True]
- yield head + [False]
- else:
- yield [True]
- yield [False]
- def nth_term(seq, idx):
- return [elem[idx] for elem in seq]
- first_term = lambda seq: nth_term(seq, 0)
- second_term = lambda seq: nth_term(seq, 1)
- def truthtable(vars, funcs, conclusion):
- output = '\\item\n'
- output += '{'
- output += '\n'.join(['$' + var + '$: ' + desc + '\\\\' for var, desc in vars])
- output += '\\(\n'
- output += '\n'.join([func[0] + '\\\\' for func in funcs])
- output += '\\line \\\\ \n'
- output += conclusion[0] + '\n'
- output += '\\)}\n\n'
- output += '\\item\n'
- output += '\\(\n'
- lVars = len(vars)
- lFuncs = len(funcs)
- output += r'\begin{array}[t]{' + '|'.join(['c']*lVars) + '||' + '|'.join(['c']*lFuncs) + '||c}' + '\n'
- labels = first_term(vars) + first_term(funcs) + first_term([conclusion])
- output += '&'.join(labels) + r'\\'
- output += '\n\\hline\n\\hline\n'
- tfText = {True:'T', False:'F'}
- for tf in tfSeq(len(vars)):
- values = [tfText[val] for val in tf]
- values += [tfText[func[1](*tf)] for func in funcs]
- values += [tfText[conclusion[1](*tf)]]
- output += '&'.join(values) + '\\\\\n'
- output += r'\end{array}'
- output += '\n\)\n'
- return output
- # Setup notes
- '''
- Install http://www.ctan.org/pkg/python
- Run latex with --shell-escape 'latex -shell-escape document.tex'
- The first argument in which you declare variables also determines the order that the arguments come to the lambdas in the second argument. Ex, if your first argument is [('A', ''), ('B', ''), ('C', '')], then all of the lambdas should be lambda A, B, C not lambda A, C, B.
- All macros (even unary operators) must accept as arguments all of the variables. Ex, lambda A, B, C: not A is correct while lambda A: not A is incorrect.
- When debugging, consult LATEX_SOURCE.py.out (which holds stdout) and LATEX_SOURCE.py.err (which holds stderr).
- Be sure to define macros \lif (a -> b), and \liff (a <-> b) like so (\newcommand{\lif}{\rightarrow}). If you don't like the default line, you can always redefine them (\renewcommand{\line}{\\[-1.5ex]\rule{\linewidth}{.4pt}}).
- The program expects to be run inside of an \begin{enumerate}...\end{enumerate}
- '''
- # Sample input
- '''
- \begin{enumerate}
- \begin{python}[truth.py]
- print truthtable(
- [('P', 'The patient will die'), ('O', 'We will operate')],
- [('\lnot O \lif P', lambda P, O: not (not O) or P), ('O', lambda P, O: O)],
- ('\lnot P', lambda P, O: not P)
- )
- \end{python}
- \end{enumerate}
- '''
- # Sample output
- '''
- \begin{enumerate}
- \item
- {
- $P$: The patient will die\\
- $O$: We will operate\\
- \(
- \lnot O \lif P \\
- O \\
- \line \\
- \lnot P
- \)
- }
- \item
- \(
- \begin{array}[t]{c|c||c|c||cc}
- P&O&\lnot O \lif P&O&\lnot P\\
- \hline
- \hline
- T&T&T&T&F&*\\
- T&F&T&F&F&\\
- F&T&T&T&T&*\\
- F&F&F&F&T&\\
- \end{array}
- \)
- \end{enumerate}
- '''
Advertisement
Add Comment
Please, Sign In to add comment