migrate to git.charlotte.sh

This commit is contained in:
Charlotte Croce 2025-04-19 23:42:08 -04:00
commit fbd588721e
412 changed files with 13750 additions and 0 deletions

View file

@ -0,0 +1,150 @@
# Charlotte Croce
# CSI 160
# Week 6: Lab - Loops
# 2/17/25
# 1. Area Codes
# Given a list of phone numbers that are missing the area code,
# append the area code to the phone numbers in the list and return the result list.
def add_area_code(phone_numbers, area_code):
"""Returns a list of phone numbers with the area code added.
Given a list of phone numbers that are missing the area code,
append the area code to the phone numbers in the list and return the result list.
:param phone_numbers: (list) A list of phone numbers (strings) that do not have the area code
Example: ['555-1212']
:param area_code: (str) The area code to add Example: '802'
:return: (list) A list of phone numbers with the area code Example: ['802-555-1212']
"""
phone_numbers_with_area = phone_numbers.copy()
for i in range(len(phone_numbers_with_area)):
phone_numbers_with_area[i] = area_code + '-' + phone_numbers[i]
return phone_numbers_with_area
# example usage
phone_numbers = ['555-1212', '999-0738']
with_area_code = add_area_code(phone_numbers, '802')
print(with_area_code)
################################################################
# 2. Print even numbers
# Complete the following function to print the even numbers of a list, one per line.
def print_even(numbers):
"""Prints the even numbers in a list, one per line
:param numbers: (list) list of integers
:return: None
"""
for number in numbers:
if number % 2 == 0:
print(number)
print_even([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
################################################################
# 3. Guessing Game
# Extend your guessing game from earlier. Write a program that picks a random number from 1-100. Then ask the user to guess a number.
# Tell the user if the answer is higher or lower than the number they guessed, or if they got the correct answer.
# Allow them to guess again if they got the guess incorrect. They should be able to guess numbers an infinite number
# of times until they get the correct answer, at which point your loop will end.
# To generate a number from 1-100 you will need the following code at the beginning of your program:
from random import randint
def guessing_game():
"""Plays a guessing game where the user guesses a number between 1 and 100
:return: None
"""
randomNum = randint(1, 100)
while True:
guess = int(input("Guess a number 1-100: "))
if guess < randomNum:
print("Higher")
elif guess > randomNum:
print("Lower")
else:
print("Correct!")
break
guessing_game()
################################################################
#4. Backpack of Stuff
#Complete the following code. Fill in the two sections of code identified in the comments.
import sys
def backpack_of_stuff():
"""Allows user to add and check items in a backpack
:return: None
"""
itemsInBackpack = ["book", "computer", "keys", "travel mug"]
while True:
print("Would you like to:")
print("1. Add an item to the backpack?")
print("2. Check if an item is in the backpack?")
print("3. Quit")
userChoice = input()
if(userChoice == "1"):
print("What item do you want to add to the backpack?")
itemToAdd = input()
itemsInBackpack.append(itemToAdd)
if(userChoice == "2"):
print("What item do you want to check to see if it is in the backpack?")
itemToCheck = input()
if itemToCheck in itemsInBackpack:
print("yes, this item is in backpack")
else:
print("no, this item is not in backpack")
if(userChoice == "3"):
#sys.exit()
break
backpack_of_stuff()
################################################################
# 5. Comma Code
# Say you have a list value like this:
# listToPrint = ['apples', 'bananas', 'tofu', 'cats']
#Write a program that prints a list with all the items separated by a comma and a space,
# with and inserted before the last item. For example, the above list would print 'apples, bananas, tofu, and cats'.
# But your program should be able to work with any list not just the one shown above.
# Because of this, you will need to use a loop in case the list to print is shorter or longer than the above list.
# Do not modify the list by inserting 'and' into the list and do not simply print the entire list
# (you must loop through printing each item individually). This problem is trickier than it appears at first glance.
def comma_code(listToPrint):
"""Prints a list with all the items separated by a comma and a space, with 'and' inserted before the last item
:param listToPrint: (list) A list of strings
:return: None
"""
# cases for list being 2, 1, or 0 elements
if len(listToPrint) == 0:
print("List is empty")
return
elif len(listToPrint) == 1:
print(listToPrint[0])
return
elif len(listToPrint) == 2:
print(listToPrint[0] + " and " + listToPrint[1])
return
else:
for i in range(len(listToPrint) - 1): # loop through all but last element
print(listToPrint[i], end=", ")
print("and " + listToPrint[-1]) #print final element of list
comma_code(['apples', 'bananas', 'tofu', 'cats', 'dogs'])

View file

@ -0,0 +1,7 @@
#1 initialize the loop
x = int(input('What number do you want to start at? '))
# Print countdown using while loop with one number per line
while x > 0:
print(x)
x -= 1

View file

@ -0,0 +1,8 @@
a = int(input('What number do you want to start at? '))
b = int(input('What number do you want to end at? '))
# Print all numbers from a to b inclusively. Assume (a ≤ b)
while a <= b:
print(a)
a += 1

View file

@ -0,0 +1,18 @@
def all_squares(n):
"""Returns a list that contains all the squares of positive integers
where the square is less than or equal to N, in ascending order.
:param n: (int) Upper bound
:return: (list) List of squares
"""
squares = []
i = 1
while i * i <= n:
squares.append(i * i)
i += 1
return squares
# Leave this part for easily testing your function
print('all_squares(50) returns:', all_squares(50))
print('all_squares(9) returns:', all_squares(9))

View file

@ -0,0 +1,21 @@
def introductions(names):
"""Prints introductions.
An introduction is 'Hello' followed by a space and a name.
Include a newline after each introduction.
Example:
'Hello Josh'
Important: Your function should use print() and not have a return statement
:param names: (list) First Names as strings
:return: None
"""
for name in names:
print("Hello", name)
# Leave this part for easily testing your function
sample_names = input('Enter the names separated by a comma and a space: ').split(', ')
introductions(sample_names)

View file

@ -0,0 +1,25 @@
def num_distinct_elements(numbers):
"""Determine the number of distinct elements in the list
:param numbers: (list) A list of numbers (int or float)
:return: (int) Number of distinct elements
"""
# Use the function sorted() to temporarily sort the numbers
# into ascending order or use the method .sort() to permanently
# sort the list in ascending order.
numbers.sort()
unique_numbers = 1
for i in range(1, len(numbers)):
if numbers[i] != numbers[i - 1]:
unique_numbers += 1
return unique_numbers
# Leave this part for easily testing your function
test1 = [2, 5, 5, 7,2, 9.5, 2, 4]
print(f'num_distinct_elements({test1}) returns:', num_distinct_elements(test1))
test2 = [2, 1, 1, 7, 1, 9.5, 2, 1]
print(f'num_distinct_elements({test2}) returns:', num_distinct_elements(test2))

View file

@ -0,0 +1,15 @@
def fibonacci(n):
"""Return the nth Fibonacci number.
param n: (int) position to determine fibonacci of
return: (int) value at position n
"""
fib_numbers = [0, 1]
for i in range(2, n + 1):
fib_numbers.append(fib_numbers[i - 1] + fib_numbers[i - 2])
return fib_numbers[n]
# Leave this part for easily testing your function
print('fibonacci(6) returns:', fibonacci(6), 'expected 8')
print('fibonacci(3) returns:', fibonacci(3), 'expected 2')