From dc5551ef7f65858129b72a4f3605ee40f04162b8 Mon Sep 17 00:00:00 2001 From: jskulnik Date: Tue, 12 Oct 2021 20:32:49 -0400 Subject: [PATCH 1/4] tutorial 1 --- main.py | 27 +++++++++++++++++----- tutorial1.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 tutorial1.py 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/tutorial1.py b/tutorial1.py new file mode 100644 index 0000000..c5c217a --- /dev/null +++ b/tutorial1.py @@ -0,0 +1,63 @@ +def hello_world(): + '''Prints "Hello World!".''' + return print("Hello World") + + +def sum(a, b): + '''Accepts 2 numbers as parameters, returns sum of a and b.''' + return a + b + + +def sub(a, b): + '''Accepts 2 numbers as parameters, returns subtraction of a and b.''' + 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 + p = 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 + count = 0 + while (a != 0): + a = sub(a,b) + count +=1 + return count + + +def root(num): + '''Accepts a number as a parameter, returns the sqrt of num.''' + # only pass in numbers that are perfect squares for sake of implementation + # leetcode easy + # CHALLENGE: do not use any built-in Python functions + a = 0 + while (product(a,a)!= num): + a+=1 + return a + + +def main(): + '''The main function is where you will test all of your functions.''' + hello_world() + print("Testing sum:") + print("EXPECTED: %d, ACTUAL: %d\n" % (5, sum(2, 3))) + print("Testing sub:") + print("EXPECTED: %d, ACTUAL: %d\n" % (5, sub(7, 2))) + print("Testing product:") + print("EXPECTED: %d, ACTUAL: %d\n" % (10, product(2, 5))) + print("Testing divide:") + print("EXPECTED: %d, ACTUAL: %d\n" % (5, divide(10, 2))) + print("Testing root:") + print("EXPECTED: %d, ACTUAL: %d\n" % (5, root(25))) + # Add any additional test cases if needed + +if __name__ == "__main__": + main() \ No newline at end of file From 046ec6234b4b0ccf8f80b0bfc9350edc5e9cea07 Mon Sep 17 00:00:00 2001 From: jskulnik Date: Tue, 12 Oct 2021 20:39:27 -0400 Subject: [PATCH 2/4] main1 --- tutorial1.py | 63 ---------------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 tutorial1.py diff --git a/tutorial1.py b/tutorial1.py deleted file mode 100644 index c5c217a..0000000 --- a/tutorial1.py +++ /dev/null @@ -1,63 +0,0 @@ -def hello_world(): - '''Prints "Hello World!".''' - return print("Hello World") - - -def sum(a, b): - '''Accepts 2 numbers as parameters, returns sum of a and b.''' - return a + b - - -def sub(a, b): - '''Accepts 2 numbers as parameters, returns subtraction of a and b.''' - 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 - p = 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 - count = 0 - while (a != 0): - a = sub(a,b) - count +=1 - return count - - -def root(num): - '''Accepts a number as a parameter, returns the sqrt of num.''' - # only pass in numbers that are perfect squares for sake of implementation - # leetcode easy - # CHALLENGE: do not use any built-in Python functions - a = 0 - while (product(a,a)!= num): - a+=1 - return a - - -def main(): - '''The main function is where you will test all of your functions.''' - hello_world() - print("Testing sum:") - print("EXPECTED: %d, ACTUAL: %d\n" % (5, sum(2, 3))) - print("Testing sub:") - print("EXPECTED: %d, ACTUAL: %d\n" % (5, sub(7, 2))) - print("Testing product:") - print("EXPECTED: %d, ACTUAL: %d\n" % (10, product(2, 5))) - print("Testing divide:") - print("EXPECTED: %d, ACTUAL: %d\n" % (5, divide(10, 2))) - print("Testing root:") - print("EXPECTED: %d, ACTUAL: %d\n" % (5, root(25))) - # Add any additional test cases if needed - -if __name__ == "__main__": - main() \ No newline at end of file From a6fef619d0edf71c850b2cce52ec4f334a2145ad Mon Sep 17 00:00:00 2001 From: jskulnik Date: Tue, 26 Oct 2021 19:56:00 -0400 Subject: [PATCH 3/4] main2 work --- main2.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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(): From 6f551399f6fcd9679b98cb50c796cd9d6ae42604 Mon Sep 17 00:00:00 2001 From: jskulnik Date: Mon, 8 Nov 2021 15:42:53 -0500 Subject: [PATCH 4/4] solution 3 --- main3.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 main3.py 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