Решение на BiDict от Станислав Гатев

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

Към профила на Станислав Гатев

Резултати

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

Код

class BiDict(dict):
def __init__(self, dictionary):
self.update(dictionary)
def __setitem__(self, key, value):
hash(value)
super().__setitem__(key, value)
def __repr__(self):
return 'BiDict(' + super().__repr__() + ')'
def update(self, dictionary):
for key, value in dictionary.items():
self.__setitem__(key, value)
def inverse(self):
inversed_dict = {value: key for key, value in self.items()}
self.clear()
self.update(inversed_dict)

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1spox4w", 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-1spox4w", 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-1spox4w", 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-1spox4w", 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 версия и 2 коментара)

Станислав обнови решението на 10.04.2012 16:07 (преди около 12 години)

+class BiDict(dict):
+ def __init__(self, dictionary):
+ self.update(dictionary)
+
+ def __setitem__(self, key, value):
+ hash(value)
+ super().__setitem__(key, value)
+
+ def __repr__(self):
+ return 'BiDict(' + super().__repr__() + ')'
+
+ def update(self, dictionary):
+ for key, value in dictionary.items():
+ self.__setitem__(key, value)
+
+ def inverse(self):
+ inversed_dict = {value: key for key, value in self.items()}
+ self.clear()
+ self.update(inversed_dict)

Здравей, Решението ти е впечатляващо. Само две бързи забележки:

  • Добра идея е да предефинираш и repr, което в твоя случай няма да се различава изобщо от str
  • Методът inverse е изключително четим, но какво ще стане ако ти подам речник: {1: 2, 2: 3, 3: 1} и изпълна inverse()? :)