# Решение на BiDict от Станислав Станчев

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

Към профила на Станислав Станчев

## Резултати

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

## Код

class BiDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
def __setitem__(self, key, value):
if value.__hash__:
if value not in self.values():
super(BiDict, self).__setitem__(key, value)
elif value in self.values() and key in self.keys():
self.delete_diplicate(value)
super(BiDict, self).__setitem__(key, value)
else:
raise TypeError("Data duplication!")
else:
raise TypeError("unhashable type: {0}".format(type(value)))
def delete_diplicate(self, value):
for key in self:
if self[key] == value:
del self[key]
break
def update(self, *args, **kwargs):
if args:
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got {0}" .format(len(args)))
other = dict(args[0])
for key in other:
if other[key] in self.values():
self.delete_diplicate(other[key])
self[key] = other[key]
for key in kwargs:
self[key] = kwargs[key]
def setdefault(self, key, value=None):
if key not in self:
self.__setitem__(key, value)
return self[key]
def inverse(self):
inverted_dict = {value: key for key, value in self.items()}
self.clear()
self.update(inverted_dict)

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

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

+class BiDict(dict):
+ def __init__(self, *args, **kwargs):
+ self.update(*args, **kwargs)
+
+ def __setitem__(self, key, value):
+ if value.__hash__:
+ if value not in self.values():
+ super(BiDict, self).__setitem__(key, value)
+ elif value in self.values() and key in self.keys():
+ self.delete_diplicate(value)
+ super(BiDict, self).__setitem__(key, value)
+ else:
+ raise TypeError("Data duplication!")
+ else:
+ raise TypeError("unhashable type: {0}".format(type(value)))
+
+ def delete_diplicate(self, value):
+ for key in self:
+ if self[key] == value:
+ del self[key]
+ break
+
+ def update(self, *args, **kwargs):
+ if args:
+ if len(args) > 1:
+ raise TypeError("update expected at most 1 arguments, got {0}" .format(len(args)))
+ other = dict(args[0])
+ for key in other:
+ if other[key] in self.values():
+ self.delete_diplicate(other[key])
+ self[key] = other[key]
+ for key in kwargs:
+ self[key] = kwargs[key]
+
+ def setdefault(self, key, value=None):
+ if key not in self:
+ self.__setitem__(key, value)
+ return self[key]
+
+ def inverse(self):
+ inverted_dict = {value: key for key, value in self.items()}
+ self.clear()
+ self.update(inverted_dict)
+