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

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

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

Резултати

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

Код

class BiDict:
def __init__(self,information):
if type(information) != type({}):
raise TypeError("unhashable type: 'list'")
else:
self.information = information
def __repr__(self):
return 'BiDict(' + str(self.information) + ')'
def __getitem__(self,key):
return self.information[key]
def inverse(self):
new_info = {}
for i in self.information:
new_info[self.information[i]] = i
self.information = new_info
def pop(self,key):
return self.information.pop(key)
def keys(self):
return self.information.keys()
def copy(self):
return self.information.copy()
def update(self,supplement):
if type(supplement) != type({}):
raise TypeError("unhashable type %s" % type(supplement))
elif type(next(iter(supplement.values()))) == type([]):
raise TypeError("unhashable type %s" %
type(next(iter(supplement.values()))))
else:
self.information.update(supplement)
def values(self):
return self.information.values()
def clear(self):
return self.information.clear()
def fromkeys(self,sequence,value=None):
return self.information.fromkeys(sequence,value)
def get(self,key,value=None):
return self.information.get(key,value)
def setdefault(self,key,default=None):
return self.information.setdefault(key,default)

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

EEEE..FFF.F.....
======================================================================
ERROR: test_assign_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1jjttti", line 12, in test_assign_value
    self.person['last_name'] = 'Кънчов'
TypeError: 'BiDict' object does not support item assignment

======================================================================
ERROR: test_assign_value_and_reverse (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1jjttti", line 16, in test_assign_value_and_reverse
    self.person['last_name'] = 'Кънчов'
TypeError: 'BiDict' object does not support item assignment

======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1jjttti", 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-1jjttti", line 57, in test_copied_circular_values
    inversed_circular.inverse()
AttributeError: 'dict' object has no attribute 'inverse'

======================================================================
FAIL: test_has_dict_attrs (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1jjttti", line 71, in test_has_dict_attrs
    self.assertIn('items', dir(self.person))
AssertionError: 'items' not found in ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'clear', 'copy', 'fromkeys', 'get', 'information', 'inverse', 'keys', 'pop', 'setdefault', 'update', 'values']

======================================================================
FAIL: test_hashing_self (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1jjttti", line 65, in test_hashing_self
    self.assertRaises(TypeError, self.person.update, {'clone': self.person})
AssertionError: TypeError not raised by update

======================================================================
FAIL: test_insert_existing_key_with_existing_value (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-1jjttti", 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-1jjttti", 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=4, errors=4)

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

Илия обнови решението на 11.04.2012 21:05 (преди над 12 години)

+class BiDict:
+ def __init__(self,information):
+ if type(information) != type({}):
+ raise TypeError("unhashable type: 'list'")
+ else:
+ self.information = information
+
+ def __repr__(self):
+ return 'BiDict(' + str(self.information) + ')'
+
+ def __getitem__(self,key):
+ return self.information[key]
+
+ def inverse(self):
+ new_info = {}
+ for i in self.information:
+ new_info[self.information[i]] = i
+ self.information = new_info
+
+ def pop(self,key):
+ return self.information.pop(key)
+
+ def keys(self):
+ return self.information.keys()
+ def copy(self):
+ return self.information.copy()
+
+ def update(self,supplement):
+ if type(supplement) != type({}):
+ raise TypeError("unhashable type %s" % type(supplement))
+ elif type(next(iter(supplement.values()))) == type([]):
+ raise TypeError("unhashable type %s" %
+ type(next(iter(supplement.values()))))
+ else:
+ self.information.update(supplement)
+
+ def values(self):
+ return self.information.values()
+
+ def clear(self):
+ return self.information.clear()
+
+ def fromkeys(self,sequence,value=None):
+ return self.information.fromkeys(sequence,value)
+
+ def get(self,key,value=None):
+ return self.information.get(key,value)
+
+ def setdefault(self,key,default=None):
+ return self.information.setdefault(key,default)