Boolean values are those which have a value of true or false. (cf. “4.1. Data types” and “Boolean operators and comparison operators”.) Given two boolean values, we can perform and/or operations or conjunction/disjunction operations:
Write a program that reads several and/or conjunction/disjunction operations and prints their result.
Input will consist of several lines each containing
a boolean p
, an operator x
and a boolean q
separated by a single space.
Booleans p
and q
are either of the strings true
or false
and the operator x
is either of the strings and
or or
.
For each line of input,
there should be a line of output
containing a line with the result of the given input operation.
Example input
true and false
false or true
Example output
false
true
conjunction
and disjunction
functionsThe submitted program should contain the conjunction
and disjunction
functions
that receive two boolean as arguments and return a boolean.
Please refer to the information for the chosen language:
Python definitions:
def conjunction(p, q):
and
def disjunction(p, q):
C prototypes:
int conjunction(int p, int q);
and
int disjunction(int p, int q);
Haskell type:
conjunction, disjunction :: Bool -> Bool -> Bool
C++ prototypes:
bool conjunction(bool p, bool q);
and
bool disjunction(bool p, bool q);
(see hint below)
C# definitions:
public static bool Conjunction(bool p, bool q)
and
public static bool Disjunction(bool p, bool q)
in a public class Program
.
Java definitions:
public static boolean conjunction(boolean p, boolean q)
and
public static boolean disjunction(boolean p, boolean q)
in a public class BoolAndOr
.
JavaScript definitions:
function conjunction(p,q)
and
function disjunction(p,q)
Lua definitions:
function conjunction (p, q)
and
function disjunction (p, q)
Ruby definitions:
def conjunction(p, q)
and
def disjunction(p, q)
Comparing strings to arrive at a bool. You can use string equality to create a boolean value from strings.
In Python and Haskell,
you can compare strings s1
and s2
using the equality operator:
s1 == s2
This operator also works with literal strings:
str == "hello"
In C, you can compare strings using the strcmp
function:
strcmp(s1, s2) == 0
When the result of strcmp
is 0 that means the given strings are equal.
The strcmp
function also works with literal strings:
strcmp(str, "hello") == 0
What are booleans? Read both “4.1. Data types” and “Boolean operators and comparison operators” sections of the Computer Science by Example book for an explanation of what are booleans and how to use them in programming.
Beware of the data types.
Users of Python and dinamically-typed languages beware.
The conjunction
and disjunction
functions will be tested
with proper boolean values as their arguments
and not with booleans encoded in strings.
This is similar to how the add
function
of earlier exercises add1 and add
does take proper integer values
and not integer values encoded as strings.
In Python,
there is a distinction between the boolean value False
and the string 'False'
.
Confusingly enough, in Python
'False'
has a boolean value of True
:
>>> bool('False')
True
This comes from the fact that in Python,
boolean conversion from strings defaults to treating
empty strings as False
-valued and non-empty strings as True
-valued.
This similar to the distinction between
the integer 12
and the string "12"
.
C++ namespace conflicts.
If you are facing namespace conflicts locally, read on.
Starting with C++17,
the std
namespace does export two names called conjunction
and disjunction
.
In this exercise,
you should avoid using namespace std;
and import required std
symbols one by one:
using std::cin;
using std::cout;
Or, you can simply prefix all uses with std::
, e.g.: std::cin
and std::cout
.
Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/bool-and-or