diff --git a/main.py b/main.py index 379f286..4c019ab 100644 --- a/main.py +++ b/main.py @@ -5,25 +5,28 @@ def hello_world(): 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 + product = 0 + for i in range(a): + product = product + b + return product 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): @@ -31,7 +34,12 @@ 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; + i = 0 + for i in range(1000000000): + if i*i == num: + break + ++i; + return i def main(): diff --git a/main2.py b/main2.py new file mode 100644 index 0000000..497be38 --- /dev/null +++ b/main2.py @@ -0,0 +1,45 @@ +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] + returnList = [] + for item in nums: + if item % 2 == 0: + returnList.append(1) + else: + returnList.append(0) + return returnList + + +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 + map = {} + for item in nums: + if item in map: + map[item] += 1 + else: + map[item] = 1 + + currentMax = list(map.keys())[0] + for key in map: + if map[key] > map[currentMax]: + currentMax = key + return currentMax + + +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]))) + print("EXPECTED:", 4, "\nACTUAL:", mostOccurences([2, 4, 5, 7, 4, 10])) + print("EXPECTED:", 2, "\nACTUAL:", mostOccurences([2, 2, 5, 2, 2, 10])) + # Add any additional test cases if needed + +if __name__ == "__main__": + main() \ No newline at end of file