Skip to content

Commit

Permalink
Replace one of our bad exercises with a better one ;P (#127)
Browse files Browse the repository at this point in the history
* Replace one of our bad exercises with a better one ;P

* Add another sub-exercise

* Apply suggestions from code review

Co-authored-by: Hana Harencarova <[email protected]>

---------

Co-authored-by: Ferdinand Niedermann <[email protected]>
Co-authored-by: Hana Harencarova <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent 487dfec commit e497c5f
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions exercises/boolean-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,47 @@ The following exercises might be a bit dry, but the concept you'll be practising

You can tell what the result will be without knowing the value of `x`. Why?

3. Write methods for the boolean operators `||`, `&&` and `!`. Here are the method signatures, just fill in the bodies:
3. Imagine you are developing an application that controls a cat door. There are 3 cats that you need to accommodate, Alpha, Bravo and Charlie.

a) Alpha is hurt and therefore shouldn't leave the house at the moment. Complete the following code so that Alpha can't leave the house:

```ruby
def my_not(a)
def can_alpha_open_door?
# your code goes here
end
```

def my_and(a, b)
b) Bravo likes to run away and sometimes does not come back for days if he is out between 20:00 and 06:00. Therefore, we want to lock the door to him if it's this late. Otherwise he can leave the house.
```ruby
def can_bravo_open_door?(hour_of_day)
# your code goes here
end
```
c) Charlie is a bit more complicated. Charlie should only be allowed out if both Alpha is inside (since they don't get along), and if Bravo is already inside (because they like to chase each other). Complete the following code to let Charlie out just if the two other cats are already inside:

def my_or(a, b)
```ruby
def can_charlie_open_door?(is_alpha_inside, is_bravo_inside)
# your code goes here
end
```

You can test your implementation by running
d) Another rule the system needs to work with is the holiday/winter mode. In case it is winter or the owners are on holidays, no cats are allowed to go outside.

```ruby
def is_any_cat_allowed_to_go_outside?(owners_are_on_holidays, season)
# your code goes here
end
```

`puts my_not(my_and(my_or(true, false), my_or(false, true)))`
e) Bonus: Safety Mode. In some cases, we need to lock the door for all cats when the outside temperature is too low (below 5°C). Complete the code for checking if the door should be locked for all cats.

Your program should output `false` as a result.
```ruby
def is_door_locked_for_all?(temperature)
# your code goes here
end
```

4. The following is a so-called _truth table_. For more examples of truth tables, refer to this session's cheat sheet.
Expand Down

0 comments on commit e497c5f

Please sign in to comment.