Решение на BiDict от Димитър Банков

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

Към профила на Димитър Банков

Резултати

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

Код

# solution.py
class BiDict:
def __init__(self, data):
self.keys_dict = {}
self.values_dict = {}
self.update(data)
def inverse(self):
temp_dict_holder = self.keys_dict
self.keys_dict = self.values_dict
self.values_dict = temp_dict_holder
def __repr__(self):
return str(self.keys_dict)
def __getitem__(self, key):
return self.keys_dict[key]
def __setitem__(self, key, value):
if key in self.keys_dict:
if value in self.values_dict:
del self.keys_dict[self.values_dict[value]]
del self.values_dict[self.keys_dict[key]]
else:
if value in self.values_dict:
del self.keys_dict[self.values_dict[value]]
self.keys_dict[key] = value
self.values_dict[value] = key
def keys(self):
return self.keys_dict.keys()
def values(self):
return self.keys_dict.values()
def pop(self, key):
self.values_dict.pop(self.keys_dict.pop(key))
def copy(self):
return BiDict(self.keys_dict)
def update(self, data):
for key, value in data.items():
self[key] = value
# sample_tests.py (updated)
import unittest
from solution import *
class BiDictTestCase(unittest.TestCase):
def setUp(self):
self.person = BiDict({'name': 'Кънчо', 'age': 18, 'sex': 'M'})
def test_get_a_key(self):
self.assertEqual(self.person['name'], 'Кънчо')
def test_inverse(self):
self.person.inverse()
self.assertIn('Кънчо', self.person.keys())
def test_invalid_value(self):
self.assertRaises(TypeError, self.person.update, {'sports': ['boxing',]})
def test_has_dict_attrs(self):
self.assertIn('keys', dir(self.person))
self.assertIn('pop', dir(self.person))
self.assertIn('copy', dir(self.person))
def test_insert_existing_key_with_existing_value(self):
new_person = self.person.copy()
new_person['name'] = 18
self.assertIn('name', new_person.keys())
self.assertIn(18, new_person.values())
self.assertNotIn('age', new_person.keys())
def test_insert_existing_key_with_none_existing_value(self):
new_person = self.person.copy()
new_person['name'] = -1
self.assertIn('name', new_person.keys())
self.assertIn(-1, new_person.values())
self.assertNotIn('Кънчо', new_person.values())
def test_insert_none_existing_key_with_existing_value(self):
new_person = self.person.copy()
new_person['none'] = 18
self.assertIn('none', new_person.keys())
self.assertIn(18, new_person.values())
self.assertNotIn('age', new_person.keys())
def test_insert_none_existing_key_with_none_existing_value(self):
new_person = self.person.copy()
new_person['none'] = -1
self.assertIn('none', new_person.keys())
self.assertIn(-1, new_person.values())
self.assertEquals(len(self.person.keys()) + 1, len(new_person.keys()))
if __name__ == '__main__':
unittest.main()

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

..EF..FF........
======================================================================
ERROR: test_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-amjolb", line 49, in test_circular_values
    circular = self.solutions.BiDict({1: 2, 2: 3, 3: 1})
AttributeError: 'BiDictTestCase' object has no attribute 'solutions'

======================================================================
FAIL: test_copied_circular_values (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-amjolb", line 59, in test_copied_circular_values
    self.assertEqual(circular, inversed_circular)
AssertionError: {1: 2, 2: 3, 3: 1} != {1: 2, 2: 3, 3: 1}

======================================================================
FAIL: test_has_dict_attrs (__main__.BiDictTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-22085-amjolb", 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__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'copy', 'inverse', 'keys', 'keys_dict', 'pop', 'update', 'values', 'values_dict']

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

----------------------------------------------------------------------
Ran 16 tests in 0.002s

FAILED (failures=3, errors=1)

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

Димитър обнови решението на 19.04.2012 03:48 (преди над 8 години)

+# solution.py
+
+class BiDict:
+
+ def __init__(self, data):
+ self.keys_dict = {}
+ self.values_dict = {}
+ self.update(data)
+
+ def inverse(self):
+ temp_dict_holder = self.keys_dict
+ self.keys_dict = self.values_dict
+ self.values_dict = temp_dict_holder
+
+ def __repr__(self):
+ return str(self.keys_dict)
+
+ def __getitem__(self, key):
+ return self.keys_dict[key]
+
+ def __setitem__(self, key, value):
+ if key in self.keys_dict:
+ if value in self.values_dict:
+ del self.keys_dict[self.values_dict[value]]
+ del self.values_dict[self.keys_dict[key]]
+ else:
+ if value in self.values_dict:
+ del self.keys_dict[self.values_dict[value]]
+ self.keys_dict[key] = value
+ self.values_dict[value] = key
+
+ def keys(self):
+ return self.keys_dict.keys()
+
+ def values(self):
+ return self.keys_dict.values()
+
+ def pop(self, key):
+ self.values_dict.pop(self.keys_dict.pop(key))
+
+ def copy(self):
+ return BiDict(self.keys_dict)
+
+ def update(self, data):
+ for key, value in data.items():
+ self[key] = value
+
+# sample_tests.py (updated)
+
+import unittest
+from solution import *
+
+class BiDictTestCase(unittest.TestCase):
+ def setUp(self):
+ self.person = BiDict({'name': 'Кънчо', 'age': 18, 'sex': 'M'})
+
+ def test_get_a_key(self):
+ self.assertEqual(self.person['name'], 'Кънчо')
+
+ def test_inverse(self):
+ self.person.inverse()
+ self.assertIn('Кънчо', self.person.keys())
+
+ def test_invalid_value(self):
+ self.assertRaises(TypeError, self.person.update, {'sports': ['boxing',]})
+
+ def test_has_dict_attrs(self):
+ self.assertIn('keys', dir(self.person))
+ self.assertIn('pop', dir(self.person))
+ self.assertIn('copy', dir(self.person))
+
+ def test_insert_existing_key_with_existing_value(self):
+ new_person = self.person.copy()
+ new_person['name'] = 18
+ self.assertIn('name', new_person.keys())
+ self.assertIn(18, new_person.values())
+ self.assertNotIn('age', new_person.keys())
+
+ def test_insert_existing_key_with_none_existing_value(self):
+ new_person = self.person.copy()
+ new_person['name'] = -1
+ self.assertIn('name', new_person.keys())
+ self.assertIn(-1, new_person.values())
+ self.assertNotIn('Кънчо', new_person.values())
+
+ def test_insert_none_existing_key_with_existing_value(self):
+ new_person = self.person.copy()
+ new_person['none'] = 18
+ self.assertIn('none', new_person.keys())
+ self.assertIn(18, new_person.values())
+ self.assertNotIn('age', new_person.keys())
+
+ def test_insert_none_existing_key_with_none_existing_value(self):
+ new_person = self.person.copy()
+ new_person['none'] = -1
+ self.assertIn('none', new_person.keys())
+ self.assertIn(-1, new_person.values())
+ self.assertEquals(len(self.person.keys()) + 1, len(new_person.keys()))
+
+if __name__ == '__main__':
+ unittest.main()
+
+