-
Notifications
You must be signed in to change notification settings - Fork 32
/
final-prob4.py
84 lines (66 loc) · 2.32 KB
/
final-prob4.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
# final-prob4.py
#
# Final Exam, Problem 4
#
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
"""
You are given a dictionary aDict that maps integer keys to integer values.
Write a Python function that returns a list of keys in aDict that map to
dictionary values that appear exactly once in aDict.
This function takes in a dictionary and returns a list.
Return the list of keys in increasing order.
If aDict does not contain any values appearing exactly once, return an empty list.
If aDict is empty, return an empty list.
Hint: You may want to create another dictionary as an intermediate data structure.
For example:
If aDict = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0} then your function should return [1, 3, 8]
If aDict = {1: 1, 2: 1, 3: 1} then your function should return []
"""
# Sample values for aDict
# Remove before submitting
# Program should return [1,3, 8]
# aDict = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0}
# Program should return []
# aDict = {1: 1, 2: 1, 3: 1}
# Program should Return [1, 5]
aDict = {0: 4, 9: 4, 3: 4, 5: 2, 1: 1}
def uniqueValues(aDict):
'''
aDict: a dictionary
returns: a sorted list of keys that map to unique aDict values, empty list if none
'''
# Your code here
# initialize a list, uniqList
uniqList = []
# set uniqList to all values from aDict
for theKey, theValue in aDict.iteritems():
uniqList.append(theValue)
print uniqList
# Remove duplicates from uniqList
uniqList = list(set(uniqList))
print uniqList
for theKey, theValue in aDict.iteritems():
print theValue
if theValue in uniqList:
print("In if")
# if key theValue exists in countDict, increment it's value
uniqList.remove(theValue)
print uniqList
else:
print("In else")
# Otherwise, add theValue to countDict with value of 1
uniqList.append(theValue)
print uniqList
# countDict contains the values that appear only once
print uniqList
# create a list that contains the associated keys for the values
# for key, value in countDict.iteritems():
# if value == 1:
# uniqList.append(key)
# sort the list
uniqList.sort()
# print uniqList
return uniqList
# Remove before submitting
uniqueValues(aDict)