Question

Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format()...

Rules:

You may NOT use the following built-in functions.

sort()

sum()

max()

min()

Except for format()

all string methods are banned. Except for append() and extend()

all list methods are banned. You may not import any module.

Make sure to test your code with a variety of inputs. Do not assume the examples in these directions are reflective of the hidden test cases.

part 5 Key to Min

Write a function called get_key_to_min that takes a dictionary and returns the key of the smallest value in the dictionary that is greater than or equal to 10.

About the values in the given dictionary, you may assume:

1.They are all integers that do not exceed 1000.

2.They are all unique (i.e. different from each other).

Here are some examples of how your program should behave on Python IDLE.

>>> get_key_to_min({'a':5,'b':8,'abc':13,'def':18,'xyz':10})

'xyz'

>>> get_key_to_min({'a':5,'b':8,'abc':13,'def':18,'xyz':9})

'abc'

>>> get_key_to_min({'a':12,'b':8,'abc':13,'def':18,'xyz':9})

'a'

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the function. I have also give the code to test the function.
PLEASE MAKE SURE INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.


def get_key_to_min(dic):
   min_val = None
   key_to_min = None
   for k in dic:
       if dic[k] >= 10 and (min_val == None or dic[k] < min_val):
           min_val = dic[k]
           key_to_min = k

   return key_to_min


#testing the function
print( get_key_to_min({'a':5,'b':8,'abc':13,'def':18,'xyz':10}))
print(get_key_to_min({'a':5,'b':8,'abc':13,'def':18,'xyz':9}))
print( get_key_to_min({'a':12,'b':8,'abc':13,'def':18,'xyz':9}))

output
---
xyz
abc
a

Add a comment
Know the answer?
Add Answer to:
Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT