index | submit | rank | book

bool-not – Boolean negation

Boolean values are those which have a value of true or false. (cf. “4.1. Data types” and “Boolean operators and comparison operators”.) One of the operations we can perform on booleans is negation: that which is not true is false and that which is not false is true; in other words, the negation of true is false and the negation of false is true.

Boolean negation represented in three different ways

Write a program that reads several boolean values and prints their negation.

Input and output

Input will consist of several lines containing either true or false. For each line of input, there should be a line of output with the negation of the corresponding input value: either true or false.

Example input

true
false

Example output

false
true

The negation function

The submitted program should contain a negation function that receives one boolean as argument and returns a boolean. Please refer to the information for the chosen language:

Scoring

Hints

  1. 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
    
  2. 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.

  3. Beware of the data types. Users of Python and dinamically-typed languages beware. The negation function will be tested with proper boolean values as its argument and not with booleans encoded in strings. This is similar to how the triple and inc functions of earlier exercises triple1, inc1, triple and inc do 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".

  4. C++ namespace conflicts. If you are facing namespace conflicts locally, read on. Starting with C++17, the std namespace does export a name called negation. 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.

try first: hi base2base order

try next: bool-and-or

index | submit | rank | book

Copyright © 2020-2023 Rudy Matela
All rights reserved