Решение на BiDict от Ангел Миланов

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

Към профила на Ангел Миланов

Резултати

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

Код

class BiDict:
def __init__(self, dictionary = {}):
self.bidict = dictionary
def inverse(self):
self.bidict = {self.bidict[x]: x for x in self.bidict}
def __str__(self):
return "BiDic(%s)" % (self.bidict)
def keys(self):
return self.bidict.keys()
def pop(self, key, *args):
return self.bidict.pop(key, *args)
def copy(self):
return self.bidict.copy()
def update(self,dict2):
for key in dict:
if(olddict[key].__hash__):
raise TypeError
else:
self.bidict.update(dict2)
def __getitem__(self,key):
return self.bidict[key]
def __setitem__(self,key,value):
self.bidict[key] = value
self.inverse()
self.bidict[value] = key
self.inverse()
def __delitem__(self,key):
del self.bidict[key]

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

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

======================================================================
FAIL: test_has_dict_attrs (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1itbm8k", line 71, in test_has_dict_attrs
self.assertIn('items', dir(self.person))
AssertionError: 'items' not found in ['__class__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bidict', 'copy', 'inverse', 'keys', 'pop', 'update']

======================================================================
FAIL: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test20120627-22085-1itbm8k", 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-1itbm8k", 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=3, errors=2)

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

Ангел обнови решението на 19.04.2012 16:01 (преди над 12 години)

+class BiDict:
+ def __init__(self, dictionary = {}):
+ self.bidict = dictionary
+
+ def inverse(self):
+ self.bidict = {self.bidict[x]: x for x in self.bidict}
+
+ def __str__(self):
+ return "BiDic(%s)" % (self.bidict)
+
+ def keys(self):
+ return self.bidict.keys()
+
+ def pop(self, key, *args):
+ return self.bidict.pop(key, *args)
+
+ def copy(self):
+ return self.bidict.copy()
+
+ def update(self,dict2):
+ for key in dict:
+ if(olddict[key].__hash__):
+ raise TypeError
+ else:
+ self.bidict.update(dict2)
+
+ def __getitem__(self,key):
+ return self.bidict[key]
+
+ def __setitem__(self,key,value):
+ self.bidict[key] = value
+ self.inverse()
+ self.bidict[value] = key
+ self.inverse()
+
+ def __delitem__(self,key):
+ del self.bidict[key]
+