Решение на BiDict от Никола Велинов

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

Към профила на Никола Велинов

Резултати

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

Код

class BiDict(dict):
def __init__(self, *args, **kwargs):
super(BiDict, self).__init__()
for argument in args:
for key, val in argument.items():
hash(key)
hash(val)
self[key] = val
for key, val in kwargs.items():
hash(key)
hash(val)
self[key] = val
def __setitem__(self, key, val):
hash(val)
hash(key)
if val in self.values():
for search_key, search_val in self.items():
if search_val == val:
key_to_remove = search_key
del self[key_to_remove]
super().__setitem__(key, val)
def inverse(self):
inverted = [{val:key} for key, val in self.items()]
self.clear()
for new_pair in inverted:
self.update(new_pair)
def update(self, dict):
for key, val in dict.items():
hash(key)
hash(val)
self[key] = val

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

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

Никола обнови решението на 18.04.2012 16:38 (преди почти 12 години)

+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ super(BiDict, self).__init__()
+ for argument in args:
+ for key, val in argument.items():
+ hash(key)
+ hash(val)
+ self[key] = val
+ for key, val in kwargs.items():
+ hash(key)
+ hash(val)
+ self[key] = val
+
+ def __setitem__(self, key, val):
+ hash(val)
+ hash(key)
+ if val in self.values():
+ for search_key, search_val in self.items():
+ if search_val == val:
+ key_to_remove = search_key
+ del self[key_to_remove]
+ super().__setitem__(key, val)
+
+ def inverse(self):
+ inverted = [{val:key} for key, val in self.items()]
+ self.clear()
+ for new_pair in inverted:
+ self.update(new_pair)
+
+ def update(self, dict):
+ for key, val in dict.items():
+ hash(key)
+ hash(val)
+ self[key] = val