Решение на BiDict от Павлина Гатова

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

Към профила на Павлина Гатова

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 9 успешни тест(а)
  • 7 неуспешни тест(а)

Код

class BiDict():
def __init__(self, other_dict={}):
self.dict = dict(other_dict.items())
def __repr__(self):
return 'BiDict(' + str(self.dict) + ')'
def __getitem__(self, key):
return self.dict.__getitem__(key)
def __setitem__(self, key, what):
self.dict.__setitem__(key, what)
def keys(self):
return self.dict.keys()
def values(self):
return self.dict.values()
def items(self):
return self.dict.items()
def fromkeys(cls, value=0):
d = BiDict()
for x in len(cls):
d[key] = value
return d
def clear(self):
for i in range(len(self.dict)):
self.dict.popitem()
def pop(self, *args):
return self.dict.pop(*args)
def popitem(self):
self.dict.popitem()
def setdefault(self, key, default=0):
return self.dict.setdefault(key, default)
def __delitem__(self, key):
self.dict.__delitem__(key)
def __contains__(self, key):
return self.dict.__contains__(key)
def __eq__(self, other):
return self.dict.__eq__(other)
def __ne__(self, other):
return self.dict.__ne__(other)
def __iter__(self):
return self.dict.__iter__()
def get(self, key, default=0):
return self.dict.get(key, default)
def __len__(self):
return self.dict.__len__()
def update(self, other_dict):
self.dict.update(other_dict)
self.inverse()
self.inverse()
def inverse(self):
list = []
for x in range(len(self.dict)):
(x, y) = self.dict.popitem()
list.append((x, y))
for y in range(len(list)):
self.dict[list[y][1]] = list[y][0]

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

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

======================================================================
ERROR: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-ui8mhu", 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-ui8mhu", 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-ui8mhu", 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-ui8mhu", line 95, in test_insert_none_existing_key_with_none_existing_value
    new_person = self.person.copy()
AttributeError: 'BiDict' object has no attribute 'copy'

======================================================================
FAIL: test_has_dict_attrs (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-ui8mhu", line 70, in test_has_dict_attrs
    self.assertIn('copy', dir(self.person))
AssertionError: 'copy' not found in ['__class__', '__contains__', '__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__', 'clear', 'dict', 'fromkeys', 'get', 'inverse', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

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

FAILED (failures=1, errors=6)

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

Павлина обнови решението на 19.04.2012 02:45 (преди почти 12 години)

+class BiDict():
+ def __init__(self, other_dict={}):
+ self.dict = dict(other_dict.items())
+
+ def __repr__(self):
+ return 'BiDict(' + str(self.dict) + ')'
+
+ def __getitem__(self, key):
+ return self.dict.__getitem__(key)
+
+ def __setitem__(self, key, what):
+ self.dict.__setitem__(key, what)
+
+ def keys(self):
+ return self.dict.keys()
+
+ def values(self):
+ return self.dict.values()
+
+ def items(self):
+ return self.dict.items()
+
+ def fromkeys(cls, value=0):
+ d = BiDict()
+ for x in len(cls):
+ d[key] = value
+ return d
+
+ def clear(self):
+ for i in range(len(self.dict)):
+ self.dict.popitem()
+
+ def pop(self, *args):
+ return self.dict.pop(*args)
+
+ def popitem(self):
+ self.dict.popitem()
+
+ def setdefault(self, key, default=0):
+ return self.dict.setdefault(key, default)
+
+ def __delitem__(self, key):
+ self.dict.__delitem__(key)
+
+ def __contains__(self, key):
+ return self.dict.__contains__(key)
+
+ def __eq__(self, other):
+ return self.dict.__eq__(other)
+
+ def __ne__(self, other):
+ return self.dict.__ne__(other)
+
+ def __iter__(self):
+ return self.dict.__iter__()
+
+ def get(self, key, default=0):
+ return self.dict.get(key, default)
+
+ def __len__(self):
+ return self.dict.__len__()
+
+ def update(self, other_dict):
+ self.dict.update(other_dict)
+ self.inverse()
+ self.inverse()
+
+ def inverse(self):
+ list = []
+ for x in range(len(self.dict)):
+ (x, y) = self.dict.popitem()
+ list.append((x, y))
+ for y in range(len(list)):
+ self.dict[list[y][1]] = list[y][0]