From fd3782339a2acd762d2e4f781172b01f04fb3731 Mon Sep 17 00:00:00 2001 From: j0nathanchuang Date: Mon, 11 Oct 2021 19:38:32 -0400 Subject: [PATCH 1/2] Copied over main2.py to solution2.py --- solution2.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solution2.py diff --git a/solution2.py b/solution2.py new file mode 100644 index 0000000..2f6ad21 --- /dev/null +++ b/solution2.py @@ -0,0 +1,26 @@ +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] + return [] + + +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 + return -1 + + +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() \ No newline at end of file From a8906c40640a89ace26db5c4ae7f6c07f4aa2b95 Mon Sep 17 00:00:00 2001 From: j0nathanchuang Date: Mon, 11 Oct 2021 19:54:43 -0400 Subject: [PATCH 2/2] Solution #2 --- solution2.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/solution2.py b/solution2.py index 2f6ad21..0f1da1e 100644 --- a/solution2.py +++ b/solution2.py @@ -2,7 +2,24 @@ 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] - return [] + + 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): @@ -10,7 +27,25 @@ def mostOccurences(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 - return -1 + + 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():