Решение на BiDict от Тодор Ганчев

Обратно към всички решения

Към профила на Тодор Ганчев

Резултати

• 5 точки от тестове
• 0 бонус точки
• 5 точки общо
• 12 успешни тест(а)
• 4 неуспешни тест(а)

Код

class BiDict(dict):
def __init__(self, *args, **kwargs):
if len(args) == 0:
super(BiDict, self).__init__()
else:
new_dict = BiDict()
new_dict.update(args[0])
super(BiDict, self).__init__(new_dict)
def update(self, dictionary):
for k in dictionary:
self[k] = dictionary[k]
def __setitem__(self, key, value):
{value: key}
keys_to_delete = [each_key for each_key in self
if self[each_key] == value]
for each_key in keys_to_delete:
self.pop(each_key)
super(BiDict, self).__setitem__(key, value)
def __repr__(self):
return 'BiDict(' + super(BiDict, self).__repr__() + ')'
def inverse(self):
new_dict = {self[key]: key for key in self}
self.clear()
self.update(new_dict)

Лог от изпълнението

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-h6aa3q", line 49, in test_circular_values
circular = self.solutions.BiDict({1: 2, 2: 3, 3: 1})
AttributeError: 'BiDictTestCase' object has no attribute 'solutions'

======================================================================
ERROR: test_copied_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-h6aa3q", line 57, in test_copied_circular_values
inversed_circular.inverse()
AttributeError: 'dict' object has no attribute 'inverse'

======================================================================
FAIL: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-h6aa3q", line 78, in test_insert_existing_key_with_existing_value
self.assertNotIn('age', new_person.keys())
AssertionError: 'age' unexpectedly found in dict_keys(['age', 'name', 'sex'])

======================================================================
FAIL: test_insert_none_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-h6aa3q", line 92, in test_insert_none_existing_key_with_existing_value
self.assertNotIn('age', new_person.keys())
AssertionError: 'age' unexpectedly found in dict_keys(['age', 'name', 'none', 'sex'])

----------------------------------------------------------------------
Ran 16 tests in 0.003s

FAILED (failures=2, errors=2)

История (1 версия и 0 коментара)

Тодор обнови решението на 19.04.2012 13:55 (преди над 12 години)

+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ if len(args) == 0:
+ super(BiDict, self).__init__()
+ else:
+ new_dict = BiDict()
+ new_dict.update(args[0])
+ super(BiDict, self).__init__(new_dict)
+
+ def update(self, dictionary):
+ for k in dictionary:
+ self[k] = dictionary[k]
+
+ def __setitem__(self, key, value):
+ {value: key}
+
+ keys_to_delete = [each_key for each_key in self
+ if self[each_key] == value]
+ for each_key in keys_to_delete:
+ self.pop(each_key)
+
+ super(BiDict, self).__setitem__(key, value)
+
+ def __repr__(self):
+ return 'BiDict(' + super(BiDict, self).__repr__() + ')'
+
+ def inverse(self):
+ new_dict = {self[key]: key for key in self}
+ self.clear()
+ self.update(new_dict)