diff --git a/lab2/lab2.md b/lab2/lab2.md new file mode 100644 index 0000000..753c1dc --- /dev/null +++ b/lab2/lab2.md @@ -0,0 +1,68 @@ +# CS 278 Lab 2 +- Programmed in Python3 + +## How to Run on CS Machines + + +``` +cd +python3 lab2.py +``` + + +## Last Output + +``` +/home/ugrad22/zarafat/PycharmProjects/cs278lab2/venv/bin/python /home/ugrad22/zarafat/PycharmProjects/cs278lab2/lab2.py +Proposition 1 +p q r (~p → q) and (r → p) +F F F +F F T +F T F +F T T +T F F +T F T +T T F +T T T +Truths: 5 +Falsehoods: 3 +The proposition is Neither a Tautology or a Contradiction + ____________________ +Proposition 2 +p q r (p or (~q)) and (r or ~(p → q)) +F F F +F F T +F T F +F T T +T F F +T F T +T T F +T T T +Truths: 4 +Falsehoods: 4 +The proposition is Neither a Tautology or a Contradiction + ____________________ +Proposition 3 +p q (p or (~q)) and (r or ~(p → q)) +F F +F T +T F +T T +Truths: 4 +Falsehoods: 0 +The proposition is a Tautology + ____________________ +Proposition 4 +p q (p or (~q)) and (r or ~(p → q)) +F F +F T +T F +T T +Truths: 0 +Falsehoods: 4 +The proposition is a Contradiction + ____________________ + +Process finished with exit code 0 + +``` \ No newline at end of file diff --git a/lab2/lab2.py b/lab2/lab2.py new file mode 100644 index 0000000..d9cacf0 --- /dev/null +++ b/lab2/lab2.py @@ -0,0 +1,103 @@ +def truth_gen(vars_count): + """ + Generate a 2D list of truth values for N variables + :param vars_count: Number of variables in the proposition + :return: A 2d list of all the possible values for the variables + """ + rows = 2 ** vars_count + table = [] + bin_to_str = { + '0': 'F', + '1': 'T', + ' ': 'F' + } + for i in range(rows): + bins = "{0:>{1}}".format("{0:b}".format(i), vars_count) + table.append([]) + for j, val in enumerate(bins): + table[i].append(bin_to_str[val]) + return table + + +# Logical operators +def _not(i): + return 'T' if i is 'F' else 'F' + + +def _or(i, j): + return 'T' if i is 'T' or j is 'T' else 'F' + + +def _and(i, j): + return 'T' if i is 'T' and j is 'T' else 'F' + + +def impl(i, j): + return 'F' if i is 'T' and j is 'F' else 'T' + + +# Propositions +def prop1(args=None): + if args is None: + args = [0, 0, 0] + p, q, r = tuple(args) + return _and((impl(_not(p), q)), (impl(r, p))), "(~p → q) and (r → p)" + + +def prop2(args=None): + if args is None: + args = [0, 0, 0] + p, q, r = tuple(args) + return _and((_or(p, _not(q))), + (_or(r, _not(impl(p, q))))), "(p or (~q)) and (r or ~(p → q))" + + +def prop3(args=None): + if args is None: + args = [0, 0] + p, q = tuple(args) + return impl(p, (impl((_not(_and(p, _not(q)))), + (_and(p, q))))), "(p or (~q)) and (r or ~(p → q))" + + +def prop4(args=None): + if args is None: + args = [0, 0] + p, q = tuple(args) + return _and((_and(p, (impl(p, q)))), + (_not(q))), "(p or (~q)) and (r or ~(p → q))" + + +def apply_prop(prop_function, vars_count): + """ + Function to apply proposition functions to a truth table + :param prop_function: a proposition function to apply + :param vars_count: the number of variables for the proposition + """ + args = [truth_gen(vars_count)] + t_count = 0 + prop_type = "" + print("p", "q", "r" if vars_count >= 3 else "", prop_function()[1]) + for i in truth_gen(vars_count): + print(*i[0:], "{0:>{1}}".format(prop_function(i[:])[0], + len(prop_function()[1]) / 2)) + if prop_function(i[:])[0] is 'T': + t_count += 1 + f_count = 2 ** vars_count - t_count + if t_count == 2 ** vars_count: + prop_type = "a tautology" + elif t_count == 0: + prop_type = "a contradiction" + else: + prop_type = "neither a Tautology nor a Contradiction" + + print("Truths:", t_count, "\n", "\rFalsehoods:", f_count, "\n", + "\rThe proposition is", prop_type, "\n", "_"*20) + + +# Main code to run all the propositions. +props = [prop1, prop2, prop3, prop4] + +for key, prop in enumerate(props): + print("Proposition", key + 1) + apply_prop(prop, (3 if key <= 1 else 2)) diff --git a/lab2/lab3.md b/lab2/lab3.md new file mode 100644 index 0000000..e69de29 diff --git a/lab2/lab3.py b/lab2/lab3.py new file mode 100644 index 0000000..e69de29 diff --git a/lab2/test b/lab2/test deleted file mode 100755 index 4af5b5e..0000000 Binary files a/lab2/test and /dev/null differ diff --git a/lab2/test.c b/lab2/test.c deleted file mode 100644 index e2edb5b..0000000 --- a/lab2/test.c +++ /dev/null @@ -1,115 +0,0 @@ -#include - -// init functions -char not(char var); -char or(char l, char r); -char and(char l, char r); -char implies(char l, char r); -char prop1(char p, char q, char r); -char prop2(char p, char q, char r); -char prop3(char p, char q, char r); -char prop4(char p, char q, char r); -/* -* Another function, but this includes the body of -* the function. This is a "definition". -*/ - -int main(){ - char truth[8][8]; - char tick = 'F'; - // generate all possible truth combinations - for(int i = 0; i < 8; i++){ - truth[i][0] = tick; - if((i+1) % 4 == 0 && i != 0){ - tick = not(tick); - } - } - tick = 'F'; - for(int i = 0; i < 8; i++) { - truth[i][1] = tick; - if (i % 2 != 0&&i != 0){ - tick = not(tick); - } - - } - tick = 'F'; - for(int i = 0; i < 8; i++) { - truth[i][2] = tick; - tick = not(tick); - - } - - // apply all 4 propositions - for(int i = 0; i < 8; i++){ - truth[i][3] = prop1(truth[i][0], truth[i][1], truth[i][2]); - } - for(int i = 0; i < 8; i++){ - truth[i][4] = prop2(truth[i][0], truth[i][1], truth[i][2]); - } - for(int i = 0; i < 8; i++){ - truth[i][5] = prop2(truth[i][0], truth[i][1], truth[i][2]); - } - for(int i = 0; i < 8; i++){ - truth[i][6] = prop3(truth[i][0], truth[i][1], truth[i][2]); - } - for(int i = 0; i < 8; i++){ - truth[i][7] = prop4(truth[i][0], truth[i][1], truth[i][2]); - } - - - - // print out array -// printf("%c\t"); - for(int i = 0; i<8; i++){ - printf("\n"); - for(int j = 0; j<(7); j++){ - printf("%c\t", truth[i][j]); - } - } - printf("\n"); - return(0); -} - -// logic functions -char not(char var){ - if(var == 'T'){ - return ('F'); - }else{ - return ('T'); - } -} -char or(char l, char r){ - if (l == 'T'|| r == 'T'){ - return ('T'); - }else{ - return ('F'); - } -} -char and(char l, char r){ - if (l == 'T' && r == 'T'){ - return ('T'); - }else{ - return ('F'); - } -} -char implies(char l, char r){ - if ((l != 'T') || (l == 'T' && r == 'T')){ - return ('T'); - }else{ - return('F'); - } -} - -// propositions 1 to 4 -char prop1(char p, char q, char r){ - return and(implies(not(p), q), implies(r, p)); -} -char prop2(char p, char q, char r){ - return and(or(p, not(q)), or(r, not(implies(p,q)))); -} -char prop3(char p, char q, char r) { - return implies(implies(p,not(and(p, not(q)))), and(p, q)); -} -char prop4(char p, char q, char r) { - return and(and(p, implies(p,q)), not(q)); -} diff --git a/lab2/test_old b/lab2/test_old deleted file mode 100755 index 82ad71b..0000000 Binary files a/lab2/test_old and /dev/null differ diff --git a/lab2/test_old.c b/lab2/test_old.c deleted file mode 100644 index 386a5b1..0000000 --- a/lab2/test_old.c +++ /dev/null @@ -1,141 +0,0 @@ -#include -#include - -/* -* Tell the compiler that we intend -* to use a function called show_message. -* It has no arguments and returns no value -* This is the "declaration". -* -*/ - -void show_message(void); -bool not(bool var); -bool or(bool l, bool r); -bool and(bool l, bool r); -bool implies(bool l, bool r); -bool prop1(bool p, bool q, bool r); -bool prop2(bool p, bool q, bool r); -bool prop3(bool p, bool q, bool r); -bool prop4(bool p, bool q, bool r); -/* -* Another function, but this includes the body of -* the function. This is a "definition". -*/ - -int main(){ - not(false); - or(false, true); - and(true, true); - implies(true, false); - bool truth[8][3+4]; - // generate p - for(int i = 0; i < 8; i++){ - if(i < 4){ - truth[i][0] = false; - }else{ - truth[i][0] = true; - } - } - for(int i = 0; i < 8; i++){ - printf(truth[i][0] ? "true\n" : "false\n"); - - } - printf("new\n"); - bool tick = false; - for(int i = 0; i < 8; i++) { - truth[i][1] = tick; - if (i % 2 != 0&&i != 0){ - tick = !tick; - } - - } - tick = false; - for(int i = 0; i < 8; i++) { - truth[i][2] = tick; - tick = !tick; - - } - - for(int i = 0; i < 8; i++){ - printf(truth[i][1] ? "true\n" : "false\n"); - - } - for(int i = 0; i < 8; i++){ - truth[i][3] = prop1(truth[i][0], truth[i][1], truth[i][2]); - printf(prop1(truth[i][0], truth[i][1], truth[i][2]) ? "atrue\n" : "afalse\n"); - } - for(int i = 0; i < 8; i++){ - truth[i][4] = prop2(truth[i][0], truth[i][1], truth[i][2]); - printf(prop1(truth[i][0], truth[i][1], truth[i][2]) ? "atrue\n" : "afalse\n"); - } - for(int i = 0; i < 8; i++){ - truth[i][5] = prop2(truth[i][0], truth[i][1], truth[i][2]); - printf(prop1(truth[i][0], truth[i][1], truth[i][2]) ? "atrue\n" : "afalse\n"); - } - for(int i = 0; i < 8; i++){ - truth[i][6] = prop3(truth[i][0], truth[i][1], truth[i][2]); - printf(prop1(truth[i][0], truth[i][1], truth[i][2]) ? "atrue\n" : "afalse\n"); - } - for(int i = 0; i < 8; i++){ - truth[i][7] = prop4(truth[i][0], truth[i][1], truth[i][2]); - printf(prop1(truth[i][0], truth[i][1], truth[i][2]) ? "atrue\n" : "afalse\n"); - } - - - - - for(int i = 0; i<8; i++){ - printf("\n"); - for(int j = 0; j<(3+4); j++){ - printf(truth[i][j] ? "T\t" : "F\t"); - } - } - return(0); -} - -/* -* The body of the simple function. -* This is now a "definition". -*/ - -bool not(bool var){ - if(var){ - return (false); - }else{ - return (true); - } -} -bool or(bool l, bool r){ - if(l||r){ - return (true); - }else{ - return (false); - } -} -bool and(bool l, bool r){ - if(l&&r){ - return (true); - }else{ - return (false); - } -} -bool implies(bool l, bool r){ - if( l<=r){ - return (true); - }else{ - return(false); - } -} -bool prop1(bool p, bool q, bool r){ - return and(implies(not(p), q), implies(r, p)); -} -bool prop2(bool p, bool q, bool r){ - return and(or(p, not(q)), or(r, not(implies(p,q)))); -} -bool prop3(bool p, bool q, bool r) { - return implies(implies(p,not(and(p, not(q)))), and(p, q)); -} -bool prop4(bool p, bool q, bool r) { - return and(and(p, implies(p,q)), not(q)); -}