CS50 Introduction To Python Programming
The CS50's Introduction to Programming with Python is a course offered by Harvard University, focusing on teaching Python programming.
This course, led by Harvard Professor David J. Malan, is designed for students with or without prior programming experience who want to learn Python specifically.
It covers topics such as functions, variables, conditionals, loops, exceptions, libraries, unit tests, file I/O, regular expressions, and object-oriented programming.
The course provides hands-on practice opportunities with exercises inspired by real-world programming problems. It can be taken independently or as a precursor to the more general CS50x course,
which covers computer science and programming with C, Python, SQL, and JavaScript.
The CS50's Introduction to Programming with Python course is available online through platforms like Harvard Online, edX, and YouTube, offering a comprehensive introduction to Python programming
Problem Set 2 Solutions
- camelCase
- Coke Machine
- Just setting up my twttr
- Vanity Plates
- Nutrition Facts
camelCase
In some languages, it's common to use camel case (otherwise known as “mixed case”) for variables' names when those names comprise multiple words, whereby the first letter of the first word is lowercase but the first letter of each subsequent word is uppercase.
For instance, whereas a variable for a user's name might be called name, a variable for a user's first name might be called firstName, and a variable for a user's preferred first name (e.g., nickname) might be called preferredFirstName.
Python, by contrast, recommends snake case, whereby words are instead separated by underscores (_), with all letters in lowercase.
For instance, those same variables would be called name, first_name, and preferred_first_name, respectively, in Python.
In a file called camel.py, implement a program that prompts the user for the name of a variable in camel case and outputs the corresponding name in snake case. Assume that the user's input will indeed be in camel case.
Source Code
def main(): camel_case = input("Enter a variable name in camel case: ").strip() snake_case = "" for i, c in enumerate(camel_case): if c.isupper(): if i != 0: snake_case += "_" snake_case += c.lower() else: snake_case += c print("Snake case: " + snake_case) if __name__ == "__main__": main()
Coke Machine
Suppose that a machine sells bottles of Coca-Cola (Coke) for 50 cents and only accepts coins in these denominations: 25 cents, 10 cents, and 5 cents.
In a file called coke.py, implement a program that prompts the user to insert a coin, one at a time, each time informing the user of the amount due.
Once the user has inputted at least 50 cents, output how many cents in change the user is owed.
Assume that the user will only input integers, and ignore any integer that isn't an accepted denomination.
Source Code
# Initialize variables amount_due = 0 # Define accepted coin denominations accepted_coins = [25, 10, 5] # Prompt user to insert coins until amount due reaches 50 cents while amount_due < 50: try: coin = int(input("Insert Coin: ")) if coin in accepted_coins: amount_due += coin print(f"Amount Due: {amount_due}") else: print("Invalid coin. Please insert a valid coin.") except ValueError: print("Invalid input. Please enter a valid integer.") # Calculate change owed change_owed = amount_due - 50 if amount_due > 50 else 0 # Display change owed print(f"Change Owed: {change_owed}")
Just setting up my twttr
When texting or tweeting, it's not uncommon to shorten words to save time or space, as by omitting vowels, much like Twitter was originally called twttr.
In a file called twttr.py, implement a program that prompts the user for a str of text and
then outputs that same text but with all vowels (A, E, I, O, and U) omitted, whether inputted in uppercase or lowercase.
Source Code
text = input("text: ") new = "" vowels=["a","e","i","o","u"] for i in range(len(text)): if text[i].lower() not in vowels: new+=text[i] print(new)
Vanity plates
In plates.py, implement a program that prompts the user for a vanity plate and then output Valid if meets all of the requirements or Invalid if it does not. Assume that any letters in the user's input will be uppercase. Structure your program per the below, wherein is_valid returns True if s meets all requirements and False if it does not. Assume that s will be a str. You're welcome to implement additional functions for is_valid to call (e.g., one function per requirement).
Source Code
def main(): plate = input("Plate: ") if is_valid(plate): print("Valid") else: print("Invalid") def is_valid(s): if 6>=len(s) >=2 and s[0:2].isalpha() and s.isalnum(): for char in s: if char.isdigit(): index=s.index(char) if s[index:].isdigit() and int(char)!=0: return True else: return False main()
Nutrition Facts
In a file called nutrition.py, implement a program that prompts consumers users to input a fruit (case-insensitively) and then outputs the number of calories in one portion of that fruit, per the FDA's poster for fruits, which is also available as text. Capitalization aside, assume that users will input fruits exactly as written in the poster (e.g., strawberries, not strawberry). Ignore any input that isn't a fruit.
Source Code
fruits = { 'apple': 130, 'avocado': 50, 'banana': 110, 'cantaloupe': 50, 'grapefruit': 60, 'grapes': 90, 'honeydew melon': 50, 'kiwifruit': 90, 'lemon': 15, 'lime': 20, 'nectarine': 60, 'orange': 80, 'peach': 60, 'pear': 100, 'pineapple': 50, 'plums': 70, 'strawberries': 50, 'sweet cherries': 100, 'tangerine': 50, 'watermelon': 80 } fruit = input("Fruit: ").lower() if fruit in fruits: print("Calories: ", fruits[fruit])