13
If you remember back to your schooling years, you might remember learning about Truth Tables. They seemed boring, but they are basis for logic and (some would argue) all computing...
Problem
Your mission, should you choose to accept it is to write a program, function, or widget of code that can output a truth table given input.
Input
Input will be a string (like data structure) containing the logic statement to make the Truth Table into. For example:
p ∧ q
This means p and q
(logical conjuction) and will output:
p q p ∧ q
T T T
T F F
F T F
F F F
Notice the spacing: The item of the column is in the center of the header
Characters
Score via characters, not bytes The logic comparison characters are special and not always what they look like. Use these characters:
Logical Conjunction (AND): ∧
U+2227
Logical Disjunction (OR): ∨
U+2228
Logical Negation (NOT) ~
or ¬
U+7e and U+ac respectively
Bonuses
All of these bonuses are optional, but will knock points off your score. Pick any.
Logical Negation
Logical Negation is a unary operator in truth tables. It is the equivalent of !
in most C-based languages. It makes false
=>true
and vise versa. It is notated with a ¬
or ~
(you must support both). Supporting this will knock off 10% of your score. You must, however, add an additional column to show its results:
For example:
~p ∧ q
will output:
p ~p q ~p ∧ q
T F T F
T F F F
F T T T
F T F F
Pretty Print
The normal table notation is boring. Let's make it pretty! Pretty print format is as follows for p ∧ q
is as follows:
+---+---+-------+
| p | q | p ∧ q |
+---+---+-------+
| T | T | T |
+---+---+-------+
| T | F | F |
+---+---+-------+
| F | T | F |
+---+---+-------+
| F | F | F |
+---+---+-------+
Special details for pretty printing:
- There is a 1 space padding in each cell
- Cell values are still centered
If you pretty print your tables, from your code and then multiply by 0.6. Use this function for this bonus:
score = 0.6 * code
Examples
p ∧ q
:
p q p ∧ q
T T T
T F F
F T F
F F F
p ∨ q
:
p q p ∨ q
T T T
T F T
F T T
F F F
~p ∧ q
:
p ~p q ~p ∧ q
T F T F
T F F F
F T T T
F T F F
~p ∨ q
:
p ~p q ~p ∧ q
T F T T
T F F F
F T T T
F T F T
Rules
- Standard loopholes apply
- No external resources
- If you're going to break the rules, be clever ;)
Shortest Code (in characters) wins. Good Luck!
4From the description it sounded like these were arbitrary Boolean expressions. But all the examples (without bonus) have only one operator. Is this restricted to a single operator? Also, the names of the values in the examples are all
p
andq
. Unless they always have these names, you may want to show a few different options in the test examples. Are they always a single letter? – Reto Koradi – 2015-09-18T03:44:40.8932Since this uses non-ASCII characters, it might be good to specify if the length of the code is counted in characters or bytes. If it's bytes, it would be helpful to know how many bytes the unicode characters use. – Reto Koradi – 2015-09-18T03:51:38.147
Simplify :).
score = 0.6 * (code - 15)
=.6 * code - 9
– mınxomaτ – 2015-09-18T07:14:01.653@RetoKoradi Changed. Score by characters, not bytes – MayorMonty – 2015-09-19T04:35:19.483
@RetoKoradi If what my geometry teacher tells me is correct you will never see more then
p
q
andr
in a truth table ;) – MayorMonty – 2015-09-19T04:36:12.597What?! No way! I was just thinking of making a challenge like this! – mbomb007 – 2015-09-19T05:15:46.627
@SpeedyNinja You can easily see more than those in a truth table. You might not see it in a class, but that's like saying that you'll never see an equation with more than three variables. – mbomb007 – 2015-09-19T05:19:13.413
@mbomb007 For the purposes of keeping a moderately difficult challenge simple, if an answer doesn't support more then
p
q
orr
that will be fine – MayorMonty – 2015-09-19T05:22:38.387I would like to state for the record that my great-grandfather's computer was a slide rule. I doubt that anyone's great-grandfather computed with truth tables: the jump was directly from slide rules to electronic computers. – Peter Taylor – 2016-01-09T17:19:58.873
May we pretty-print using box-drawing chars
┌─┬
? – Adám – 2016-05-31T06:06:47.657Does a symbolic expression count as a string-like data structure; i.e. can I take my input as
P∨Q
or does it have to be"P∨Q"
? – ngenisis – 2017-01-11T01:00:01.183May we take just the Boolean operator (∧ ∨ ~) instead of the full "p ∧ q" string? – Adám – 2017-01-11T09:41:07.193
@Adám String parsing is not the challenge, you may take the operator as input – MayorMonty – 2017-01-11T17:33:52.150
Can we assume that 'p', 'q', etc. is just one character long? – 0WJYxW9FMN – 2017-01-11T20:44:16.667
Is each boolean (like p and q) necessarily in alphabetical order in the table? – 0WJYxW9FMN – 2017-01-11T20:48:36.673