# Решение на BiDict от Василина Татарлиева

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

Към профила на Василина Татарлиева

## Резултати

• 3 точки от тестове
• 0 бонус точки
• 3 точки общо
• 7 успешни тест(а)
• 9 неуспешни тест(а)

## Код

class BiDict( dict ):
def __init__( self , init_dict ):
self.normal_dict = {}
self.reverse_dict = {}
if isinstance( init_dict , dict ):
for key in init_dict.keys():
self.reverse_dict[ init_dict[key] ] = key
self.normal_dict[ key ] = init_dict[ key ]
else:
raise TypeError
def __getitem__( self , what ):
return self.normal_dict[ what ]
def __setitem__( self , key , value ):
self.reverse_dict[ value ] = key
self.normal_dict[ key ] = value
def __delitem__( self , key ):
del self.normal_dict[ key ]
def reverse( self ):
for key in self.normal_dict.keys():
value = self.normal_dict[ key ]
del self.normal_dict[ key ]
self.normal_dict[ value ] = key
def __repr__( self ):
return self.normal_dict.__repr__()

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

.EEEE..F....FEEE
======================================================================
ERROR: test_assign_value_and_reverse (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 17, in test_assign_value_and_reverse
self.person.inverse()
AttributeError: 'BiDict' object has no attribute 'inverse'

======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", 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-kxg86q", line 57, in test_copied_circular_values
inversed_circular.inverse()
AttributeError: 'dict' object has no attribute 'inverse'

======================================================================
ERROR: test_double_inverse (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 21, in test_double_inverse
self.person.inverse()
AttributeError: 'BiDict' object has no attribute 'inverse'

======================================================================
ERROR: test_inverse (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 45, in test_inverse
self.person.inverse()
AttributeError: 'BiDict' object has no attribute 'inverse'

======================================================================
ERROR: test_lots_of_even_inverses (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 26, in test_lots_of_even_inverses
self.person.inverse()
AttributeError: 'BiDict' object has no attribute 'inverse'

======================================================================
ERROR: test_lots_of_odd_inverses (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 35, in test_lots_of_odd_inverses
self.person.inverse()
AttributeError: 'BiDict' object has no attribute 'inverse'

======================================================================
FAIL: test_hashing_self (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 65, in test_hashing_self
self.assertRaises(TypeError, self.person.update, {'clone': self.person})
AssertionError: TypeError not raised by update

======================================================================
FAIL: test_invalid_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-kxg86q", line 62, in test_invalid_value
self.assertRaises(TypeError, self.person.update, {'sports': ['boxing',]})
AssertionError: TypeError not raised by update

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

FAILED (failures=2, errors=7)

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

### Василина обнови решението на 19.04.2012 09:39 (преди над 12 години)

+class BiDict( dict ):
+ def __init__( self , init_dict ):
+ self.normal_dict = {}
+ self.reverse_dict = {}
+ if isinstance( init_dict , dict ):
+ for key in init_dict.keys():
+ self.reverse_dict[ init_dict[key] ] = key
+ self.normal_dict[ key ] = init_dict[ key ]
+ else:
+ raise TypeError
+
+ def __getitem__( self , what ):
+ return self.normal_dict[ what ]
+
+ def __setitem__( self , key , value ):
+ self.reverse_dict[ value ] = key
+ self.normal_dict[ key ] = value
+
+ def __delitem__( self , key ):
+ del self.normal_dict[ key ]
+
+ def reverse( self ):
+ for key in self.normal_dict.keys():
+ value = self.normal_dict[ key ]
+ del self.normal_dict[ key ]
+ self.normal_dict[ value ] = key
+ def __repr__( self ):
+ return self.normal_dict.__repr__()