From 8d795dfbb3cb3f93c20b6627cbad06814e1d9bf3 Mon Sep 17 00:00:00 2001 From: akarnam Date: Tue, 25 Jan 2022 21:19:34 -0500 Subject: [PATCH] finished mains --- main.py | 28 +++++++++++++++++++++++----- main2.py | 29 +++++++++++++++++++++++++++-- main3.py | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 379f286..d31c6c1 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,40 @@ +from math import sqrt + + 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 + total = 0 + for i in range(b): + total = sum(total,a) + return total 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 + total = 0 + while a > 0: + a = sub(a,b) + total += 1 + return total def root(num): @@ -31,7 +42,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 + square = 1 + total = 1 + while total < num: + square += 1 + total = square*square + return square def main(): diff --git a/main2.py b/main2.py index 2f6ad21..1537e5d 100644 --- a/main2.py +++ b/main2.py @@ -2,7 +2,14 @@ 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 range(len(nums)): + if nums[i] % 2 == 0: + list.append(1) + else: + list.append(0) + + return list def mostOccurences(nums): @@ -10,7 +17,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 + temp = 1 + mode = 1 + first = nums[0] + second = first + i = 1 + for i in range(len(nums)): + if first == nums[i]: + temp += 1 + if temp > mode: + mode = temp + second = first + else: + if temp > mode: + mode = temp + second = first + temp = 1 + first = nums[i] + + return second def main(): diff --git a/main3.py b/main3.py index 1f7c89d..d1a32a3 100644 --- a/main3.py +++ b/main3.py @@ -1,16 +1,25 @@ #### INCLUDE ANY IMPORTS YOU NEED HERE, DO NOT PIP INSTALL ANY LIBRARIES #### - +from math import sqrt, floor +import random +import datetime +from statistics import mean, median, mode +import csv def perfect_square(num): '''Return the sqrt of only if is a perfect square, otherwise return -1.''' # Hint: math library - return 0 + total = sqrt(num) + if floor(total) * floor(total) == num: + return total + else: + return -1 def random_num_generator(min, max): '''Returns a random number between min and max inclusive.''' # Hint: random library - return 0 + randomNum = random.randint(min, max) + return randomNum def get_today(): @@ -18,14 +27,25 @@ def get_today(): # Note: Code must work regardless of today's date # Example: November 19, 2021 # Hint: datetime library - return "November 19, 2021" + current = datetime.datetime.now().strftime("%F") + month = {1: "January", 2: "February", 3: "March", 4: "April", + 5: "May", 6: "June", 7: "July", 8: "August", + 9: "September", 10: "October", 11: "November", 12: "December",} + today = current.split('-') + + return month[int(today[1])] + " " + today[2] + ", " + today[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 - return 0 + if type == "mean": + return mean(nums) + elif type == "median": + return median(nums) + else: + return mode(nums) def print_by_profit(): @@ -33,7 +53,11 @@ 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: + row = list(csv.reader(file, delimiter = ",")) + row.sort() + for i in row: + print(i) def main():