Решение на BiDict от Мирослав Милев

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

Към профила на Мирослав Милев

Резултати

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

Код

class BiDict(dict):
def __init__(self, d):
for key, value in d.items():
self._check_mutable(value)
self._remove_duplicate_values(d)
dict.__init__(self, d)
def inverse(self):
d = dict()
for key, value in self.items():
d[value] = key
self.clear()
self.update(d)
def __setitem__(self, key, value):
self._check_mutable(value)
for skey, svalue in self.items():
if svalue is value:
del self[skey]
break
dict.__setitem__(self, key, value)
def update(self, d):
for key, value in d.items():
self._check_mutable(value)
dict.update(self, d)
self._remove_duplicate_values(self)
def _check_mutable(self, value):
if not type(value) in [tuple, int, float, complex, str, bool, frozenset]:
raise TypeError ("unhashable type: ", type(value))
def _remove_duplicate_values(self, d):
distinct = dict()
for key, value in d.items():
distinct[value] = key
d.clear()
for key, value in distinct.items():
d[value] = key

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-cwj0sh", 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-cwj0sh", 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-cwj0sh", 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-cwj0sh", 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 16:16 (преди над 12 години)

+class BiDict(dict):
+ def __init__(self, d):
+ for key, value in d.items():
+ self._check_mutable(value)
+ self._remove_duplicate_values(d)
+ dict.__init__(self, d)
+
+ def inverse(self):
+ d = dict()
+ for key, value in self.items():
+ d[value] = key
+ self.clear()
+ self.update(d)
+
+ def __setitem__(self, key, value):
+ self._check_mutable(value)
+ for skey, svalue in self.items():
+ if svalue is value:
+ del self[skey]
+ break
+ dict.__setitem__(self, key, value)
+
+ def update(self, d):
+ for key, value in d.items():
+ self._check_mutable(value)
+ dict.update(self, d)
+ self._remove_duplicate_values(self)
+
+ def _check_mutable(self, value):
+ if not type(value) in [tuple, int, float, complex, str, bool, frozenset]:
+ raise TypeError ("unhashable type: ", type(value))
+
+ def _remove_duplicate_values(self, d):
+ distinct = dict()
+ for key, value in d.items():
+ distinct[value] = key
+ d.clear()
+ for key, value in distinct.items():
+ d[value] = key
+