Решение на BiDict от Александър Иванов

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

Към профила на Александър Иванов

Резултати

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

Код

class BiDict(dict):
'''2 dicts
don't use a double size of memory most
of the time
'''
def __init__(self, *args, **kwargs):
col = [] if len(args) == 0 else args[0]
super().__init__(col)
self._inv = {f:e for e, f in self.items()}
def __setitem__(self, k, v):
if v in self._inv :
del self[self._inv[v]]
if k in self :
del self._inv[self[k]]
print(v,k)
self._inv[v] = k
super().__setitem__(k,v)
def inverse(self):
dic = self._inv.copy()
for k,v in dic.items():
del self[v]
for k,v in dic.items():
super().__setitem__(k,v)
self._inv = {v:k for k, v in dic.items()}
def update(self,d = None):
if d is None:
pass
else:
self.__init__(d)

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

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

+class BiDict(dict):
+ '''2 dicts
+ don't use a double size of memory most
+ of the time
+ '''
+ def __init__(self, *args, **kwargs):
+
+
+ col = [] if len(args) == 0 else args[0]
+ super().__init__(col)
+ self._inv = {f:e for e, f in self.items()}
+
+ def __setitem__(self, k, v):
+ if v in self._inv :
+ del self[self._inv[v]]
+
+ if k in self :
+ del self._inv[self[k]]
+ print(v,k)
+ self._inv[v] = k
+ super().__setitem__(k,v)
+
+ def inverse(self):
+ dic = self._inv.copy()
+ for k,v in dic.items():
+ del self[v]
+ for k,v in dic.items():
+ super().__setitem__(k,v)
+
+ self._inv = {v:k for k, v in dic.items()}
+
+
+ def update(self,d = None):
+ if d is None:
+ pass
+ else:
+ self.__init__(d)