Write a recursive function unPackNums(objectToUnpack, destinationList):

This function is designed to have a Python object ( objectToUnpack, , perhaps a highly nested object) passed to it. It is to find all the numbers in this nested object (integers, floats, or strings that can be interpreted as either of the above). The numbers to be found may also exist as keys or values in a dictionary. As the numbers are found, they are appended to the destinationList . The following test code (available on D2L) should be included in your program file. Your program will also be tested by importing it into another program, so it is essential that your recursive function is named correctly.

Plan to use type() in your function to determine the type of each object and you will also find it necessary to use try/except . Use recursion to process nested objects.

Your program should be able to correctly handle int, float, list, tuple, str and dict types.

def unPackNums(objectToUnpack,destinationList):
###### Your code goes here. ######
if __name__ == "__main__":
class Dummy(): # This is here to ensure that your program can successfully ignore other types
def __repr__(self):
return "Dummy object"
d = Dummy()
a = [3,[4,[2,3,9.9,6,(12,13,14),[7],[],d,{4:2,"4":3,"no":"way"},10,"23.4"]],"-75",5]
print(a) # Print the original challenging object
b = [1,2,3] # Create an initial output list
print(b) # and print it.
unPackNums(a,b) # Append the “numbers” found in a to the list b
print(b) # Show the output of your processing
unPackNums(99,b)# Make sure that it also works when passed just a number
print(b) # and print the result

Output from this above test yields the following. Note that the Got other type: lines are printed inside your recursive function. The other lines are printed by the test code. As usual, the order of items in a dictionary may vary.

[3, [4, [2, 3, 9.9, 6, (12, 13, 14), [7], [], Dummy object, {'no': 'way', 4: 2, '4': 3}, 10, '23.4']], '-75', 5]
[1, 2, 3]
Got other type: Dummy object
Got other type: no
Got other type: way
[1, 2, 3, 3, 4, 2, 3, 9.9, 6, 12, 13, 14, 7, 4, 2, 4, 3, 10, 23.4, -75.0, 5]

[1, 2, 3, 3, 4, 2, 3, 9.9, 6, 12, 13, 14, 7, 4, 2, 4, 3, 10, 23.4, -75.0, 5, 99]
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.