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

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

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

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 6 успешни тест(а)
  • 10 неуспешни тест(а)

Код

class BiDict:
def __init__(self, dict_p={}):
self.dict = dict_p
def get(self, key):
if key in self.dict[key]:
return self.dict[key]
def __getitem__(self, i):
if i in self.dict:
return self.dict[i]
def __setitem__(self, key, value):
try:
hash(value)
if value in list(self.dict.values()):
for i in self:
if self[i] == value:
del self[i]
self.dict[key] = value
break
else:
self.dict[key] = value
except TypeError:
raise TypeError
def __delitem__(self, key):
del self.dict[key]
def inverse(self):
list_keys = list(self.dict.keys())
for i in list_keys:
self[self[i]] = i
del self[i]
def __len__(self):
return len(self.dict)
def __iter__(self):
return iter(self.dict)
def __str__(self):
return str(self.dict)

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

..EE..FEEEEEEE..
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", 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-1inmfr0", line 56, in test_copied_circular_values
    inversed_circular = circular.copy()
AttributeError: 'BiDict' object has no attribute 'copy'

======================================================================
ERROR: test_hashing_self (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 65, in test_hashing_self
    self.assertRaises(TypeError, self.person.update, {'clone': self.person})
AttributeError: 'BiDict' object has no attribute 'update'

======================================================================
ERROR: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 74, in test_insert_existing_key_with_existing_value
    new_person = self.person.copy()
AttributeError: 'BiDict' object has no attribute 'copy'

======================================================================
ERROR: test_insert_existing_key_with_none_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 81, in test_insert_existing_key_with_none_existing_value
    new_person = self.person.copy()
AttributeError: 'BiDict' object has no attribute 'copy'

======================================================================
ERROR: test_insert_none_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 88, in test_insert_none_existing_key_with_existing_value
    new_person = self.person.copy()
AttributeError: 'BiDict' object has no attribute 'copy'

======================================================================
ERROR: test_insert_none_existing_key_with_none_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 95, in test_insert_none_existing_key_with_none_existing_value
    new_person = self.person.copy()
AttributeError: 'BiDict' object has no attribute 'copy'

======================================================================
ERROR: test_invalid_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 62, in test_invalid_value
    self.assertRaises(TypeError, self.person.update, {'sports': ['boxing',]})
AttributeError: 'BiDict' object has no attribute 'update'

======================================================================
ERROR: test_inverse (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1inmfr0", line 46, in test_inverse
    self.assertIn('Кънчо', self.person.keys())
AttributeError: 'BiDict' object has no attribute 'keys'

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

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

FAILED (failures=1, errors=9)

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

Илия обнови решението на 15.04.2012 15:01 (преди над 8 години)

+class BiDict:
+
+ def __init__(self, dict_p={}):
+ self.dict = dict_p
+
+ def get(self, key):
+ if key in self.dict[key]:
+ return self.dict[key]
+
+ def __getitem__(self, i):
+ if i in self.dict:
+ return self.dict[i]
+
+ def __setitem__(self, key, value):
+ try:
+ hash(value)
+ if value in list(self.dict.values()):
+ for i in self:
+ if self[i] == value:
+ del self[i]
+ self.dict[key] = value
+ break
+ else:
+ self.dict[key] = value
+ except TypeError:
+ raise TypeError
+
+ def __delitem__(self, key):
+ del self.dict[key]
+
+ def inverse(self):
+ list_keys = list(self.dict.keys())
+ for i in list_keys:
+ self[self[i]] = i
+ del self[i]
+
+ def __len__(self):
+ return len(self.dict)
+
+ def __iter__(self):
+ return iter(self.dict)
+
+ def __str__(self):
+ return str(self.dict)