True Detective loves the truth, seeks the truth night and day. He keeps asking "Is it true that... ?" about everything, even the most profane aspects of the world.
The only thing that makes him happy if he can
grab the truth value of the case being examined
in one, single, crystal clear expression.
For example, whether someone is a queen can be
answered with the expression has_been_crowned and not is_male
.
His assistant, Dr Whatif admires this ability and always tries to reproduce the results of his friend. Unfortunately he lacks the logical skills of True Detective: he is only able to think in a series of simple conditionals (if this then that), and he doesn't know about logical operators or any other higher level constructs. Nevertheless, even if a much more roundabout fashion, he is always able to reproduce the results of True Detective. For example to the above queen question Dr Whatif would write something like this:
if is_male:
return False
else:
if has_been_crowned:
return True
else:
return False
Which mindset is closer to you?
To check this you need to solve cases (implement functions)
both ways. Write two implementations
for each required function, one in truedetective.py
and one in whatif.py
.
Tests are included with many expected answers in the repository. Keep running them to check your solutions!
You will learn and practice the following topics:
- writing conditionals,
- using logical operators,
- simplifying boolean expressions
- running tests.
-
Is a given positive integer an odd number with two digits? Implement
is_twodigit_odd
in two versions!- Dr Whatif's solution has no logical operators and passes all the tests.
- True Detective's solution is a one-liner and passes all the tests.
-
Does a user have write access to a unix file? The parameters are
user
,users_groups
,file_owner
,writable_by_owner
,file_group
,writable_by_group
,writable_by_others
,sudo_mode
. Implementhas_access
in two versions!- Dr Whatif's solution has no logical operators and passes all the tests.
- True Detective's solution is a one-liner and passes all the tests.
-
Is a given year (AD) a leap year in the Gregorian calendar? The official definition is the following: "Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400." Implement
is_leap_year
in two versions!- Dr Whatif's solution has no logical operators and passes all the tests.
- True Detective's solution is a one-liner and passes all the tests.
-
Is a given day of a month Sunday? We also know the first day of the month (one of
('M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su')
. Suppose a 31-day-long month, so days other than1..31
cannot be Sundays. Implementis_sunday
in two versions!- Dr Whatif's solution has no logical operators and passes all the tests.
- True Detective's solution is a one-liner and passes all the tests.
-
Should True Detective bring an umbrella when going out? Well, it depends. Obviously he should when it rains, unless the wind is too strong. Wind strength greater than or equal to 7 (on the Beaufort scale) would turn the umbrella inside out. Taking the umbrella is also reasonable when it's not raining but it is cloudy, the wind is not too strong, and there are signs of a rain coming. Such signs are: red sky during sunset // flowers smell stronger than normal // spiders come down from their webs // cattle lie down in a group in the fields. But True Detective also likes to take his umbrella when the sun is shining strongly. Again, unless the wind situation is not too stormy. Implement
should_bring_umbrella
in two versions!- Dr Whatif's solution has no logical operators and passes all the tests.
- True Detective's solution is a one-liner and passes all the tests.
-
[OPTIONAL] Should one take a nap? Follow this decision tree. Implement
should_take_a_nap
in two versions!- Dr Whatif's solution has no logical operators and passes all the tests.
- True Detective's solution is a one-liner and passes all the tests.
None
- There are two test files included that use the
pytest
module. Check it out first how to runpytest
. - Write Dr Whatif's version first, that should help building the denser solution. You might even find a way to do the conversion systematically.
- Usually there are multiple equivalent solutions both for Dr Whatif and for True Detective. Try to come up with alternative solutions! After finishing the project, compare others' good solutions with yours!
has_access
function requires lots of parameters. You can find a short description for each parameter below:user
: integer type represents the ID of the userusers_groups
: list type represents the IDs of each user group has write access to the filefile_owner
: integer type represents the ID of the file ownerwritable_by_owner
: boolean type represents the file is writable by ownerfile_group
: integer type represents the ID of the group has write access to the filewritable_by_group
: boolean type represents the file is writable by groupwritable_by_others
: boolean type represents the file is writable by otherssudo_mode
: boolean type represents the sudo mode is activated
Follow this link to create your project repository.
You will need this only at review time, after completing the project. Use this form to record the review you provide for your peer.
For now, you can check your (and everyone else's) results in the summary spreadsheet.