# Решение на BiDict от Орлин Христов

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

Към профила на Орлин Христов

## Резултати

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

## Код

class Reverter():
def inverse(self):
inverses = []
for key, value in self.items():
inverses.append([value, key])
self.clear()
self.update(inverses)
def __setitem__(self, key, value):
hash(value)
other = self.__getByValue(value)
if other:
del self[other[0]]
super().__setitem__(key, value)
def __init__(self):
self.inverse()
self.inverse()
def __getByValue(self, value):
for item in self.items():
if item[1] == value:
return item
def update(self, *args, **kwargs):
dict_ = dict(*args, **kwargs)
for item in dict_.items():
self.__setitem__(*item)
class BiDict(Reverter, dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
Reverter.__init__(self)
b = BiDict(one=1, two=2)
b.update(three=3, four=4)
print('______')
b.update({'sto':100, 'piise':50})

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

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

### Орлин обнови решението на 17.04.2012 09:54 (преди над 12 години)

+class Reverter():
+ def inverse(self):
+ inverses = []
+ for key, value in self.items():
+ inverses.append([value, key])
+ self.clear()
+ self.update(inverses)
+
+ def __setitem__(self, key, value):
+ hash(value)
+ other = self.__getByValue(value)
+ if other:
+ del self[other[0]]
+ super().__setitem__(key, value)
+
+ def __init__(self):
+ self.inverse()
+ self.inverse()
+
+ def __getByValue(self, value):
+ for item in self.items():
+ if item[1] == value:
+ return item
+
+ def update(self, *args, **kwargs):
+ dict_ = dict(*args, **kwargs)
+ for item in dict_.items():
+ self.__setitem__(*item)
+
+
+class BiDict(Reverter, dict):
+ def __init__(self, *args, **kwargs):
+ dict.__init__(self, *args, **kwargs)
+ Reverter.__init__(self)
+
+
+b = BiDict(one=1, two=2)
+b.update(three=3, four=4)
+print('______')
+b.update({'sto':100, 'piise':50})