Предизвикателства > Полиморфичен негативизъм > Решения > Решението на Атанас Ников

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

5 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1def contra_dict(expression):
 2    """
 3    Return the appropriate method based on the argument's type,
 4    using a dictionary as an alternative to an if/elif block, the name is pun intended
 5    """
 6    return { bool: the_other_boolean_around, int: the_other_number_around, float: the_other_number_around, str: the_other_string_around }.get(type(expression))(expression)
 7
 8
 9def the_other_boolean_around(expression):
10    """Negate a boolean expression"""
11    return not expression
12
13
14def the_other_number_around(expression):
15    """Negate a number (an int or a float)"""
16    return -expression;
17
18
19def the_other_string_around(expression):
20    """Return a string in reverse order"""
21    return expression[::-1]
22
23
24def no_it_isnt(expressions):
25    """Return a list of the negated expressions in reverse order"""
26    return [contra_dict(expression) for expression in expressions[::-1]]

.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s

OK

Дискусия
История

f1def contra_dict(expression):f1def contra_dict(expression):
nn2    """
2    """Return the appropriate method based on the argument's type,3    Return the appropriate method based on the argument's type,
3     using a dictionary as an alternative to an if/elif block, the name is pun intended"""4    using a dictionary as an alternative to an if/elif block, the name is pun intended
5    """
4    return { bool: the_other_boolean_around, int: the_other_number_around, float: the_other_number_around, str: the_other_string_around }.get(type(expression))(expression)6    return { bool: the_other_boolean_around, int: the_other_number_around, float: the_other_number_around, str: the_other_string_around }.get(type(expression))(expression)
57
68
7def the_other_boolean_around(expression):9def the_other_boolean_around(expression):
8    """Negate a boolean expression"""10    """Negate a boolean expression"""
9    return not expression11    return not expression
1012
1113
12def the_other_number_around(expression):14def the_other_number_around(expression):
13    """Negate a number (an int or a float)"""15    """Negate a number (an int or a float)"""
14    return -expression;16    return -expression;
1517
1618
17def the_other_string_around(expression):19def the_other_string_around(expression):
18    """Return a string in reverse order"""20    """Return a string in reverse order"""
19    return expression[::-1]21    return expression[::-1]
2022
2123
22def no_it_isnt(expressions):24def no_it_isnt(expressions):
23    """Return a list of the negated expressions in reverse order"""25    """Return a list of the negated expressions in reverse order"""
24    return [contra_dict(expression) for expression in expressions[::-1]]26    return [contra_dict(expression) for expression in expressions[::-1]]
t25 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op