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

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

1 точки общо

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

 1def check_type(x, type_of_x):
 2    return type(x) == type_of_x
 3
 4
 5def no_it_isnt(list_of_vars):
 6    reversed_list = []
 7    for var in list_of_vars:
 8        reversed_var = None
 9        if check_type(var, str):
10            reversed_var = var[::-1]
11        elif check_type(var, int) or check_type(var, float):
12            reversed_var = -var
13        elif check_type(var, bool):
14            reversed_var = True if False else False
15        else:
16            reversed_var = var
17        reversed_list.append(reversed_var)
18
19    return reversed_list[::-1]

F..F.
======================================================================
FAIL: test_bool (test.TestOpposite)
Test with a bool.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 23, in test_bool
self.assertEqual(no_it_isnt([True, False]), [True, False])
AssertionError: Lists differ: [False, False] != [True, False]

First differing element 0:
False
True

- [False, False]
+ [True, False]

======================================================================
FAIL: test_mixed (test.TestOpposite)
Test with a long mixed content of types.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 34, in test_mixed
self.assertEqual(no_it_isnt([True, True, False]), [True, False, False])
AssertionError: Lists differ: [False, False, False] != [True, False, False]

First differing element 0:
False
True

- [False, False, False]
+ [True, False, False]

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=2)

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

nn1def check_type(x, type_of_x):
2    return type(x) == type_of_x
3 
4 
1def no_it_isnt(list_of_vars):5def no_it_isnt(list_of_vars):
2    reversed_list = []6    reversed_list = []
3    for var in list_of_vars:7    for var in list_of_vars:
4        reversed_var = None8        reversed_var = None
n5        if type(var) == str:n9        if check_type(var, str):
6            reversed_var = var[::-1]10            reversed_var = var[::-1]
n7        elif type(var) == int or type(var) == float:n11        elif check_type(var, int) or check_type(var, float):
8            reversed_var = -var12            reversed_var = -var
t9        elif type(var) == bool:t13        elif check_type(var, bool):
10            reversed_var = True if False else False14            reversed_var = True if False else False
11        else:15        else:
12            reversed_var = var16            reversed_var = var
13        reversed_list.append(reversed_var)17        reversed_list.append(reversed_var)
1418
15    return reversed_list[::-1]19    return reversed_list[::-1]
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op