diff --git a/main.py b/main.py index 379f286..74ac913 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,37 @@ def hello_world(): '''Prints "Hello World!".''' - return + print("Hello World") + 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 + s = 0 + for i in range(b): + s = sum(s,a) + return s 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 + count = 0 + while (a > 0): + a = sub(a,b) + count +=1 + return count def root(num): @@ -31,7 +39,14 @@ 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 is 0 or num is 1: + return num + a = 1 + result = 1 + while (result < num): + a+=1 + result = a + a + return a def main(): diff --git a/main2.py b/main2.py index 2f6ad21..1615e90 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 [] + list1 = [] + for x in nums: + if x%2 == 0: + list1 += [1] + else: + list1 += [0] + return list1 def mostOccurences(nums): @@ -10,7 +16,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 = 0 # answer to keep track of the key + maxCount = 0 # maxCount to keep track of max count we've seen in uniqueCount + for k in uniqueCount: + if uniqueCount[k] > maxCount: + answer = k # update answer + maxCount = uniqueCount[k] # update maxCount + + return answer + def main(): diff --git a/main3.py b/main3.py new file mode 100644 index 0000000..c5d0351 --- /dev/null +++ b/main3.py @@ -0,0 +1,98 @@ +#### INCLUDE ANY IMPORTS YOU NEED HERE, DO NOT PIP INSTALL ANY LIBRARIES #### +import math +import random +import datetime +from statistics import mean, median, mode +import csv +import string +def perfect_square(num): + '''Return the sqrt of only if is a perfect square, otherwise return -1.''' + # Hint: math library + number = math.sqrt(num) + if((num%math.sqrt(num)==0)): + return math.sqrt(num) + + return -1 + + + +def random_num_generator(min, max): + '''Returns a random number between min and max inclusive.''' + # Hint: random library + return random.randint(min,max) + + +def get_today(): + '''Returns today's date in the format , where month is a string, day & year are numbers.''' + # Note: Code must work regardless of today's date + # Example: November 19, 2021 + # Hint: datetime library + # Map of month to month + month = { + 1: "January", + 2: "Feburary", + # ... + 10: "October", + 11: "November", + 12: "December", + } + # Gets date in the format -- + today = datetime.datetime.now().strftime("%F") + # Split the formatted date by the separate '-' + date_obj = today.split('-') + + return month[int(date_obj[1])] + " " + date_obj[2] + ", " + date_obj[0] + + + +def get_stat(nums, type): + '''Returns of an unsorted list , where type can be mean, median, mode.''' + # Example: get_stat([0, 1, 2], "median"), returns 1 + # Hint: statistics library + if type == "mean": + return mean(nums) + elif type == "median": + return median(nums) + else: # type is mode + return mode(nums) + + +def print_by_profit(): + '''Print data/sales_records.csv in order sorted by profit.''' + # Hint: csv library + # Hint: use built-in sort() after parsing csv + # Hint: figure out how to print out each row in csv first + with open('data/sales_records.csv') as file: + # Gets all rows in csv file and put into 2d array + data_rows = list(csv.reader(file, delimiter=',')) + + # Delete first row because it's just the column titles + print(data_rows.pop(0)) + + # Sort by profit (last index of each row) + data_rows.sort(key=lambda i:float(i[13])) + + # Print each row + for row in data_rows: + print(row) + + +def main(): + '''Challenge 3 focuses on using some of Python's built-in functions and libraries.''' + # Testing challenge 3 + print("\nTesting perfect_square") + print("EXPECTED:", -1, "\nACTUAL:", perfect_square(24)) + print("EXPECTED:", 5, "\nACTUAL:", perfect_square(25)) + print("\nTesting random_num_generator") + print("EXPECTED:", "[1, 10]", "\nACTUAL:", random_num_generator(1, 10)) + print("\nTesting get_today") + print("EXPECTED:", "October 26, 2021", "\nACTUAL:", get_today()) + print("\nTesting get_stat") + print("EXPECTED:", 1, "\nACTUAL:", get_stat([0, 1, 2], "median")) + print("\nTesting print_by_profit") + print_by_profit() + # Add any additional test cases if needed + + +if __name__ == "__main__": + main() \ No newline at end of file