Home > Net >  Need solution: Return Smallest Key
Need solution: Return Smallest Key

Time:03-05

Hi I am trying to find the solution to this python problem as simply as possible without using advanced python (not using any lamda function or so).

You are given an input dictionary with keys as strings, and values as integers. Complete a function that returns the key with the nth smallest value. If the solution involves two keys that have the same value, return the the key that is lexicographically earliest. If n is larger than the number of distinct keys or equal to 0, then return null.

Signature String returnSmallestKey(HashMap<String, Integer> inputDict, int n)

Input

inputDict: Map with a string as the key and integer as the value n: Integer representing the nth smallest value to be returned

Output

string representing the key

Examples

inputDict: {"laptop": 999,"smartphone": 999,"smart tv": 500,"smart watch": 300,"smart home": 9999999}
n: 2 
output: "smart tv" 

inputDict: {"a": 10,"b": 20}
n: 0
output: null

inputDict: {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5}
n: 6 
output: null

inputDict: {"a": 10,"b": 20,"c": 3,"d": 2,"e": 9}
n: 1
output: "d"
import math
    # Add any extra import statements you may need here
    # Add any helper functions you may need here
    
    def return_smallest_key(inputDict, n):
      # Write your code here
      
      return 0
      
    
    
    
    
    
    
    
    
    
    
    
#Test Cases
    
    def printValue(n):
      print('[', n, ']', sep='', end='')
    
    test_case_number = 1
    
    def check(expected, output):
      global test_case_number
      result = False
      if expected == output:
        result = True
      rightTick = '\u2713'
      wrongTick = '\u2717'
      if result:
        print(rightTick, 'Test #', test_case_number, sep='')
      else:
        print(wrongTick, 'Test #', test_case_number, ': Expected ', sep='', end='')
        printValue(expected)
        print(' Your output: ', end='')
        printValue(output)
        print()
      test_case_number  = 1
    
    if __name__ == "__main__":
      
      # Testcase 1 
      inputDict1 = {"laptop": 999,"smartphone": 999,"smart tv": 500,"smart watch": 300,"smart home": 9999999}
      n1 = 2
      expected_1 = "smart tv"
      output_1 = return_smallest_key(inputDict1, n1)
      check(expected_1, output_1)
      
      # Testcase 2 
      inputDict2 = {"a": 10,"b": 20}
      n2 = 0
      expected_2 = None
      output_2 = return_smallest_key(inputDict2, n2)
      check(expected_2, output_2)
      
      # Testcase 3 
      inputDict3 = {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5}
      n3 = 6 
      expected_3 = None 
      output_3 = return_smallest_key(inputDict3, n3)
      check(expected_3, output_3)
    
      # Testcase 4
      inputDict4 =  {"a": 10,"b": 20,"c": 3,"d": 2,"e": 9}
      n4 = 1 
      expected_4 = "d" 
      output_4 = return_smallest_key(inputDict4, n4)
      check(expected_4, output_4)
    
      # Add your own test casesstrong text here
      

CodePudding user response:

def return_smallest_key(inputDict, n):
    _keys = list(inputDict.keys())
    _vals = list(inputDict.values())
    min = list(sorted(_vals))[n-1] # smallest value in lexicographic order
    return _keys[_vals.index(min)] 
print(return_smallest_key(
    {'a':1, 'b':1, 'c':2, 'd':3}, # first smallest value is 1 (key is a)
    1
)) 

this will do the trick
if you wanted a more compact and one-line return you could do this:

def return_smallest_key(inputDict, n):
    return list(inputDict.keys())[list(inputDict.values()).index(list(sorted(list(inputDict.values())))[n-1])] 

which would work the same way

CodePudding user response:

Is this all that you need?

def return_smallest_key(input_dict: dict, n: int) -> str | None:
    smallest = None

    if n <= len(input_dict) and n > 0:
        input_dict = dict(sorted(input_dict.items()))

        for _ in range(n):
            smallest = min(input_dict, key=input_dict.get)
            del input_dict[smallest]

    return smallest
  • Related