Решение на BiDict от Албена Куманова

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

Към профила на Албена Куманова

Резултати

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

Код

class BiDict(dict):
def __init__(self, dictionary=None):
dictionary = dictionary or {}
super().__init__(dictionary)
self.__make_bidict(dictionary)
def update(self, dictionary=None, **kwargs):
if dictionary is None:
pass
elif isinstance(dictionary, dict):
super().update(dictionary)
else:
for key, value in dictionary.items():
super().__setitem__(key, value)
if kwargs:
super().update(kwargs)
self.__make_bidict(dictionary)
def __setitem__(self, k, v):
hash(v)
dictionary = dict(set(super().items()) | set((k, v)))
self.__make_bidict(dictionary)
def inverse(self):
dictionary = super().copy()
super().clear()
for key, value in dictionary.items():
super().__setitem__(value, key)
def __make_bidict(self, dictionary):
items = [(key, value) for key, value in dictionary.items()
if value in set(dictionary.values())]
super().update(items)

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

EEEE....F.F.....
======================================================================
ERROR: test_assign_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-ybitbq", line 12, in test_assign_value
self.person['last_name'] = 'Кънчов'
File "/tmp/solution20120627-22085-ipy883", line 22, in __setitem__
dictionary = dict(set(super().items()) | set((k, v)))
ValueError: dictionary update sequence element #1 has length 6; 2 is required

======================================================================
ERROR: test_assign_value_and_reverse (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-ybitbq", line 16, in test_assign_value_and_reverse
self.person['last_name'] = 'Кънчов'
File "/tmp/solution20120627-22085-ipy883", line 22, in __setitem__
dictionary = dict(set(super().items()) | set((k, v)))
ValueError: dictionary update sequence element #1 has length 6; 2 is required

======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-ybitbq", 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-ybitbq", 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-ybitbq", 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-ybitbq", 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.004s

FAILED (failures=2, errors=4)

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

Албена обнови решението на 19.04.2012 10:52 (преди над 12 години)

+class BiDict(dict):
+
+ def __init__(self, dictionary=None):
+ dictionary = dictionary or {}
+ super().__init__(dictionary)
+ self.__make_bidict(dictionary)
+
+ def update(self, dictionary=None, **kwargs):
+ if dictionary is None:
+ pass
+ elif isinstance(dictionary, dict):
+ super().update(dictionary)
+ else:
+ for key, value in dictionary.items():
+ super().__setitem__(key, value)
+ if kwargs:
+ super().update(kwargs)
+ self.__make_bidict(dictionary)
+
+ def __setitem__(self, k, v):
+ hash(v)
+ dictionary = dict(set(super().items()) | set((k, v)))
+ self.__make_bidict(dictionary)
+
+ def inverse(self):
+ dictionary = super().copy()
+ super().clear()
+ for key, value in dictionary.items():
+ super().__setitem__(value, key)
+
+ def __make_bidict(self, dictionary):
+ items = [(key, value) for key, value in dictionary.items()
+ if value in set(dictionary.values())]
+ super().update(items)