Question

In: Computer Science

\ 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. You maynot use functions min, max, sorted.

Examples:

find_key({5:7, 6:3, 2:1, 3:4}, 4) ->    3

find_key({1:9, 7:4, 6:3, 2:1, 8:4}, 4)    ->    7

find_key({9:5, 6:3, 2:1, 3:3}, 4)       ->    False

True

False

In python Please

Solutions

Expert Solution

def find_key(a,b):
c=0
m=0
  
for i in a:
if a[i]==b:
m=i
break
  
for i in a:
if a[i]==b:
if i<m:
m=i   
else:
c+=1


if c==len(a):
return False
else:
return m

if __name__=='__main__':
print(find_key({5:7, 6:3, 2:1, 3:4}, 4))
print(find_key({1:9, 7:4, 6:3, 2:1, 8:4}, 4))
print(find_key({9:5, 6:3, 2:1, 3:3}, 4))

Explanation -

def find_key(a,b):
c=0
m=0

Define a function named find_key which accepts 2 parameters a which is a dictionary and b is an integer which corresponds to one of the dictionary values.

Also define variables m and c , both initialized to 0 which will be used later in the program.


for i in a:
if a[i]==b:
m=i
break

Using for loop iterate through every key in a and check if value of that corresponding key is equal to b . if yes then

m=i and exit the loop.It means we have found our first matched key

for i in a:
if a[i]==b:
if i<m:
m=i   
else:
c+=1

Again using for loop iterate through every key in a and check if value of that corresponding key is equal to b . if yes then check if its less than m which was computed in the above step i.e m = first found matched key(i)

if it is less than store this key in m variable else keep m as it is,

Also , while iterating through every key in a , if value of that corresponding key is NOT equal to b .Increment c by 1.


if c==len(a):
return False
else:
return m

Now check if c is equal to length of dictionary a . if it is equal it means none of the key value matched with b.

if c is not equal to length of dictionary a then the function will return m which is nothing but the key of the dictionary whose value matched with b and is smallest among all keys whose value matched with b.

if __name__=='__main__':
print(find_key({5:7, 6:3, 2:1, 3:4}, 4))
print(find_key({1:9, 7:4, 6:3, 2:1, 8:4}, 4))
print(find_key({9:5, 6:3, 2:1, 3:3}, 4))

Finally in main , call the function by passing the arguments as shown above.


Related Solutions

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
Design and implement a function which has one input parameter which is a number which is...
Design and implement a function which has one input parameter which is a number which is greater than 50, called num. Then the function will create a dictionary whose keys are 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9. Then the function calculates the values for each of the above keys. The value for a key is all the numbers between 2 and input “num” that are divisible by the key. The function...
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
Array & Function - Can I pass a single parameter to a function that takes two...
Array & Function - Can I pass a single parameter to a function that takes two arguments: data passed from: arrayOne(4); to function: string function (array[], int)
Write a function which takes one parameter int num, and prints out a countdown timer with...
Write a function which takes one parameter int num, and prints out a countdown timer with minutes and seconds separated by a colon (:). It should print out one line for each second elapsed and then pause one second before printing out the next line. A few things to note: - You can assume that calling the function usleep(1000000) makes the program pause for one second - It should count down from num minutes:zero seconds to zero minutes:zero seconds -...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT