Решение на BiDict от Борислав Статев

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

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

Резултати

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

Код

import collections
class BiDict(dict):
def __init__(self, dictionary):
self.update(dict(dictionary))
def __setitem__ (self, key, value):
if value in list(self.values()):
self.inverse()
super(BiDict, self).__setitem__(value, key)
self.inverse()
if not value.__hash__:
raise TypeError("unhashable type")
elif value not in list(self.values()):
super(BiDict, self).__setitem__(key, value)
def inverse(self):
invertse_copy = dict([[v,k] for k,v in self.items()])
super(BiDict,self).clear()
super(BiDict,self).update(invertse_copy)
def setdefault(self, key, value = None):
if key not in self.keys():
self[key] = value
return value
else:
return self[key]
def update(self, dictionary = None, **kwarg):
if isinstance(dictionary, dict):
for key in dictionary.keys():
self[key] = dictionary[key]
elif isinstance(dictionary, collections.Iterable):
for (key, value) in dictionary:
self[key] = value
else:
for key in kwarg:
self[key] = kwarg[key]
return None

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

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

+import collections
+class BiDict(dict):
+ def __init__(self, dictionary):
+ self.update(dict(dictionary))
+ def __setitem__ (self, key, value):
+ if value in list(self.values()):
+ self.inverse()
+ super(BiDict, self).__setitem__(value, key)
+ self.inverse()
+ if not value.__hash__:
+ raise TypeError("unhashable type")
+ elif value not in list(self.values()):
+ super(BiDict, self).__setitem__(key, value)
+ def inverse(self):
+ invertse_copy = dict([[v,k] for k,v in self.items()])
+ super(BiDict,self).clear()
+ super(BiDict,self).update(invertse_copy)
+ def setdefault(self, key, value = None):
+ if key not in self.keys():
+ self[key] = value
+ return value
+ else:
+ return self[key]
+ def update(self, dictionary = None, **kwarg):
+ if isinstance(dictionary, dict):
+ for key in dictionary.keys():
+ self[key] = dictionary[key]
+ elif isinstance(dictionary, collections.Iterable):
+ for (key, value) in dictionary:
+ self[key] = value
+ else:
+ for key in kwarg:
+ self[key] = kwarg[key]
+ return None