Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple ifs to one line solution in Clear category for Sport Watch by BootzenKatzen
def pace_adjust(h1: int, h2: int, s: int) -> int:
too_high = h1 * 3
too_low = h1 * 2
if h2 > too_high or s < 95:
return -1
elif h2 < too_low and s > 97:
return 1
else:
return 0
"""
This one uses simple logic and if statements. If it meets either of the conditions for slowing down, it returns -1
if it meets both conditions for speeding up, it returns 1
otherwise, it returns 0 to maintain speed
"""
def pace_adjust2(h1: int, h2: int, s: int) -> int:
cond_1 = h2 > h1 * 3 or s < 95
cond_2 = h2 < h1 * 2 and s > 97
return [[-1, 1, 0][i] for i in range(0,3) if [cond_1, cond_2, cond_1 == False and cond_2 == False ][i] == True][0]
"""
This one I was trying to work out the logic to make it one line, but to fit the "one line" on one line,
I had to make it 3 lines to shorten the conditions
It's making a list of values from the [-1, 1, 0] list, but only if they meet the conditions
So for the first value in the range - 0, if [cond_1, cond_2, cond_1 == False and cond_2 == False ][0] (cond1)
is true, then it will return [-1, 1, 0][0] (-1) and so forth. So if condition 1 was met,
then our return statement turns out to be [-1][0], so it returns -1
"""
pace_adjust3 = lambda h1, h2, s: [[-1, 1, 0][i] for i in range(0,3) if [
h2 > h1 * 3 or s < 95,
h2 < h1 * 2 and s > 97,
True][i] == True][0]
"""
This is kind of cheating, but technically it is just one really long line, I just broke one of the lists into parts
so it would all fit within the window easily without having to scroll
The only thing that's different from the last one, aside from using lambda to make it one line,
is that instead of saying "condition 1 is false and condition 2 is false" for the last condition,
I just put True
This means our list will always have 0
The trick is, we're returning [list][0]
So if condition 1 or condition 2 are true, they'll come first in the list, and be returned
Otherwise, the list will consist of just the 0 and it will return that
"""
print("Example:")
print(pace_adjust(60, 180, 97))
# These "asserts" are used for self-checking
assert pace_adjust(60, 190, 98) == -1
assert pace_adjust(70, 140, 92) == -1
assert pace_adjust(70, 130, 98) == 1
print("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 4, 2024
Comments: