diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..c240367 Binary files /dev/null and b/.DS_Store differ diff --git a/data/sales_records.csv b/data/sales_records.csv old mode 100644 new mode 100755 diff --git a/main.py b/main.py index 379f286..cea5535 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,37 @@ def hello_world(): '''Prints "Hello World!".''' + 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 + ans = 0 + for i in range(b): + ans = sum(ans, a) + return ans 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 + ans = 0 + while (a > 0): + a = sub(a, b) + ans += 1 + return ans 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 == 0 or num == 1: + return num + ans = 1 + result = 1 + while (result < num): + ans += 1 + result = ans * ans + return ans; def main(): diff --git a/main2.py b/main2.py index 2f6ad21..69e9d7d 100644 --- a/main2.py +++ b/main2.py @@ -2,7 +2,7 @@ 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 [] + return [(i + 1) % 2 for i in nums] def mostOccurences(nums): @@ -10,7 +10,18 @@ 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 + mp = {} + for k in nums: + if k in mp: + mp[k] += 1 + else: + mp[k] = 1 + max, ans = -1, 0 + for i in mp: + if max < mp[i]: + max = mp[i] + ans = i + return ans def main(): diff --git a/main3.py b/main3.py index 1f7c89d..31dcd48 100644 --- a/main3.py +++ b/main3.py @@ -1,16 +1,23 @@ #### INCLUDE ANY IMPORTS YOU NEED HERE, DO NOT PIP INSTALL ANY LIBRARIES #### +import statistics as stats +from math import sqrt +from random import randrange +from datetime import date +import csv def perfect_square(num): '''Return the sqrt of only if is a perfect square, otherwise return -1.''' # Hint: math library - return 0 + square_root = sqrt(num) + if int(square_root + 0.5) ** 2 == num: + return int(square_root) + return -1 def random_num_generator(min, max): '''Returns a random number between min and max inclusive.''' - # Hint: random library - return 0 + return randrange(min - 1, max) def get_today(): @@ -18,14 +25,19 @@ def get_today(): # Note: Code must work regardless of today's date # Example: November 19, 2021 # Hint: datetime library - return "November 19, 2021" + today = date.today() + return today.strftime("%B %d, %Y") 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 - return 0 + if type == "mean": + return stats.mean(nums) + elif type == "median": + return stats.median(nums) + return stats.mode(nums) def print_by_profit(): @@ -33,7 +45,16 @@ def print_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 - print("Working with csvs!") + 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(): diff --git a/solution.py b/solution.py old mode 100644 new mode 100755 diff --git a/solution2.py b/solution2.py old mode 100644 new mode 100755