From f48e190a6f2641f6c6bae0f761a85cd682edf413 Mon Sep 17 00:00:00 2001 From: Daniel Diaz Date: Sun, 30 Jan 2022 19:38:31 -0500 Subject: [PATCH] completed python challenge --- main.py | 27 ++++++++++++++++++++++----- main2.py | 26 ++++++++++++++++++++++++-- main3.py | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 379f286..69fd821 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 + sumVal = 0 + for i in range(a): + sumVal = sum(sumVal, b) + return sumVal 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 = ans + 1 + return ans def root(num): @@ -31,7 +39,16 @@ 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 == 1 or num == 0: + return num + + sum = 1 + itr = 1 + while sum < num: + itr += 1 + sum = itr*itr + return itr def main(): diff --git a/main2.py b/main2.py index 2f6ad21..292908e 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 [] + list = [] + for i in nums: + if i % 2 == 0: + list.append(1) + else: + list.append(0) + return list def mostOccurences(nums): @@ -10,7 +16,23 @@ 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 + + dict_of_nums = {} + + for i in nums: + if i in dict_of_nums: + dict_of_nums[i] += 1 + else: + dict_of_nums[i] = 1 + + currMax = 0 + maxValue = -1 + for val in dict_of_nums: + if dict_of_nums[val] > currMax: + currMax = dict_of_nums[val] + maxValue = val + + return maxValue def main(): diff --git a/main3.py b/main3.py index 1f7c89d..6d2fda3 100644 --- a/main3.py +++ b/main3.py @@ -1,16 +1,26 @@ #### INCLUDE ANY IMPORTS YOU NEED HERE, DO NOT PIP INSTALL ANY LIBRARIES #### - +import math +import random +import datetime +import statistics +import csv def perfect_square(num): '''Return the sqrt of only if is a perfect square, otherwise return -1.''' # Hint: math library - return 0 + + if num >= 0: + x = int(math.sqrt(num)) + if (x*x == num): + return x + + return -1 def random_num_generator(min, max): '''Returns a random number between min and max inclusive.''' # Hint: random library - return 0 + return random.randint(min, max) def get_today(): @@ -18,14 +28,22 @@ def get_today(): # Note: Code must work regardless of today's date # Example: November 19, 2021 # Hint: datetime library - return "November 19, 2021" + x = datetime.datetime.now() + months = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", + 7:"July", 8:"August", 9:"September", 10:"October", 11:"November", 12:"December"} + return months[x.month] + " " + str(x.day) + ", " + str(x.year) 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 == "mode": + return statistics.mode(nums) + elif type == "mean": + return statistics.mean(nums) + else: + return statistics.median(nums) def print_by_profit(): @@ -33,7 +51,15 @@ 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 f: + rows = list(csv.reader(f, delimiter=',')) + + rows.pop(0) + rows.sort(key=lambda i:float(i[13])) + + for row in rows: + print(row) def main():