-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReturn Nth Smallest Key.py
91 lines (66 loc) · 1.92 KB
/
Return Nth Smallest Key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
if n == 0 or n > len(inputDict):
return None
else:
values = []
letters = []
for i in inputDict:
values.append(inputDict[i])
values.sort()
value = values[n-1]
for i in inputDict:
if value == inputDict[i]:
letters.append(i)
letters.sort()
return letters[0]
# These are the tests we use to determine if the solution is correct.
# You can add your own at the bottom.
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 cases here