5 kyu

Three-valued logic

68 of 88pea18013

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!

Interpreters
Logic

More By Author:

Check out these other kata created by pea18013

Stats:

CreatedNov 17, 2023
PublishedNov 23, 2023
Warriors Trained401
Total Skips7
Total Code Submissions1068
Total Times Completed88
Python Completions68
Haskell Completions11
JavaScript Completions16
Total Stars25
% of votes with a positive feedback rating96% of 28
Total "Very Satisfied" Votes27
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes1
Total Rank Assessments9
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • pea18013 Avatar
  • JohanWiltink Avatar
  • dfhwze Avatar
  • Mednoob Avatar
Ad