Решение на BiDict от Стоян Стоянов

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

Към профила на Стоян Стоянов

Резултати

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

Код

class BiDict(dict):
def BiDict(self, dic):
for key, value in dic.items():
self[key] = value
def update(self, dic):
for key, value in dic.items():
if type(key) is list or type(value) is list:
raise TypeError
self[key] = value
def pop(self, key_to_del):
for key, value in self.items():
if (key == key_to_del):
del self[key]
return value
def copy(self):
result = dict()
for key, value in self.items():
result[key] = value
return result
def keys(self):
result = []
for key, value in self.items():
result.append(key)
return result
def inverse(self):
result = dict()
for key, value in self.items():
result[value] = key
self.clear()
for second_key, second_value in result.items():
self[second_key] = second_value

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

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

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

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

Стоян обнови решението на 19.04.2012 13:20 (преди над 8 години)

+class BiDict(dict):
+
+ def BiDict(self, dic):
+ for key, value in dic.items():
+ self[key] = value
+
+ def update(self, dic):
+ for key, value in dic.items():
+ if type(key) is list or type(value) is list:
+ raise TypeError
+ self[key] = value
+
+ def pop(self, key_to_del):
+ for key, value in self.items():
+ if (key == key_to_del):
+ del self[key]
+ return value
+
+ def copy(self):
+ result = dict()
+ for key, value in self.items():
+ result[key] = value
+ return result
+
+ def keys(self):
+ result = []
+ for key, value in self.items():
+ result.append(key)
+ return result
+
+ def inverse(self):
+ result = dict()
+ for key, value in self.items():
+ result[value] = key
+ self.clear()
+ for second_key, second_value in result.items():
+ self[second_key] = second_value