Need help on a project To :"Create a class called BankAccount with the following parameters "

lee malitician at gmail.com
Sun Dec 27 10:02:38 EST 2015



> >Instead, your function should examine the "kind" parameter and decide 
> >what to do. So it would reasonably look like this (untested):
> >
> > def manipulate_data(kind, data):
> >   if kind == 'list':
> >     ... do stuff with data using it as a list ...
> >   elif kind == 'set':
> >     ... do stuff with data using it as a set ...
> >   if kind == 'dictionary':
> >     ... do stuff with data using it as a dictionary ...
> >   else:
> >     raise ValueError("invalid kind %r, expected 'list', 'set' or 'dictionary'" % (kind,))
> >
> >Try starting with that and see how you go.
> >
> >Cheers,
> >Cameron Simpson <cs at zip.com.au>
> >-- 
> >https://mail.python.org/mailman/listinfo/python-list





the question again


>Create a function manipulate_data that does the following
>Accepts as the first parameter a string specifying the data structure to be >used "list", "set" or "dictionary"
>Accepts as the second parameter the data to be manipulated based on the data >structure specified e.g [1, 4, 9, 16, 25] for a list data structure
>Based off the first parameter

>    return the reverse of a list or
>    add items `"ANDELA"`, `"TIA"` and `"AFRICA"` to the set and return the >resulting set
>    return the keys of a dictionary.



unittest for the question



>import unittest
>class DataStructureTest(TestCase):
>  def setUp(self):
>    self.list_data = [1,2,3,4,5]
>    self.set_data = {"a", "b", "c", "d", "e"}
>    self.dictionary_data = {"apples": 23, "oranges": 15, "mangoes": 3, >"grapes": 45}
>    
>  def test_manipulate_list(self):
>    result = manipulate_data("list", self.list_data)
>    self.assertEqual(result, [5,4,3,2,1], msg = "List not manipulated >correctly")
>    
>  def test_manipulate_set(self):
>    result = manipulate_data("set", self.set_data)
>    self.assertEqual(result, {"a", "b", "c", "d", "e", "ANDELA", "TIA", >"AFRICA"}, msg = "Set not manipulated correctly")
>  
>  def test_manipulate_dictionary(self):
>    result = manipulate_data("dictionary", self.dictionary_data)
>    self.assertEqual(result, ["grapes", "mangoes", "apples", "oranges"], msg = >"Dictionary not manipulated correctly")



the code i have tested base on Cameron's code




def manipulate_data(kind, data):
   if kind == 'list':
    return list(data)[::-1]
 
   elif kind == 'set':
        return set(data)
   elif kind == 'dictionary':
     return dict( data)
     
manipulate_data("list", range(1,6))
a = manipulate_data("set", {"a", "b", "c", "d", "e"})
a.add("ANDELA")
a.add("TIA")
a.add("AFRICA")
b = manipulate_data("dictionary", {"apples": 23, "oranges": 15, "mangoes": 3, "grapes": 45})
list(b.keys()) 






this is the result i got from the unittest




Total Specs: 3 Total Failures: 2
1 .  test_manipulate_dictionary

Failure in line 23, in test_manipulate_dictionary self.assertEqual(result, ["grapes", "mangoes", "apples", "oranges"], msg = "Dictionary not manipulated correctly") AssertionError: Dictionary not manipulated correctly
2 .  test_manipulate_set

Failure in line 19, in test_manipulate_set self.assertEqual(result, {"a", "b", "c", "d", "e", "ANDELA", "TIA", "AFRICA"}, msg = "Set not manipulated correctly") AssertionError: Set not manipulated correctly 




i guess i passed the first requirement to return the reversed order of the list and believe i messed up when creating a set then add data to the set, also something is wrong with returning the keys of the dictionary


can someone point out the error in my code and the meaning of the unittes error?
thanks in advance



More information about the Python-list mailing list