Write a recursive function that checks to see if a key is in a dictionary. This...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a recursive function that checks to see if a key is in a dictionary. This function should return a boolean indicating whether or not the key is present at any level of the dictionary. Think of dictionaries as being of these shapes: d1 = { "a": 1 } d2 = { "b": ( } } d3 = { J "a": "Test" "c": { 1 "z": ["'blue', 'green', 'red' Dictionaries can be nested, but there will only ever be 1 key at each level. The value of these keys can be anything, a string, a number, a list, another dictionary. Only if the value is a dictionary do you have to recursively check if the key exists in the nested dictionary. Use the following function header: isKeyInDict(key, obj) When your program is executed you should do the following: Use Python's Input function to execute your code. When your file is executed, prompt the user as follows: >> What would you like to do? Choose from "execute isKeyInDict" or "write to file" Based on the user's feedback, you will either output the result of isKeyInDict for a set of examples that you stored previously in variables. Make sure to test your function with inputs of multiple levels and some where the key is present and other where the key is not present. Helpful hints: Remember that the user may not type the commands ("execute isKeylnDict" or "write to file") exactly as you ask. While we can't predict what the user meant if their input is way off, there is at least 1 way we can help. Your code should account for any uppercase/lowercase lettering deviation from your instruction. For example, your code should be able to understand "EXECUte iskeyindict". If the user entered "write to file" Then your code should continue to ask the user a couple more questions: 1. What filename would the user like the file to have? 2. What message would the user like to write to the file? 3. If the filename provided by the user already exists, you should ask a 3rd question: Does the user wish to overwrite the contents of the existing file with the message given on number 2 or should the message simply be appended to existing contents of the file? If the user entered something else in the prompt, end the program with a message indicating that you can not understand the user's instruction. Write a recursive function that checks to see if a key is in a dictionary. This function should return a boolean indicating whether or not the key is present at any level of the dictionary. Think of dictionaries as being of these shapes: d1 = { "a": 1 } d2 = { "b": ( } } d3 = { J "a": "Test" "c": { 1 "z": ["'blue', 'green', 'red' Dictionaries can be nested, but there will only ever be 1 key at each level. The value of these keys can be anything, a string, a number, a list, another dictionary. Only if the value is a dictionary do you have to recursively check if the key exists in the nested dictionary. Use the following function header: isKeyInDict(key, obj) When your program is executed you should do the following: Use Python's Input function to execute your code. When your file is executed, prompt the user as follows: >> What would you like to do? Choose from "execute isKeyInDict" or "write to file" Based on the user's feedback, you will either output the result of isKeyInDict for a set of examples that you stored previously in variables. Make sure to test your function with inputs of multiple levels and some where the key is present and other where the key is not present. Helpful hints: Remember that the user may not type the commands ("execute isKeylnDict" or "write to file") exactly as you ask. While we can't predict what the user meant if their input is way off, there is at least 1 way we can help. Your code should account for any uppercase/lowercase lettering deviation from your instruction. For example, your code should be able to understand "EXECUte iskeyindict". If the user entered "write to file" Then your code should continue to ask the user a couple more questions: 1. What filename would the user like the file to have? 2. What message would the user like to write to the file? 3. If the filename provided by the user already exists, you should ask a 3rd question: Does the user wish to overwrite the contents of the existing file with the message given on number 2 or should the message simply be appended to existing contents of the file? If the user entered something else in the prompt, end the program with a message indicating that you can not understand the user's instruction.
Expert Answer:
Answer rating: 100% (QA)
Heres the code for the recursive function that checks if a key is in a dictionary def isKeyInDictkey ... View the full answer
Related Book For
Financial Accounting and Reporting a Global Perspective
ISBN: 978-1408076866
4th edition
Authors: Michel Lebas, Herve Stolowy, Yuan Ding
Posted Date:
Students also viewed these programming questions
-
Big Top Inc. reported net operating profit after tax (or unlevered net profit) of $260 million for the most recent fiscal year. The firm had depreciation expenses of $100 million, capital...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
The site http://virtualpiano.net features an online player piano. If you click on the Key Assist menu just above the piano keyboard, and then Keyboard Keys, you will see that each key is labelled...
-
choess answaer that How many bits are required to store one BCD digit?" 2 8 1 Kilo bits are equal to 1000 bits 1024 bits 1012 bits 1008 bits The function of the encoder is to convert coded...
-
Explain how you could have a virtual object.
-
Open the Custom Field area and choose Invoice. On the invoice form, open the Gear menu and click Add Custom Field. How would you add a custom field to an invoice form?
-
Do you think it is possible to have win-win negotiations? Why or why not?
-
Ajax Corporation has two divisions. The Mining Division makes toldine, which is then transferred to the Metals Division. The toldine is further processed by the Metals Division and sold to customers...
-
As of June 30, Year 1, the bank statement showed an ending balance of $16,878. The unadjusted Cash account balance was $15,239. The following information is available: 1. Deposit in transit, $2,190....
-
Solutions Plus is an industrial chemicals company that produces specialized cleaning fluids and solvents for a wide variety of applications. Solutions Plus just received an invitation to submit a bid...
-
The function has one parameter, a Turtle. After the function definition, call the function 7 times, preferably in a loop. import turtle import random wn = turtle.Screen() t = turtle.Turtle() x =...
-
In 2005, before the financial crisis, Timothy Geithner, who was then president of the Federal Reserve Bank of New York, thought that leverage at hedge funds was rising, probably because of heightened...
-
In late 2009, during the debate over the Dodd-Frank Act, a newspaper article noted: Last summer, the central bank hired an experienced Democratic hand and former lobbyist, Linda Robertson, to help...
-
An article in the Economist magazine says about investment banks: By unlocking the capital markets and helping firms to manage risks, investment banks are important conduits of credit. How do...
-
Are the high rates of inflation that the United States experienced during the 1970s consistent with the public interest view of the Feds motivation?
-
A bank analyst was quoted as saying: There are very real expenses to owning and operating ATMs. In the current environment, banks are unable to support the cost structure they have historically. a....
-
The expression (f(x+h)-f(x))/(h) for h!=0 is called the difference quotient. Find and simplify the difference quotient for the following function: f(x)=-7x^(2)+3x+10
-
Suppose that A is an m n matrix with linearly independent columns and the linear system LS(A, b) is consistent. Show that this system has a unique solution.
-
The Sibelius Company was incorporated on 1 March X1. It carries a commercial activity. The following transactions were undertaken during the first month of operation of Sibelius Co. 1 March 1...
-
Nokia (Finland) is still among the leaders in the telecommunication industry, with emphasis on cellular phones and other wireless solutions. The consolidated financial statements are prepared in...
-
The following are excerpts of the equity and liabilities section of the balance sheet of four companies (with Notes when applicable). China International Marine Containers (China IFRS Source:...
-
The government regularly publishes many different economic statistics, such as the unemployment rate, the inflation rate, and the surplus or deficit in the federal budget. Study recent newspapers and...
-
Prove that the dual function of Eq. (16.12) is concave Data From Equation (16.12) 19S See https://www.gams.com/ 20 See http://cvxr.com/
-
Consider the quadratic programming problem. min x+x s.t. 1x2 = 4. x1
Study smarter with the SolutionInn App