Решение на BiDict от Илиян Величков

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

Към профила на Илиян Величков

Резултати

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

Код

class BiDict(dict):
def __init__(self):
super(BiDict,self).__init__()
def __init__(self,something):
'''
something can be mapping or iterable see help(dict)
'''
if isinstance(something,dict):
mapping=something.items()
else:
mapping=something
bidict=self.__make_bidict(mapping)
super(BiDict,self).__init__(bidict)
def __make_bidict(self,mapping):
'''
Takes [(a1,b1),(a2,b2),...]
'''
keys_values={}
values_keys={}
return_dict={}
for k,v in mapping:
if k in values_keys:
values_keys.pop(k)
if v in keys_values:
keys_values.pop(v)
keys_values[k]=v
values_keys[v]=k
for v,k in values_keys.items():
if not v in keys_values:
return_dict[values_keys[v]]=keys_values[k]
return return_dict
def inverse(self):
reversed={}
for k,v in self.items():
reversed[v]=k
self.clear()
#self.update(reversed)
super(BiDict,self).update(reversed)
def update(self,E,**F):
bidict_e=self.__make_bidict(dict(E).items())
super(BiDict,self).update(bidict_e)
bidict_e.update(self.__make_bidict(dict(**F).items()))
super(BiDict,self).update(bidict_e)
new_bidict=self.__make_bidict(self.items())
self.clear()
super(BiDict,self).update(new_bidict)
def __setitem__(self,key,value):
self.update({key:value})

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

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

+class BiDict(dict):
+ def __init__(self):
+ super(BiDict,self).__init__()
+
+ def __init__(self,something):
+ '''
+ something can be mapping or iterable see help(dict)
+ '''
+ if isinstance(something,dict):
+ mapping=something.items()
+ else:
+ mapping=something
+ bidict=self.__make_bidict(mapping)
+ super(BiDict,self).__init__(bidict)
+
+ def __make_bidict(self,mapping):
+ '''
+ Takes [(a1,b1),(a2,b2),...]
+ '''
+
+ keys_values={}
+ values_keys={}
+ return_dict={}
+ for k,v in mapping:
+ if k in values_keys:
+ values_keys.pop(k)
+ if v in keys_values:
+ keys_values.pop(v)
+
+ keys_values[k]=v
+ values_keys[v]=k
+
+
+ for v,k in values_keys.items():
+ if not v in keys_values:
+ return_dict[values_keys[v]]=keys_values[k]
+
+
+ return return_dict
+
+ def inverse(self):
+ reversed={}
+ for k,v in self.items():
+ reversed[v]=k
+
+ self.clear()
+ #self.update(reversed)
+ super(BiDict,self).update(reversed)
+
+ def update(self,E,**F):
+ bidict_e=self.__make_bidict(dict(E).items())
+ super(BiDict,self).update(bidict_e)
+
+ bidict_e.update(self.__make_bidict(dict(**F).items()))
+ super(BiDict,self).update(bidict_e)
+
+ new_bidict=self.__make_bidict(self.items())
+ self.clear()
+ super(BiDict,self).update(new_bidict)
+
+
+ def __setitem__(self,key,value):
+ self.update({key:value})