Three-valued logic
Description:
Background
Three-valued logic is a multiple-valued logic system in which there are three truth values; for this kata they will be True
, False
and Unknown
. In this kata, T
stands for True
, F
stands for False
and U
stands for Unknown
. They are also represented as 1
, -1
and 0
respectively.
This is the basic truth table:
*---+---+-------+---------+--------+---------*
| P | Q | not P | P and Q | P or Q | P xor Q |
| T | T | F | T | T | F |
| T | U | F | U | T | U |
| T | F | F | F | T | T |
| U | T | U | U | T | U |
| U | U | U | U | U | U |
| U | F | U | F | U | U |
| F | T | T | F | T | T |
| F | U | T | F | U | U |
| F | F | T | F | F | F |
*---+---+-------+---------+--------+---------*
Commutativity holds in this three-valued logic.
For example:
T and U is equal to U and T.
In general:
p and q is equal to q and p.
Moreover, for identical operators, associativity holds in this three-valued logic.
For example:
(U or T) or F is equal to U or (T or F).
((T xor F) xor U) xor T is equal to
(T xor (F xor U)) xor T,
T xor ((F xor U) xor T),
T xor (F xor (U xor T)),
(T xor F) xor (U xor T).
Note that the law of the excluded middle and the law of non-contradiction do not hold in this three-valued logic.
For example:
U and not U is not equal to F.
not U or U is not equal to T.
Read more about three-valued logic on Wikipedia.
Task
Write a function threevl
that accepts an expression ( as a string ) and returns its value ( as 1
, 0
, or -1
).
For example:
"not T or U" -> 0
"not (T or U)" -> -1
"U and not U" -> 0
"not (F and (U xor T))" -> 1
Notes
- Always evaluate expressions inside brackets first.
- This kata only uses 4 operators
not, and, xor, or
. - Their priorities, from highest to lowest, are:
not, and, xor, or
. Hence,not T or F xor U
should equal(not T) or (F xor U)
.
Input
0
<= the number of operators <59
- There will be no invalid inputs.
There are 300
random tests. Good luck and happy coding!
Similar Kata:
Stats:
Created | Nov 17, 2023 |
Published | Nov 23, 2023 |
Warriors Trained | 401 |
Total Skips | 7 |
Total Code Submissions | 1068 |
Total Times Completed | 88 |
Python Completions | 68 |
Haskell Completions | 11 |
JavaScript Completions | 16 |
Total Stars | 25 |
% of votes with a positive feedback rating | 96% of 28 |
Total "Very Satisfied" Votes | 27 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 9 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |