0

Please check my code below and make necessary corrections. It is giving me an error message.

The assignment is shown below.

Create a function manipulate_data that does the following

  1. Accepts as the first parameter a string specifying the data structure to be used list, set or dictionary

  2. 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

  3. 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.

My solution code is below:

def manipulate_data(argument1, argument2):
   if argument1 == "list":
     argument2.reverse()
return argument2

if argument1 == "set":
   argument2.add("ANDELA")
   argument2.add("TIA")
   argument2.add("AFRICA")
return argument2

if argument1 == "dictionary":
   argument2.keys()
return argument2

I don't know if I am making sense at all. The error messages received are:

Total Specs: 3 Total Failures: 2

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

and

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Zubyemex
  • 1
  • 1
  • 2
  • Possible duplicate [of this question](https://stackoverflow.com/questions/34801231/someone-please-take-a-look-at-my-python-syntax). – halfer Jun 22 '16 at 13:25

3 Answers3

1

You are on the right track but i think you have passed arguments incorrectly to your function in case of dict and set because by default {} signifies a dictionary

Something like this:

def manipulate_data(argument1, argument2):
    if argument1 == "list":
        argument2.reverse()
        return argument2

    if argument1 == "set":
        argument2.update(["ANDELA", "TIA", "AFRICA"])
        return argument2

    if argument1 == "dictionary":
        return argument2.keys()

print(manipulate_data('list', [1,2,3,4,5,6]), manipulate_data('set', {1,2,3,4,5,6}), manipulate_data('dictionary', {1:2,3:4,5:6}), sep='\n')

Output:

$ python3 a.py 
[6, 5, 4, 3, 2, 1]
{'ANDELA', 1, 2, 3, 4, 5, 6, 'TIA', 'AFRICA'}
dict_keys([1, 3, 5])
$
riteshtch
  • 8,629
  • 4
  • 25
  • 38
0
def manipulate_data(argument1, argument2):
    if argument1 == "list":
        if isinstance(argument2, list):
            argument2.reverse()
            return argument2

    if argument1 == "set":
        if isinstance(argument2, set):
            argument2.add("ANDELA")
            argument2.add("TIA")
            argument2.add("AFRICA")
            return argument2

    if argument1 == "dictionary":
        if isinstance(argument2, dict):
            return argument2.keys()

l = [1,2,3]
d = {'a':1, 'b':2, 'c':3 }
s = set()
s.add(1)
s.add(2)
s.add(3)

print manipulate_data("list", l)
print manipulate_data("dictionary", d)
print manipulate_data("set", s)
marienbad
  • 1,461
  • 1
  • 9
  • 19
  • It would be helpful if you would explain in your answer what was wrong with the OP's code and what you changed to solve it. – zondo Apr 03 '16 at 05:49
  • 1
    I am already doing his/her homework for him/her, mate. And frankly, the code is pretty self explanatory. – marienbad Apr 03 '16 at 05:51
  • If the code is self-explanatory, it should be easy to explain. If you don't think the question is suitable to be answered correctly, flag it to be closed. An answer has some responsibility with it. – zondo Apr 03 '16 at 05:54
  • Thanks marienbad. Your code has answered my question. – Zubyemex Apr 03 '16 at 20:59
0
def manipulate_data(arg1, arg2):
    if arg1 == "list":
        arg2.reverse()
        return arg2,type(arg2)

    #http://stackoverflow.com/questions/15768757/how-to-construct-a-set-out-of-list-items
    if arg1 == "set":
        myset=set(arg2)
        myset.add("ANDELA")
        myset.add("TIA")
        myset.add("AFRICA")
        return myset,type(myset)

    if arg1 == "dictionary":
        mydict={}
        for l in arg2:
            mydict[l]=l
        return mydict.keys(),type(mydict)


print manipulate_data("list",[1, 4, 9, 16, 25])
print manipulate_data("set",[1, 4, 9, 16, 25])
print manipulate_data("dictionary",[1, 4, 9, 16, 25])

Output:

([25, 16, 9, 4, 1], <type 'list'>)
(set([1, 4, 9, 'AFRICA', 16, 25, 'TIA', 'ANDELA']), <type 'set'>)
([16, 1, 4, 25, 9], <type 'dict'>)
roadrunner66
  • 7,772
  • 4
  • 32
  • 38