-
Notifications
You must be signed in to change notification settings - Fork 2
/
solution2.py
61 lines (48 loc) · 1.99 KB
/
solution2.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
def oddOrEven(nums):
'''Given an unsorted list of numbers, return a list that indicates if the value at each index is odd (0) or even (1).'''
# EXAMPLE:
# Given [2, 4, 5, 7, 8, 10], return [1, 1, 0, 0, 1, 1]
solution = []
# Push into solution as we determine if num is even or odd
for num in nums:
if num % 2 == 0:
solution.append(1)
else:
solution.append(0)
# Likewise, can also just replace the indexes of nums
# for i, num in enumerate(nums):
# if num % 2 == 0:
# nums[i] = 1
# else:
# nums[i] = 0
return solution # return nums if use second solution
def mostOccurences(nums):
'''Given an unsorted list of numbers, returns the value that occured the most in nums.'''
# Hint: use oddOrEven to test function faster
# Hint: use a map
# Hint: https://stackoverflow.com/questions/13098638/how-to-iterate-over-the-elements-of-a-map-in-python
uniqueCount = {}
for num in nums:
# Check if key already exists, if so, just add one to count
if num in uniqueCount:
uniqueCount[num] += 1
# Key doesn't exist, set count to 1
else:
uniqueCount[num] = 1
answer = -1 # answer to keep track of the key
maxCount = 0 # maxCount to keep track of max count we've seen in uniqueCount
for key in uniqueCount:
if uniqueCount[key] > maxCount:
answer = key # update answer
maxCount = uniqueCount[key] # update maxCount
return answer
def main():
'''The main function is where you will test all of your functions.'''
# Testing challenge 2
print("\nTesting oddOrEven")
print("EXPECTED:", [1, 1, 0, 0, 1, 1], "\nACTUAL:", oddOrEven([2, 4, 5, 7, 8, 10]))
print("\nTesting mostOccurences")
print("EXPECTED:", 1, "\nACTUAL:", mostOccurences(oddOrEven([2, 4, 5, 7, 8, 10])))
# Add any additional test cases if needed
if __name__ == "__main__":
main()