Question

In: Computer Science

#Write a function called clean_data. clean_data takes one #parameter, a dictionary. The dictionary represents the #observed...

#Write a function called clean_data. clean_data takes one
#parameter, a dictionary. The dictionary represents the
#observed rainfall in inches on a particular calendar day
#at a particular location. However, the data has some
#errors.
#
#clean_data should delete any key-value pair where the value
#has any of the following issues:
#
# - the type is not an integer or a float. Even if the value
# is a string that could be converted to an integer (e.g.
# "5") it should be deleted.
# - the value is less than 0: it's impossible to have a
# negative rainfall number, so this must be bad data.
# - the value is greater than 100: the world record for
# rainfall in a day was 71.8 inches
#
#Return the dictionary when you're done making your changes.
#
#Remember, the keyword del deletes items from lists
#and dictionaries. For example, to remove the key "key!" from
#the dictionary my_dict, you would write: del my_dict["key!"]
#Or, if the key was the variable my_key, you would write:
#del my_dict[my_key]
#
#Hint: If you try to delete items from the dictionary while
#looping through the dictionary, you'll run into problems!
#We should never change the number if items in a list or
#dictionary while looping through those items. Think about
#what you could do to keep track of which keys should be
#deleted so you can delete them after the loop is done.
#
#Hint 2: To check if a variable is an integer, use
#type(the_variable) == int. To check if a variable is a float,
#use type(the_variable) == float.


#Write your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print (although the order of the keys may vary):
#{"20190101": 5, "20190103": 7.5, "20190104": 0, "20190107": 1}
rainfall = {"20190101": 5, "20190102": "6", "20190103": 7.5,
"20190104": 0, "20190105": -7, "20190106": 102,
"20190107": 1}
print(clean_data(rainfall))

Solutions

Expert Solution

# Write a function called clean_data. clean_data takes one
# parameter, a dictionary. The dictionary represents the
# observed rainfall in inches on a particular calendar day
# at a particular location. However, the data has some
# errors.
#
# clean_data should delete any key-value pair where the value
# has any of the following issues:
#
# - the type is not an integer or a float. Even if the value
# is a string that could be converted to an integer (e.g.
# "5") it should be deleted.
# - the value is less than 0: it's impossible to have a
# negative rainfall number, so this must be bad data.
# - the value is greater than 100: the world record for
# rainfall in a day was 71.8 inches
#
# Return the dictionary when you're done making your changes.
#
# Remember, the keyword del deletes items from lists
# and dictionaries. For example, to remove the key "key!" from
# the dictionary my_dict, you would write: del my_dict["key!"]
# Or, if the key was the variable my_key, you would write:
# del my_dict[my_key]
#
# Hint: If you try to delete items from the dictionary while
# looping through the dictionary, you'll run into problems!
# We should never change the number if items in a list or
# dictionary while looping through those items. Think about
# what you could do to keep track of which keys should be
# deleted so you can delete them after the loop is done.
#
# Hint 2: To check if a variable is an integer, use
# type(the_variable) == int. To check if a variable is a float,
# use type(the_variable) == float.


def clean_data(rain):
    keys = list(rain.keys())
    for key in keys:
        if (type(rain[key]) != int and type(rain[key]) != float) or rain[key] < 0 or rain[key] > 100:
            del rain[key]
    return rain


# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
# {"20190101": 5, "20190103": 7.5, "20190104": 0, "20190107": 1}
rainfall = {"20190101": 5, "20190102": "6", "20190103": 7.5,
            "20190104": 0, "20190105": -7, "20190106": 102,
            "20190107": 1}
print(clean_data(rainfall))


Related Solutions

In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
\ Implement function find_key that takes a dictionary with int:int pairs and a second parameter which...
\ Implement function find_key that takes a dictionary with int:int pairs and a second parameter which is an int that corresponds to one of the dictionary values, and returns the key that corresponds to this value. If there are more than one keys, it returns the smallest key. If there is no key that maps to this value, the functions returns False. You may not use the .values and .items methods. You may not create lists, tuples, strings, sets, dictionaries....
(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a...
(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a double, and it prints this parameter out formatted like a dollar amount with $ and exactly 2 digits to the right of the decimal. Write a driver that declares an array of monetary amounts like this: double amounts[MAX_AMOUNTS]; and uses a while or do while loop to ask the user for monetary amounts with -1 for the amount as a sentinel value to put...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
(Python) I want to use a function called level() that takes a dictionary. Here is a...
(Python) I want to use a function called level() that takes a dictionary. Here is a dictionary with people's job and skill level. dict1 = {'Jame': {'Cleaning': 5, 'Tutoring': 2, 'Baking': 1},Pam': {'Plumbing': 2, 'Cleaning': 5}) like if I called level(dict1), the output will return {'Pam', 'James'} It finds the people's average skill level like for Pam is (2+5)/2=3.5 and sorted descending. How do I do that and how do I do it in only one return statement(using comprehension or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT