migrate to git.charlotte.sh
This commit is contained in:
commit
fbd588721e
412 changed files with 13750 additions and 0 deletions
104
python-csi160/week05/croce_week5_lab.py
Normal file
104
python-csi160/week05/croce_week5_lab.py
Normal file
|
@ -0,0 +1,104 @@
|
|||
# Charlotte Croce
|
||||
# CSI 160: week5 lab
|
||||
# 2/13/2025
|
||||
|
||||
import math
|
||||
|
||||
def number_of_zeros(grades):
|
||||
"""Given a list of grades, determines the number of grades that are 0%
|
||||
|
||||
params:
|
||||
grades (list of floats) - The grades to search. Example [75, 82.5, 97, 0, 87.5]
|
||||
|
||||
return (int) - the number of 0% grades
|
||||
"""
|
||||
count = 0
|
||||
for grade in grades:
|
||||
if grade == 0:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def median(numbers):
|
||||
"""Find the median of the given list of numbers.
|
||||
|
||||
How to find the median:
|
||||
https://www.mathsisfun.com/median.html
|
||||
|
||||
Note: Write your own implementation and do not use any libraries. You will need to sort the list.
|
||||
|
||||
params:
|
||||
numbers (list) A list containing either int or float elements
|
||||
|
||||
return (numeric) The median value as either an int or float
|
||||
"""
|
||||
numbers.sort()
|
||||
length = len(numbers)
|
||||
middle = length // 2
|
||||
if length % 2 == 0: # even number-> avg of middle two
|
||||
return (numbers[middle - 1] + numbers[middle]) / 2
|
||||
else: # odd number-> middle element
|
||||
return numbers[middle]
|
||||
|
||||
|
||||
def top_quartile(grades):
|
||||
"""Return the top 25% of the grades in the supplied list of grades. Round up when determining how many grades to include in the top 25%.
|
||||
|
||||
Hint: You will need to sort the list of grades
|
||||
|
||||
params:
|
||||
grades (list of floats) Example [75, 82.5, 97, 0, 87.5]
|
||||
|
||||
return (list of floats) - The top 25%
|
||||
"""
|
||||
grades.sort()
|
||||
quarter = math.ceil(len(grades) / 4)
|
||||
return grades[-quarter:]
|
||||
|
||||
|
||||
def domain_name_extractor(url):
|
||||
"""Given a url, return the domain name
|
||||
|
||||
You will need to utilize the .find method to complete this https://docs.python.org/3/library/stdtypes.html#str.find
|
||||
|
||||
Hint: Find the starting point of the domain name, then find the end point.
|
||||
params:
|
||||
url (string) the url to search. Example: https://docs.python.org/3/library
|
||||
|
||||
return (string) The domain name or IP address. Example: docs.python.org
|
||||
"""
|
||||
start_index = url.find('//') + 2 # find the start of the domain name after http(s)://
|
||||
end_index = url.find('/', start_index) # starting search from start_index
|
||||
if end_index == -1: # no '/' found after domain name, or it's the last character
|
||||
return url[start_index:]
|
||||
return url[start_index:end_index]
|
||||
|
||||
def test_number_of_zeros():
|
||||
print('Running number_of_zeros tests:')
|
||||
print('Test 1 passed -', number_of_zeros([75.0, 0.0, 97.0, 0.0, 87.5]) == 2)
|
||||
print('Test 2 passed -', number_of_zeros([]) == 0)
|
||||
|
||||
def test_median():
|
||||
print('Running median tests:')
|
||||
print('Test 1 passed -', median([10, 5, 8, 4, -1]) == 5)
|
||||
print('Test 2 passed -', median([10, 8, 4, -1]) == 6)
|
||||
|
||||
def test_top_quartile():
|
||||
print('Running top_quartile tests:')
|
||||
print('Test 1 passed -', top_quartile([97, 92.5, 84, 79, 67]) == [92.5, 97])
|
||||
print('Test 2 passed -', top_quartile([92.5, 86, 89, 75]) == [92.5])
|
||||
|
||||
def test_domain_name_extractor():
|
||||
print('Running domain_name_extractor tests:')
|
||||
print('Test 1 passed -', domain_name_extractor('https://champlain.instructure.com/courses/8933') == 'champlain.instructure.com')
|
||||
print('Test 2 passed -', domain_name_extractor('ftp://champlain.edu/myfile.pdf') == 'champlain.edu')
|
||||
|
||||
print('Running Unit Tests\n')
|
||||
test_number_of_zeros()
|
||||
print()
|
||||
test_median()
|
||||
print()
|
||||
test_top_quartile()
|
||||
print()
|
||||
test_domain_name_extractor()
|
||||
|
10
python-csi160/week05/week-5Practice-Problems/part1.py
Normal file
10
python-csi160/week05/week-5Practice-Problems/part1.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
def first_element(list_to_search):
|
||||
"""Returns the first element of the supplied list. Returns None if the list was empty"""
|
||||
if len(list_to_search) == 0:
|
||||
return None
|
||||
return list_to_search[0]
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('[4, 2, 15, 5] The first_element returns', first_element([4, 2, 15, 5]))
|
||||
print('[] The first_element returns', first_element([]))
|
10
python-csi160/week05/week-5Practice-Problems/part2.py
Normal file
10
python-csi160/week05/week-5Practice-Problems/part2.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
def second_element(list_to_search):
|
||||
"""Returns the second element of the supplied list. Returns None if list contains less than 2 elements"""
|
||||
if len(list_to_search) < 2:
|
||||
return None
|
||||
return list_to_search[1]
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('[4, 2, 15, 5] The second_element returns', second_element([4, 2, 15, 5]))
|
||||
print('[3] The second_element returns', second_element([3]))
|
11
python-csi160/week05/week-5Practice-Problems/part3.py
Normal file
11
python-csi160/week05/week-5Practice-Problems/part3.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
def last_element(list_to_search):
|
||||
"""Returns the last element of the supplied list.
|
||||
Returns None if the list is empty"""
|
||||
if len(list_to_search) == 0:
|
||||
return None
|
||||
return list_to_search[-1]
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('[4, 2, 15, 5] The last_element returns', last_element([4, 2, 15, 5]))
|
||||
print('[] The last_element returns', last_element([]))
|
13
python-csi160/week05/week-5Practice-Problems/part4.py
Normal file
13
python-csi160/week05/week-5Practice-Problems/part4.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
def area_code(phone_number):
|
||||
""" Given a phone number in the format: 802-555-1212
|
||||
extract the first 3 characters and return this as a string
|
||||
|
||||
:param phone_number: (str) Must be formatted "XXX-XXX-XXXX"
|
||||
:return: (str) 3 digit area_code
|
||||
"""
|
||||
return phone_number[:3]
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('"802-555-1212" area_code returns', area_code("802-555-1212"))
|
||||
print('"410-617-3452" area_code returns', area_code("410-617-3452"))
|
12
python-csi160/week05/week-5Practice-Problems/part5.py
Normal file
12
python-csi160/week05/week-5Practice-Problems/part5.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
def last_four_digits(phone_number):
|
||||
""" Returns the last 4 digits of a phone number as a string
|
||||
|
||||
:param phone_number: (str) May be formatted "XXX-XXX-XXXX" or "1-XXX-XXX-XXXX"
|
||||
:return: (str) last four digits
|
||||
"""
|
||||
return phone_number[-4:]
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('"802-555-1212" last_four_digits returns', last_four_digits("802-555-1212"))
|
||||
print('"1-410-617-3452" last_four_digits returns', last_four_digits("410-617-3452"))
|
13
python-csi160/week05/week-5Practice-Problems/part6.py
Normal file
13
python-csi160/week05/week-5Practice-Problems/part6.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
def first_last_name(full_name):
|
||||
""" Returns a tuple with the first and last name.
|
||||
|
||||
:param full_name: (str) Contains a first and last name seperated by a space.
|
||||
There is only a single space in the full_name.
|
||||
:return: (tuple) Tuple containing first and last names
|
||||
"""
|
||||
return tuple(full_name.split(" "))
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('"Billie Holiday" first_last_name returns', first_last_name("Billie Holiday"))
|
||||
print('"James Brown" first_last_name returns', first_last_name("James Brown"))
|
15
python-csi160/week05/week-5Practice-Problems/part7.py
Normal file
15
python-csi160/week05/week-5Practice-Problems/part7.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
def second_smallest(numbers):
|
||||
"""Returns the second smallest number
|
||||
|
||||
:param numbers: (list) List of numbers of at least size 2
|
||||
:return: (int or float) The second smallest number
|
||||
"""
|
||||
numbers.sort()
|
||||
return numbers[1]
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('[6, 3, 9, 2, 1] second_smallest returns', second_smallest([6, 3, 9, 2, 1]))
|
||||
print('[23, 23.25, 23.5, 23] second_smallest returns', second_smallest([23, 23.25, 23.5, 23]))
|
||||
|
||||
|
24
python-csi160/week05/week-5Practice-Problems/part8.py
Normal file
24
python-csi160/week05/week-5Practice-Problems/part8.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
def area_code(phone_number):
|
||||
"""Returns the area code of the supplied phone number.
|
||||
|
||||
params:
|
||||
phone_number (string) in the format:
|
||||
1-XXX-XXX-XXXX
|
||||
XXX-XXX-XXXX
|
||||
(XXX)-XXX-XXXX
|
||||
return: (string) The area code
|
||||
"""
|
||||
if phone_number[0] == '(':
|
||||
return phone_number[1:4]
|
||||
elif phone_number[0] == '1':
|
||||
return phone_number[2:5]
|
||||
else:
|
||||
return phone_number[:3]
|
||||
|
||||
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('"1-617-555-1212" area_code returns', area_code("1-617-555-1212"))
|
||||
print('"802-999-1212" area_code returns', area_code("802-999-1212"))
|
||||
print('"(802)-999-1212" area_code returns', area_code("(802)-999-1212"))
|
Loading…
Add table
Add a link
Reference in a new issue