Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
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):
'''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
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():
Expand Down
26 changes: 24 additions & 2 deletions main2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,37 @@ 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):
'''Given an unsorted list of numbers, returns the value that occured the most in 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():
Expand Down
38 changes: 32 additions & 6 deletions main3.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
#### 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 <num> only if <num> 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():
'''Returns today's date in the format <month> <day>, <year> 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
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 <type> of an unsorted list <nums>, 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():
'''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
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():
Expand Down