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

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

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

Резултати

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

Код

class BiDict(dict):
def __init__(self, _dict):
for value in _dict.values():
hash(value)
dict.__init__(self)
#duplicate check not necessary
self.update(_dict)
def _remove_dups(self):
for i in range(2):
self.inverse()
def inverse(self):
inversed_dict = {v: k for k, v in self.items()}
self.clear()
dict.update(self, inversed_dict)
def __setitem__(self, key, value):
hash(value)
dict.update(self, {key: value})
self._remove_dups()
@classmethod
def fromkeys(cls, keys, value=None):
hash(value)
return BiDict(dict.fromkeys(keys, value))
def setdefault(self, key, default=None):
hash(default)
if key in self.keys():
return self[key]
else:
self[key] = default
self_remove_dups()
return default
def update(self, _dict):
for value in _dict.values():
hash(value)
dict.update(self, _dict)
self._remove_dups()
def copy(self):
return BiDict(dict.copy(self))

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

..E.............
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1idizmo", line 49, in test_circular_values
    circular = self.solutions.BiDict({1: 2, 2: 3, 3: 1})
AttributeError: 'BiDictTestCase' object has no attribute 'solutions'

----------------------------------------------------------------------
Ran 16 tests in 0.003s

FAILED (errors=1)

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

Мирослав обнови решението на 16.04.2012 00:05 (преди над 12 години)

+class BiDict(dict):
+ def __init__(self, _dict):
+ for value in _dict.values():
+ hash(value)
+ dict.__init__(self)
+ #duplicate check not necessary
+ self.update(_dict)
+
+ def _remove_dups(self):
+ for i in range(2):
+ self.inverse()
+
+ def inverse(self):
+ inversed_dict = {v: k for k, v in self.items()}
+ self.clear()
+ dict.update(self, inversed_dict)
+
+ def __setitem__(self, key, value):
+ hash(value)
+ dict.update(self, {key: value})
+ self._remove_dups()
+
+ @classmethod
+ def fromkeys(cls, keys, value=None):
+ hash(value)
+ return BiDict(dict.fromkeys(keys, value))
+
+ def setdefault(self, key, default=None):
+ hash(default)
+ if key in self.keys():
+ return self[key]
+ else:
+ self[key] = default
+ self_remove_dups()
+ return default
+
+ def update(self, _dict):
+ for value in _dict.values():
+ hash(value)
+ dict.update(self, _dict)
+ self._remove_dups()
+
+ def copy(self):
+ return BiDict(dict.copy(self))