From f6b898feae41fc2599fb20f8d1128c66804335ae Mon Sep 17 00:00:00 2001 From: Kaushik Date: Tue, 12 Oct 2021 16:41:36 -0400 Subject: [PATCH 1/4] Implement functions --- main.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 379f286..5ac0d53 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,28 @@ def hello_world(): - '''Prints "Hello World!".''' + print("Hello World!") return - +def asdkfj(): + return def sum(a, b): - '''Accepts 2 numbers as parameters, returns sum of a and b.''' - return 0 + return (a+b) def sub(a, b): - '''Accepts 2 numbers as parameters, returns subtraction of a and b.''' - return 0 + return (a-b) def product(a, b): '''Accepts 2 numbers as parameters, returns product of a and b.''' # CHALLENGE: use a for loop and your sum function to implement product - return 0 + return a*b def divide(a, b): '''Accepts 2 numbers as parameters, returns a divided by b.''' # only pass in numbers that are divisible for sake of implementation # CHALLENGE: use a while loop and your sub function to implement divide - return 0 + return a/b def root(num): @@ -50,4 +49,4 @@ def main(): # Add any additional test cases if needed if __name__ == "__main__": - main() \ No newline at end of file + main() From 87c5e2d7cbcc1e8176dda1cb061b4c066f05a598 Mon Sep 17 00:00:00 2001 From: Kaushik Date: Tue, 12 Oct 2021 20:29:28 -0400 Subject: [PATCH 2/4] Modify multiply --- main.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 5ac0d53..ad2cce2 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,11 @@ def sub(a, b): def product(a, b): '''Accepts 2 numbers as parameters, returns product of a and b.''' # CHALLENGE: use a for loop and your sum function to implement product - return a*b + sum = 0 + for i in range(a): + sum += b + return sum + def divide(a, b): @@ -30,7 +34,7 @@ def root(num): # only pass in numbers that are perfect squares for sake of implementation # leetcode easy # CHALLENGE: do not use any built-in Python functions - return 0; + return 0 def main(): From ac5dc4ce94d70df859f46f523370ac9b4701d597 Mon Sep 17 00:00:00 2001 From: Kaushik Date: Tue, 26 Oct 2021 19:03:33 -0400 Subject: [PATCH 3/4] Implement all functions --- main.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 5ac0d53..b8d305f 100644 --- a/main.py +++ b/main.py @@ -2,8 +2,6 @@ def hello_world(): print("Hello World!") return -def asdkfj(): - return def sum(a, b): return (a+b) @@ -15,14 +13,20 @@ def sub(a, b): def product(a, b): '''Accepts 2 numbers as parameters, returns product of a and b.''' # CHALLENGE: use a for loop and your sum function to implement product - return a*b + sum = 0 + for x in range(0, b): + sum += a + return sum def divide(a, b): '''Accepts 2 numbers as parameters, returns a divided by b.''' # only pass in numbers that are divisible for sake of implementation # CHALLENGE: use a while loop and your sub function to implement divide - return a/b + quotient = 0 + for x in range(a, 0, b * (-1)): + quotient += 1 + return quotient def root(num): @@ -30,7 +34,13 @@ def root(num): # only pass in numbers that are perfect squares for sake of implementation # leetcode easy # CHALLENGE: do not use any built-in Python functions - return 0; + if (num == 0 | num == 1): + return num + for x in range(2, num): + if (x*x >= num): + break + + return x def main(): From cbd63ff00f4302d736730c89a51208c316767524 Mon Sep 17 00:00:00 2001 From: Kaushik Date: Tue, 26 Oct 2021 19:33:46 -0400 Subject: [PATCH 4/4] Implement main2.py --- main2.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/main2.py b/main2.py index 2f6ad21..c6e2f76 100644 --- a/main2.py +++ b/main2.py @@ -2,7 +2,13 @@ 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 [] + output = [] + for x in nums: + if (x % 2 == 0): + output.append(1) + else: + output.append(0) + return output def mostOccurences(nums): @@ -10,14 +16,27 @@ 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 - + occurences = {} + for x in nums: + if (x in occurences): + occurences[x] += 1 + else: + occurences[x] = 1 + + number = -1 + maximum = 0 + for index in occurences: + if (occurences[index] > maximum): + number = index + maximum = occurences[index] + + return number 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("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