Please Help with this Python program

homework
python

#1

This is the instruction:
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.

AND THIS IS WHAT I HAVE WRITTEN
def manipulate_list(first = "list", data =[1, 4, 9, 16, 25]):
data.reverse()
return first, data

def manipulate_set(first = "set", data ={"a", "b", "c", "d", "e"}):
items = {"ANDELA","TIA", "AFRICA"}
data.update(items)
return first, data

def manipulate_dictionary(first = "dictionary", data ={"a":1, "b":4, "c":9, "d":16, "e":25}):
keys = set(data)
return first, keys

IS MY CODE CORRECT? AND HOW CAN I LINK IT TO THE PROGRAM IN THE UPLOADED PICTURE


Urgent guru help need please
#2

I do not think you understand what you are trying to do.

What you have listed.

  • Take a parameter specifying what data type is being passed
  • Takes a second parameter the data to be manipulated
  • Return the reverse of the list
  • Add "ANDELA", "TIA" and "AFRICA" to a set
  • Return the keys of a dict

Ok so let's do that then,

def manipulate_data(data_type=None, data=None):
    if data_type is 'list':
        return data[-1:: -1]
    if data_type == 'set':
        return set.union(data, ["ANDELA", "TIA", "AFRICA"])
    if data_type == 'dict':
        return [key for key, item in data.items()]

The above function does what you pointed out that you wanted to accomplish.

If you want to add error checking to this it would be simple,

Code: with error checking

def manipulate_data(data_type=None, data=None):
    try:
        if data_type is 'list':
            return data[-1:: -1]
        if data_type == 'set':
            return set.union(data, ["ANDELA", "TIA", "AFRICA"])
        if data_type == 'dict':
            return [key for key, item in data.items()]
        else:
            raise TypeError
    except TypeError as e:
        print("\nYou failed to pass the correct data type.\n%s\n" % e)

Or we can do some fancy stuff like this, so we can expand out accepted data types easy as pie.

Code: with ability to easily add data types

def manipulate_data(data_type=None, data=None):
    accepted_data_types = {
        'list': 'data[-1:: -1]',
        'set': 'set.union(data, ["ANDELA", "TIA", "AFRICA"])',
        'dict': '[key for key, item in data.items()]'
    }
    try:
        return eval(accepted_data_types['%s' % data_type])
    except KeyError as e:
        print("\nYou failed to pass the correct data type.\n%s\n" % e)

Now we have a function that we can build on easily.

If this is not what you need feel free to add you what you have so that we can provide some assistance.


I can't seem to understand what my mistake could be on this code
Urgent guru help need please
#3

Thank you verymuch Zeziba...you lead me to the very right path.
The only thing i adjusted was 'dict' to 'dictionary'
and keys instead of key...according to the spec i was asked for.
and boom it worked.

thanks once again


#4

So is this how thw code should look like

def manipulate_list(first = "list", data =[1, 4, 9, 16, 25]):
data.reverse()
return first, data

    def manipulate_data(data_type=None, data=None):
        if data_type is 'list':
            return data[-1:: -1]
        if data_type == 'set':
            return set.union(data, ["ANDELA", "TIA", "AFRICA"])
        if data_type == 'dictionary':
            return [keys for keys, item in data.items()]

    def manipulate_dictionary(first = "dictionary", data ={"a":1, "b":4, "c":9, "d":16, "e":25}):
    keys = set(data)
    return first, keys

#5

thank you so much ZEZIBA I finally found the answer the problem is i didn't understand the two parameters defined in our function.
and the other problem was returning a function definition to solve the set problem thanks so much


#6