Решение на BiDict от Галин Тодоров

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

Към профила на Галин Тодоров

Резултати

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

Код

class BiDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
def __setitem__(self, index, value):
if not value.__hash__:
raise TypeError("unhashable value:"+ str(value))
delete_key = None
for key,val in zip(self,self.values()):
if value == val:
delete_key = key
break
if(delete_key):
self.__delitem__(key)
super(BiDict,self).__setitem__( index, value )
def update(self, *args, **kwargs):
if len(args) > 1:
raise TypeError("update expected < 1 arg, got %d" % len(args))
other = dict(*args, **kwargs)
for key in other:
self[key] = other[key]
def inverse(self):
temp_dict={}
for key, val in zip(self,self.values()):
temp_dict[val] = key
self.clear()
for key,val in zip(temp_dict,temp_dict.values()):
self[key] = val

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

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

+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ self.update(*args, **kwargs)
+
+ def __setitem__(self, index, value):
+ if not value.__hash__:
+ raise TypeError("unhashable value:"+ str(value))
+ delete_key = None
+ for key,val in zip(self,self.values()):
+ if value == val:
+ delete_key = key
+ break
+ if(delete_key):
+ self.__delitem__(key)
+ super(BiDict,self).__setitem__( index, value )
+
+ def update(self, *args, **kwargs):
+ if len(args) > 1:
+ raise TypeError("update expected < 1 arg, got %d" % len(args))
+ other = dict(*args, **kwargs)
+ for key in other:
+ self[key] = other[key]
+
+ def inverse(self):
+ temp_dict={}
+ for key, val in zip(self,self.values()):
+ temp_dict[val] = key
+ self.clear()
+ for key,val in zip(temp_dict,temp_dict.values()):
+ self[key] = val