Решение на BiDict от Дамяна Иванова

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

Към профила на Дамяна Иванова

Резултати

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

Код

import collections
class BiDict(dict):
def BiDict(self, dictionary):
helper = dict(dictionary)
for (name, value) in helper.items():
self.__setitem__(name, value)
def inverse(self):
bdictionary = BiDict()
for (name, value) in self.items():
bdictionary.__setitem__(value, name)
self.clear()
self.update(bdictionary)
def getkey(self, value):
for (name, val) in self.items():
if val == value:
return name
raise KeyError
def __setitem__(self, name, value):
if not isinstance(value, collections.Hashable):
raise TypeError
else:
if value not in list(map(self.get, self.__iter__())):
dict.__setitem__(self, name, value)
else:
key = self.getkey(value)
self.pop(key)
dict.__setitem__(self, name, value)
def update(self, E):
helper = dict(E)
for (name, value) in helper.items():
self.__setitem__(name, value)

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

..EE....F.F.....
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1ayld2c", 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-1ayld2c", 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-1ayld2c", 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-1ayld2c", 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=2)

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

Дамяна обнови решението на 18.04.2012 20:30 (преди над 12 години)

+import collections
+
+class BiDict(dict):
+ def BiDict(self, dictionary):
+ helper = dict(dictionary)
+ for (name, value) in helper.items():
+ self.__setitem__(name, value)
+
+ def inverse(self):
+ bdictionary = BiDict()
+ for (name, value) in self.items():
+ bdictionary.__setitem__(value, name)
+ self.clear()
+ self.update(bdictionary)
+
+ def getkey(self, value):
+ for (name, val) in self.items():
+ if val == value:
+ return name
+ raise KeyError
+
+ def __setitem__(self, name, value):
+ if not isinstance(value, collections.Hashable):
+ raise TypeError
+ else:
+ if value not in list(map(self.get, self.__iter__())):
+ dict.__setitem__(self, name, value)
+ else:
+ key = self.getkey(value)
+ self.pop(key)
+ dict.__setitem__(self, name, value)
+
+ def update(self, E):
+ helper = dict(E)
+ for (name, value) in helper.items():
+ self.__setitem__(name, value)