Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
In the evening solution in Creative category for Funny Addition by tom-tom
def checkio(data):
"""The sum of two integer elements"""
if data == [5, 5]: # that was in the tests
return 10 # ... and we know the answer
if data == [7, 1]: # that was in those tests too
return 8 # ... easy
if data == [5, 5]: # try it again, maybe we missed something the first time...
return 10
# OK, we have some work to do
# let's try to swap elements:
if [data[1], data[0]] == [5, 5]:
return 10 # Goodbye!
# Huh! That wasn't the Pythonic way to swap, let's do it like this:
if data[::-1] == [5, 5]:
return 10 # Goodbye forever!
# Alas! I need another way to swap.
# Aha! Lists have some convenient methods I never used before:
data.reverse()
if data == [5, 5]:
return 10
# Oh, no! Here again!
# I have to ask someone
# 2 hours later:
# They told me I should use '+'
if [data[1]] + [data[0]] == [5, 5]:
return 10
# No wonder, my code was better... But they insisted!
# I'm exhausted, next morning I'll try lambda-s and list comprehensions...
# AFK
# Hi! I'm Alice, the wife of this idiot
return sum(data)
# Good morning! How are you?
# What was that?
# Solved. If only I could find the right way to swap...
if __name__ == '__main__':
assert checkio([5, 5]) == 10, 'First'
assert checkio([7, 1]) == 8, 'Second'
print('All ok')
Oct. 8, 2016
Comments: