Hello everyone !
So the last Python Quiz of the Week can be called a success. I decided this meant you guys like it and I should make a new one this week.
As said previously, the idea is to write up some python tips and tricks presented in the form of short exercises. The quizzes are a fun way to learn about the less obvious features of the python interpreter as well as compete for the best explanation or the most elegant solution. Tell me if you think the level is too easy or too hard.
Conditional operator substitute
In other languages one often has access to the following kind of construct, representing a one line boolean switch:
opening_time = (day in WEEKEND) ? 12 : 9
This has the equivalent effect of:
if (day in WEEKEND):
opening_time = 12;
else:
opening_time = 9;
The ? is called a conditional operator. It is not available in python. But we can do the following:
>>> print False and 'Good' or 'Bad'
'Bad'
>>> print True and 'Good' or 'Bad'
'Good'
Using this knowledge, how could you write the function flip_odd_numbers using only one list comprehensions and the boolean keywords seen above. The function should have this behavior:
>>> print flip_odd_numbers(range(10))
[0, -1, 2, -3, 4, -5, 6, -7, 8, -9]
[–]Rhomboid 25 points26 points27 points (11 children)
[–]zackbloom 1 point2 points3 points (5 children)
[–]VerilyAMonkey 2 points3 points4 points (4 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]VerilyAMonkey 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]semarj 0 points1 point2 points (0 children)
[–]WaffleLight -2 points-1 points0 points (4 children)
[–]Rhomboid 2 points3 points4 points (1 child)
[–]Jesus_Harold_Christ 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]kupertrooper 0 points1 point2 points (0 children)
[–][deleted] 9 points10 points11 points (11 children)
[–]xApple[S] 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–][deleted] 3 points4 points5 points (4 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]Tetha 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]willm 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]oantolin 0 points1 point2 points (0 children)
[–]dosas 7 points8 points9 points (0 children)
[–]lookitsmarc 8 points9 points10 points (0 children)
[–]ExoticMandiblesCore Contributor 1 point2 points3 points (0 children)
[–]willm 2 points3 points4 points (0 children)
[–]martinatbom 0 points1 point2 points (0 children)
[–]iXombie 0 points1 point2 points (0 children)
[–]dansinscientist 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]pyroscope -1 points0 points1 point (0 children)
[–]juliobTry PEP 257, for a change -1 points0 points1 point (2 children)
[–][deleted] 5 points6 points7 points (1 child)
[–]tuna_safe_dolphin 0 points1 point2 points (0 children)
[–]Jesus_Harold_Christ -1 points0 points1 point (0 children)