From c37559a40a897bde187df306c94a06e59cd9aa87 Mon Sep 17 00:00:00 2001 From: Neha Peddinti Date: Sun, 26 Jul 2020 21:33:43 -0400 Subject: [PATCH 1/6] Add practice problems/solutions to Ch. 1-8 --- 1_beginner/chapter3/practice/integer.py | 9 ++ 1_beginner/chapter3/practice/names.py | 9 ++ 1_beginner/chapter3/practice/wizard.py | 39 ++++++ 1_beginner/chapter3/solutions/integer.py | 15 +++ 1_beginner/chapter3/solutions/names.py | 15 +++ 1_beginner/chapter3/solutions/wizard.py | 39 ++++++ 1_beginner/chapter5/practice/multiply.py | 12 ++ 1_beginner/chapter5/solutions/multiply.py | 19 +++ 1_beginner/chapter6/practice/grades.py | 22 +++ 1_beginner/chapter6/practice/indexing.py | 22 +++ 1_beginner/chapter6/practice/manipulation.py | 32 +++++ 1_beginner/chapter6/practice/names.py | 17 +++ 1_beginner/chapter6/practice/restaurant.py | 94 +++++++++++++ 1_beginner/chapter6/solutions/grades.py | 32 +++++ 1_beginner/chapter6/solutions/indexing.py | 26 ++++ 1_beginner/chapter6/solutions/manipulation.py | 54 ++++++++ 1_beginner/chapter6/solutions/names.py | 30 +++++ 1_beginner/chapter6/solutions/restaurant.py | 126 ++++++++++++++++++ 1_beginner/chapter7/practice/capital.py | 14 ++ .../chapter7/practice/every_other_word.py | 8 ++ 1_beginner/chapter7/practice/phone_number.py | 22 +++ .../chapter7/practice/reverse_alphabet.py | 14 ++ 1_beginner/chapter7/solutions/capital.py | 31 +++++ .../chapter7/solutions/every_other_word.py | 17 +++ 1_beginner/chapter7/solutions/phone_number.py | 26 ++++ .../chapter7/solutions/reverse_alphabet.py | 38 ++++++ 2_intermediate/chapter8/practice/food.py | 11 ++ 2_intermediate/chapter8/practice/fortune.py | 10 ++ 2_intermediate/chapter8/solutions/food.py | 30 +++++ 2_intermediate/chapter8/solutions/fortune.py | 22 +++ 30 files changed, 855 insertions(+) create mode 100644 1_beginner/chapter3/practice/integer.py create mode 100644 1_beginner/chapter3/practice/names.py create mode 100644 1_beginner/chapter3/practice/wizard.py create mode 100644 1_beginner/chapter3/solutions/integer.py create mode 100644 1_beginner/chapter3/solutions/names.py create mode 100644 1_beginner/chapter3/solutions/wizard.py create mode 100644 1_beginner/chapter5/practice/multiply.py create mode 100644 1_beginner/chapter5/solutions/multiply.py create mode 100644 1_beginner/chapter6/practice/grades.py create mode 100644 1_beginner/chapter6/practice/indexing.py create mode 100644 1_beginner/chapter6/practice/manipulation.py create mode 100644 1_beginner/chapter6/practice/names.py create mode 100644 1_beginner/chapter6/practice/restaurant.py create mode 100644 1_beginner/chapter6/solutions/grades.py create mode 100644 1_beginner/chapter6/solutions/indexing.py create mode 100644 1_beginner/chapter6/solutions/manipulation.py create mode 100644 1_beginner/chapter6/solutions/names.py create mode 100644 1_beginner/chapter6/solutions/restaurant.py create mode 100644 1_beginner/chapter7/practice/capital.py create mode 100644 1_beginner/chapter7/practice/every_other_word.py create mode 100644 1_beginner/chapter7/practice/phone_number.py create mode 100644 1_beginner/chapter7/practice/reverse_alphabet.py create mode 100644 1_beginner/chapter7/solutions/capital.py create mode 100644 1_beginner/chapter7/solutions/every_other_word.py create mode 100644 1_beginner/chapter7/solutions/phone_number.py create mode 100644 1_beginner/chapter7/solutions/reverse_alphabet.py create mode 100644 2_intermediate/chapter8/practice/food.py create mode 100644 2_intermediate/chapter8/practice/fortune.py create mode 100644 2_intermediate/chapter8/solutions/food.py create mode 100644 2_intermediate/chapter8/solutions/fortune.py diff --git a/1_beginner/chapter3/practice/integer.py b/1_beginner/chapter3/practice/integer.py new file mode 100644 index 00000000..96693b18 --- /dev/null +++ b/1_beginner/chapter3/practice/integer.py @@ -0,0 +1,9 @@ +""" +Integer + +Write a program that takes any number +(decimals included) as input, and outputs +whether or not it's an integer. +""" + +# Insert code here. diff --git a/1_beginner/chapter3/practice/names.py b/1_beginner/chapter3/practice/names.py new file mode 100644 index 00000000..dcda62a9 --- /dev/null +++ b/1_beginner/chapter3/practice/names.py @@ -0,0 +1,9 @@ +""" +Names + +Write a program that asks a user +for two names, and outputs True if +the names are NOT the same. +""" + +# Insert code here. diff --git a/1_beginner/chapter3/practice/wizard.py b/1_beginner/chapter3/practice/wizard.py new file mode 100644 index 00000000..49c8d555 --- /dev/null +++ b/1_beginner/chapter3/practice/wizard.py @@ -0,0 +1,39 @@ +""" +Wizard + +There are 3 criteria to determine whether +you’re a wizard or not. + +Define a variable called is_wizard and +use logic operators to set it to the correct +value based on the criteria. + +Here are some example variable values and outputs. +You'll need to figure out the order of logic operators +needed to turn these inputs into these outputs! :) + +Example variable values and output: + +If you can fly, you’ve not a battled a dragon, +and you’re alive, output “Wizard: True” + +If you can fly, you’ve battled a dragon, +and you’re not alive, output “Wizard: True” + +If you can’t fly, you’ve battled a dragon, +and you’re not alive, output “Wizard: False” + +If you can’t fly, you’ve battled a dragon, +and you’re alive, output “Wizard: True" + +The initial variables are given, but you'll have +to change the values to test your code. +""" + +can_fly = True +battled_dragon = False +is_alive = True + +# Insert your code here. + +# print("Wizard:", is_wizard) diff --git a/1_beginner/chapter3/solutions/integer.py b/1_beginner/chapter3/solutions/integer.py new file mode 100644 index 00000000..4f6b539e --- /dev/null +++ b/1_beginner/chapter3/solutions/integer.py @@ -0,0 +1,15 @@ +""" +Integer + +Write a program that takes any number +(decimals included) as input, and outputs +whether or not it's an integer. +""" + +# Get input as a floating point +x = float(input("Enter a number: ")) + +# Compare x to the integer version of itself +is_integer = (x == int(x)) + +print("Is integer? " + str(is_integer)) diff --git a/1_beginner/chapter3/solutions/names.py b/1_beginner/chapter3/solutions/names.py new file mode 100644 index 00000000..09da01fd --- /dev/null +++ b/1_beginner/chapter3/solutions/names.py @@ -0,0 +1,15 @@ +""" +Names + +Write a program that asks a user +for two names, and outputs True if +the names are NOT the same. +""" + +name1 = input("Person 1: ") +name2 = input("Person 2: ") + +# "False" if name1 and name2 are equal +not_same = (name1 != name2) + +print("Not the same?", not_same) diff --git a/1_beginner/chapter3/solutions/wizard.py b/1_beginner/chapter3/solutions/wizard.py new file mode 100644 index 00000000..10154aba --- /dev/null +++ b/1_beginner/chapter3/solutions/wizard.py @@ -0,0 +1,39 @@ +""" +Wizard + +There are 3 criteria to determine whether +you’re a wizard or not. + +Define a variable called is_wizard and +use logic operators to set it to the correct +value based on the criteria. + +Here are some example variable values and outputs. +You'll need to figure out the order of logic operators +needed to turn these inputs into these outputs! :) + +Example variable values and output: + +If you can fly, you’ve not a battled a dragon, +and you’re alive, output “Wizard: True” + +If you can fly, you’ve battled a dragon, +and you’re not alive, output “Wizard: True” + +If you can’t fly, you’ve battled a dragon, +and you’re not alive, output “Wizard: False” + +If you can’t fly, you’ve battled a dragon, +and you’re alive, output “Wizard: True" + +The initial variables are given, but you'll have +to change the values to test your code. +""" + +can_fly = True +battled_dragon = False +is_alive = True + +is_wizard = can_fly or (battled_dragon and is_alive) + +print("Wizard:", is_wizard) diff --git a/1_beginner/chapter5/practice/multiply.py b/1_beginner/chapter5/practice/multiply.py new file mode 100644 index 00000000..95f0b4aa --- /dev/null +++ b/1_beginner/chapter5/practice/multiply.py @@ -0,0 +1,12 @@ +""" +Multiply + +Write a program that asks the user +for 10 integers, multiplies them all +together, and displays the product at +the end. + +Use a for loop! +""" + +# Insert your code here. diff --git a/1_beginner/chapter5/solutions/multiply.py b/1_beginner/chapter5/solutions/multiply.py new file mode 100644 index 00000000..618dcd3c --- /dev/null +++ b/1_beginner/chapter5/solutions/multiply.py @@ -0,0 +1,19 @@ +""" +Multiply + +Write a program that asks the user +for 10 integers, multiplies them all +together, and displays the product at +the end. + +Use a for loop! +""" + +# Initial value is 1 +product = 1 + +# Ask the user for 10 numbers and multiply. +for i in range(10): + product *= int(input("Enter a number: ")) + +print("The product is " + str(product)) diff --git a/1_beginner/chapter6/practice/grades.py b/1_beginner/chapter6/practice/grades.py new file mode 100644 index 00000000..1bd0e054 --- /dev/null +++ b/1_beginner/chapter6/practice/grades.py @@ -0,0 +1,22 @@ +""" +Grades + +Create a list called names and a list called grades. +Ask the user to input a name, and then ask +them to input the person's grade. Add the inputs +to the corresponding lists. Use a for loop to ask +for these inputs 5 times. + +Display the info as "[name]: [grade]". + +Example lists AFTER user input: +names = ["John", "Belle", "Ria", "Steph", "Louis"] +grades = [93, 85, 100, 82, 70] + +Example output: +John: 93 +Belle: 85 +etc. +""" + +# Insert your code here. diff --git a/1_beginner/chapter6/practice/indexing.py b/1_beginner/chapter6/practice/indexing.py new file mode 100644 index 00000000..b288bc67 --- /dev/null +++ b/1_beginner/chapter6/practice/indexing.py @@ -0,0 +1,22 @@ +""" +Indexing + +1. Create a list with the following names: + Mark + Arya + Paz + Lulu + Jon + Robin +2. Print the first element of the list. +3. Print the 3rd-to last name ("Lulu") WITHOUT +using people[3]. +4. Print the second element of the list. + +Don't "hard-code" the answers. +For example, don't write print("Mark"). +Instead, use list indexing to get the values +from the list. +""" + +# Insert your code here. diff --git a/1_beginner/chapter6/practice/manipulation.py b/1_beginner/chapter6/practice/manipulation.py new file mode 100644 index 00000000..43ae7e42 --- /dev/null +++ b/1_beginner/chapter6/practice/manipulation.py @@ -0,0 +1,32 @@ +# Manipulation +# Modify the following code according to +# the instructions. + +nums = [] + +""" +The following code adds the numbers 0, 10, ... 100 +to the list nums, and displays this list. +First, make a copy of nums and store it in the varible more_nums. + +Then, clear nums, and add the numbers +2, 7, 12, ... 72 to nums instead. +""" + +for i in range(0, 101, 10): + nums.append(i) +print(nums) + +""" +Write the code for the following actions: +Change the 1st element of more_nums to -100. +Change the 2nd element of nums to 0. +Remove the last element from more_nums. +Remove every element divisible by 3 from more_nums. +Insert a 21 after the 20 in more_nums (assume that +you DON'T know the index of 20 ahead of time). +Insert 15 0's in the 3rd-to-last position. + Sample list after insertions: + [..., 0, 0, ..., 0, 0, element, element] +Display nums and more_nums. +""" diff --git a/1_beginner/chapter6/practice/names.py b/1_beginner/chapter6/practice/names.py new file mode 100644 index 00000000..14e1f181 --- /dev/null +++ b/1_beginner/chapter6/practice/names.py @@ -0,0 +1,17 @@ +""" +Names + +Make a list called people and fill it with +at least 6 names. Make another list and use +list slicing to fill it with every other name +from the original list, starting with the 1st name. +Print both lists. +""" + +# Insert your code here. + +""" +Use a for loop to ask the user to add 4 names +to the list. After you ask for each name, print +out the last 5 names of the list. +""" diff --git a/1_beginner/chapter6/practice/restaurant.py b/1_beginner/chapter6/practice/restaurant.py new file mode 100644 index 00000000..d71be0d2 --- /dev/null +++ b/1_beginner/chapter6/practice/restaurant.py @@ -0,0 +1,94 @@ +""" +Restaurant +Write a program that asks someone +what they want to order and give them +the total cost of their meal. + +Each meal should have 3 categories +(ex. food, drinks, desserts), and the user +must order 1 item from each category. + +Make sure to include a 7% sales tax +(multiply the total by 1.07), and +round the answer to 2 decimal places + +The steps are outlined in the following code. +""" + +""" +1. Store the menu in lists. +Each of the 3 categories should have an +items menu and a costs menu. + +Example of the 6 lists you need to create: +foods, food_costs, drinks, drink_costs, +desserts, dessert_costs +""" + +# Insert your lists here. + +""" +2. Display the menu. +Iterate through the items and costs for each +category to do this. + +Example output: +Welcome to my restaurant! Here's the menu: + +Food: +Pancakes: $5 +Waffles: $3 +Toast: $100 + +Drinks: +Juice: $2 +Water: $50 +Tea: $1 + +Sugar: +Muffin: $4 +Lollipop: $20 +Brownie: $15 +""" + +# Insert the code for displaying the menu here. + +""" +3. Ask the user to order. +This code should be in a loop. After you display the +user's total at the end, ask them if they want to +order again. If they say "no", the program should end. +Otherwise, you should take their order and display the +new total again. + +As you take their order, check if what they ordered +is in the corresponding list for that category. +If it is, add the price of the item to the total. +Otherwise, display a warning message and add $1000 to the total. + +Example order (using menu above): +What food would you like? pancakes +What drink would you like? water +What sugar item would you like? candy +You didn't order a proper sugary item! Adding $1000 to tab. +""" + +# Insert the code that takes the user's order here. +# Make sure it's in a loop. + +""" +4. Finalize the total. +Add a 7% sales tax to your sum, and +round this value to 2 decimal places. +Display the total. +""" + +# Add the code to finalize and display the total here. + +""" +5. As mentioned in Step 3, ask the user if they want +to order again. If they say "no", then stop the program. +Otherwise, let them order again. +""" + +# Ask the user if they want to order again here. diff --git a/1_beginner/chapter6/solutions/grades.py b/1_beginner/chapter6/solutions/grades.py new file mode 100644 index 00000000..47c9c498 --- /dev/null +++ b/1_beginner/chapter6/solutions/grades.py @@ -0,0 +1,32 @@ +""" +Grades + +Create a list called names and a list called grades. +Ask the user to input a name, and then ask +them to input the person's grade. Add the inputs +to the corresponding lists. Use a for loop to ask +for these inputs 5 times. + +Display the info as "[name]: [grade]". + +Example lists AFTER user input: +names = ["John", "Belle", "Ria", "Steph", "Louis"] +grades = [93, 85, 100, 82, 70] + +Example output: +John: 93 +Belle: 85 +etc. +""" + +names = [] +grades = [] + +# Collect inputs. +for i in range(5): + names.append(input("Enter a name: ")) + grades.append(input("Enter their grade: ")) + +# Format output correctly. +for i in range(len(names)): + print(names[i] + ": " + grades[i]) diff --git a/1_beginner/chapter6/solutions/indexing.py b/1_beginner/chapter6/solutions/indexing.py new file mode 100644 index 00000000..0b096ab2 --- /dev/null +++ b/1_beginner/chapter6/solutions/indexing.py @@ -0,0 +1,26 @@ +""" +Indexing + +1. Create a list with the following names: + Mark + Arya + Paz + Lulu + Jon + Robin +2. Print the first element of the list. +3. Print the 3rd-to last name ("Lulu") WITHOUT +using people[3]. +4. Print the second element of the list. + +Don't "hard-code" the answers. +For example, don't write print("Mark"). +Instead, use list indexing to get the values +from the list. +""" + +names = ["Mark", "Arya", "Paz", "Lulu", "Jon", "Robin"] + +print(names[0]) +print(names[-3]) +print(names[1]) diff --git a/1_beginner/chapter6/solutions/manipulation.py b/1_beginner/chapter6/solutions/manipulation.py new file mode 100644 index 00000000..43463ed1 --- /dev/null +++ b/1_beginner/chapter6/solutions/manipulation.py @@ -0,0 +1,54 @@ +# Manipulation +# Modify the following code according to +# the instructions. + +nums = [] + +""" +The following code adds the numbers 0, 10, ... 100 +to the list nums, and displays this list. +First, make a copy of nums and store it in the varible more_nums. + +Then, clear nums, and add the numbers +2, 7, 12, ... 72 to nums instead. +""" + +for i in range(0, 101, 10): + nums.append(i) +print(nums) + +more_nums = nums.copy() + +nums.clear() +for i in range(2, 73, 5): + nums.append(i) + +""" +Write the code for the following actions: +Change the 1st element of more_nums to -100. +Change the 2nd element of nums to 0. +Remove the last element from more_nums. +Remove every element divisible by 3 from more_nums. +Insert a 21 after the 20 in more_nums (assume that +you DON'T know the index of 20 ahead of time). +Insert 15 0's in the 3rd-to-last position. + Sample list after insertions: + [..., 0, 0, ..., 0, 0, element, element] +Display nums and more_nums. +""" + +more_nums[0] = -100 +nums[1] = 0 +more_nums.pop(-1) + +for i in more_nums: + if i % 3 == 0: + more_nums.remove(i) + +more_nums.insert(more_nums.index(20)+1, 21) + +for i in range(15): + more_nums.insert(-2, 0) + +print("nums:\n", nums) +print("more_nums:\n", more_nums) diff --git a/1_beginner/chapter6/solutions/names.py b/1_beginner/chapter6/solutions/names.py new file mode 100644 index 00000000..751dddcf --- /dev/null +++ b/1_beginner/chapter6/solutions/names.py @@ -0,0 +1,30 @@ +""" +Names + +Make a list called people and fill it with +at least 6 names. Make another list and use +list slicing to fill it with every other name +from the original list, starting with the 1st name. +Print both lists. +""" + +people = ["Mark", "Anya", "Wan", "Jewel", "Stef", "Hank"] + +# Answers may vary. +# Some other possible answers: +# group = people[0, 5, 2] or people[0, len(people), 2] + +group = people[0:len(people):2] + +print(people) +print(group) + +""" +Use a for loop to ask the user to add 4 names +to the list. After you ask for each name, print +out the last 5 names of the list. +""" + +for i in range(4): + people.append(input("Enter a name: ")) + print("Last 5 names:", people[-5:]) diff --git a/1_beginner/chapter6/solutions/restaurant.py b/1_beginner/chapter6/solutions/restaurant.py new file mode 100644 index 00000000..bdd98111 --- /dev/null +++ b/1_beginner/chapter6/solutions/restaurant.py @@ -0,0 +1,126 @@ +""" +Restaurant +Write a program that asks someone +what they want to order and give them +the total cost of their meal. + +Each meal should have 3 categories +(ex. food, drinks, desserts), and the user +must order 1 item from each category. + +Make sure to include a 7% sales tax +(multiply the total by 1.07), and +round the answer to 2 decimal places + +The steps are outlined in the following code. +""" + +""" +1. Store the menu in lists. +Each of the 3 categories should have an +items menu and a costs menu. + +Example of the 6 lists you need to create: +foods, food_costs, drinks, drink_costs, +desserts, dessert_costs +""" + +foods = ["pancakes", "waffles", "toast"] +food_costs = [5, 3, 100] + +drinks = ["juice", "water", "tea"] +drink_costs = [2, 50, 1] + +sugar_foods = ["muffin", "lollipop", "brownie"] +sugar_costs = [4, 20, 15] + +""" +2. Display the menu. +Iterate through the items and costs for each +category to do this. +""" + +print("Welcome to my restaurant! Here's the menu:") +print("Foods:") +for i in range(len(foods)): + print(foods[i] + ": $" + str(food_costs[i])) +print() + +print("Drinks:") +for i in range(len(drinks)): + print(drinks[i] + ": $" + str(drink_costs[i])) +print() + +print("Sugar:") +for i in range(len(sugar_foods)): + print(sugar_foods[i] + ": $" + str(sugar_costs[i])) +print() + +print("Order in all lowercase!\n") + +""" +3. Ask the user to order. +This code should be in a loop. After you display the +user's total at the end, ask them if they want to +order again. If they say "no", the program should end. +Otherwise, you should take their order and display the +new total again. + +As you take their order, check if what they ordered +is in the corresponding list for that category. +If it is, add the price of the item to the total. +Otherwise, display a warning message and add $1000 to the total. + +4. Finalize the total. +Add a 7% sales tax to your sum, and +round this value to 2 decimal places. +Display the total. + +5. As mentioned in Step 3, ask the user if they want +to order again. If they say "no", then stop the program. +Otherwise, let them order again. +""" + +# Take the user's order. +while True: + cost = 0 + + # Foods + food = input("What food would you like? ") + + if food in foods: + cost += food_costs[foods.index(food)] + else: + print("You didn't order a proper food... Adding $1000 to tab.") + cost += 1000 + + # Drinks + drink = input("What drink would you like? ") + + if drink in drinks: + cost += drink_costs[drinks.index(drink)] + else: + print("You didn't order a proper drink... Adding $1000 to tab.") + cost += 1000 + + # Sugar + sugar = input("What sugar item would you like? ") + + if sugar in sugar_foods: + cost += sugar_costs[sugar_foods.index(sugar)] + else: + print("You didn't order a proper sugary item! Adding $1000 to tab.") + cost += 1000 + + # Sales tax + cost *= 1.07 + + # Display output + print("Your total is $%.2f." % cost) + print() + + # Ask if the user wants to order again + ans = input("Do you want to order again (\"yes\" or \"no\")? ") + print() + if ans == "no": + break diff --git a/1_beginner/chapter7/practice/capital.py b/1_beginner/chapter7/practice/capital.py new file mode 100644 index 00000000..56372834 --- /dev/null +++ b/1_beginner/chapter7/practice/capital.py @@ -0,0 +1,14 @@ +""" +Capital + +Write a program that takes a string as input, +makes every other letter capital, sets the rest +of the characters to be lowercase, and displays +the new string. + +Hint: Make sure you don't count non-alphabetic +characters. + +Example input: I love Zee the Cat. +Example output: I lOvE zEe ThE cAt. +""" diff --git a/1_beginner/chapter7/practice/every_other_word.py b/1_beginner/chapter7/practice/every_other_word.py new file mode 100644 index 00000000..dd6fffa1 --- /dev/null +++ b/1_beginner/chapter7/practice/every_other_word.py @@ -0,0 +1,8 @@ +""" +Every Other Word + +Write a program that takes a sentence +as input and prints every other word. +""" + +# Insert your code here. diff --git a/1_beginner/chapter7/practice/phone_number.py b/1_beginner/chapter7/practice/phone_number.py new file mode 100644 index 00000000..425f458e --- /dev/null +++ b/1_beginner/chapter7/practice/phone_number.py @@ -0,0 +1,22 @@ +""" +Phone Number + +Write a program that asks the user to input a phone number. +Strip their input to get rid of any accidental spaces. + +If the input has anything besides digits or the input +isn’t exactly 10 digits long, The program should keep asking them +for a phone number until the input is formatted correctly. + +Display the phone number at the end. + +Example program run: +Enter a phone number (10 digits): 732-000-0000 +Enter a phone number (10 digits): abcdeabcde +Enter a phone number (10 digits): (908)9999999 +Enter a phone number (10 digits): 732000000 +Enter a phone number (10 digits): 1234567891 +Your number is 1234567891 +""" + +# Insert your code here. diff --git a/1_beginner/chapter7/practice/reverse_alphabet.py b/1_beginner/chapter7/practice/reverse_alphabet.py new file mode 100644 index 00000000..3bb59eea --- /dev/null +++ b/1_beginner/chapter7/practice/reverse_alphabet.py @@ -0,0 +1,14 @@ +""" +Reverse Alphabet (Challenge Problem) + +Create a list of strings by asking the user +to provide 10 inputs. Sort these in reverse +alphabetical order, and display the result. + +Note: "Cat" will come before "dog" because of ASCII values. + +To sort while IGNORING case, you might want to create +another list, make the elements all uppercase or all lowercase, +sort this list, and figure out how to use this list to +sort the original list. +""" diff --git a/1_beginner/chapter7/solutions/capital.py b/1_beginner/chapter7/solutions/capital.py new file mode 100644 index 00000000..f3d06284 --- /dev/null +++ b/1_beginner/chapter7/solutions/capital.py @@ -0,0 +1,31 @@ +""" +Capital + +Write a program that takes a string as input, +makes every other letter capital, sets the rest +of the characters to be lowercase, and displays +the new string. + +Hint: Make sure you don't count non-alphabetic +characters. + +Example input: I love Zee the Cat. +Example output: I lOvE zEe ThE cAt. +""" + +str = input("Enter a string: ") + +new_str = "" +letter_count = 0 # Used to avoid counting non-alpha characters. + +for c in str: + if c.isalpha(): + if letter_count % 2 == 0: + new_str += c.upper() + else: + new_str += c.lower() + letter_count += 1 + else: + new_str += c + +print(new_str) diff --git a/1_beginner/chapter7/solutions/every_other_word.py b/1_beginner/chapter7/solutions/every_other_word.py new file mode 100644 index 00000000..45e11a82 --- /dev/null +++ b/1_beginner/chapter7/solutions/every_other_word.py @@ -0,0 +1,17 @@ +""" +Every Other Word + +Write a program that takes a sentence +as input and prints every other word. + +Example input: I like cats, dogs, and pizza. +Example output: +I +cats, +and +""" + +sentence = input("Enter a sentence: ") +words = sentence.split() +for i in range(0, len(words), 2): + print(words[i]) diff --git a/1_beginner/chapter7/solutions/phone_number.py b/1_beginner/chapter7/solutions/phone_number.py new file mode 100644 index 00000000..951e4c3e --- /dev/null +++ b/1_beginner/chapter7/solutions/phone_number.py @@ -0,0 +1,26 @@ +""" +Phone Number + +Write a program that asks the user to input a phone number. +Strip their input to get rid of any accidental spaces. + +If the input has anything besides digits or the input +isn’t exactly 10 digits long, The program should keep asking them +for a phone number until the input is formatted correctly. + +Display the phone number at the end. + +Example program run: +Enter a phone number (10 digits): 732-000-0000 +Enter a phone number (10 digits): abcdeabcde +Enter a phone number (10 digits): (908)9999999 +Enter a phone number (10 digits): 732000000 +Enter a phone number (10 digits): 1234567891 +Your number is 1234567891 +""" + +number = "" +while len(number) != 10 or not number.isdigit(): + number = input("Enter a phone number (10 digits): ") + +print("Your number is", number) diff --git a/1_beginner/chapter7/solutions/reverse_alphabet.py b/1_beginner/chapter7/solutions/reverse_alphabet.py new file mode 100644 index 00000000..13b1170b --- /dev/null +++ b/1_beginner/chapter7/solutions/reverse_alphabet.py @@ -0,0 +1,38 @@ +""" +Reverse Alphabet (Challenge Problem) + +Create a list of strings by asking the user +to provide 10 inputs. Sort these in reverse +alphabetical order, and display the result. + +Note: "Cat" will come before "dog" because of ASCII values. + +To sort while IGNORING case, you might want to create +another list, make the elements all uppercase or all lowercase, +sort this list, and figure out how to use this list to +sort the original list. + +""" + +strings = [] + +# Get user input. +for i in range(10): + strings.append(input("Enter a string: ")) + +# Create a lowercase version of strings. +lower_strings = [] +for str in strings: + lower_strings.append(str.lower()) +lower_strings.sort() + +# Set the position of each string in strings to the +# position of the lowercase string in lower_strings. +sorted_strings = strings.copy() +for str in strings: + # i is the goal index of str. + i = lower_strings.index(str.lower()) + sorted_strings[i] = str + +# Display the sorted list. +print(sorted_strings) diff --git a/2_intermediate/chapter8/practice/food.py b/2_intermediate/chapter8/practice/food.py new file mode 100644 index 00000000..7530aa12 --- /dev/null +++ b/2_intermediate/chapter8/practice/food.py @@ -0,0 +1,11 @@ +""" +Food +Start with an empty list called food. +Ask the user to either enter a fruit, a vegetable, +or a junk food item. Do this 10 times, and each time, +the question you ask should be selected randomly. + +Add each of their answers to the list, sort the list +alphabetically, and then display the final list +by printing each item on a separate line. +""" diff --git a/2_intermediate/chapter8/practice/fortune.py b/2_intermediate/chapter8/practice/fortune.py new file mode 100644 index 00000000..4fc4e49d --- /dev/null +++ b/2_intermediate/chapter8/practice/fortune.py @@ -0,0 +1,10 @@ +""" +Fortune + +Store 6 fortunes cookie messages, and display a +random one each time the user runs the program. + +Assume that you DON'T know how many messages are in the list. + +(Hint: You’re randomly choosing the index to access.) +""" diff --git a/2_intermediate/chapter8/solutions/food.py b/2_intermediate/chapter8/solutions/food.py new file mode 100644 index 00000000..bdcd5fca --- /dev/null +++ b/2_intermediate/chapter8/solutions/food.py @@ -0,0 +1,30 @@ +""" +Food +Start with an empty list called food. +Ask the user to either enter a fruit, a vegetable, +or a junk food item. Do this 10 times, and each time, +the question you ask should be selected randomly. + +Add each of their answers to the list, sort the list +alphabetically, and then display the final list +by printing each item on a separate line. +""" +import random + +foods = [] +prompts = [ + "Enter a fruit: ", + "Enter a vegetable: ", + "Enter a junk food item: " +] + +# Choose a random prompt and collect input. +for i in range(10): + i = random.randrange(len(prompts)) + item = input(prompts[i]) + foods.append(item) +print() + +foods.sort() +for food in foods: + print(food) diff --git a/2_intermediate/chapter8/solutions/fortune.py b/2_intermediate/chapter8/solutions/fortune.py new file mode 100644 index 00000000..d7288e9f --- /dev/null +++ b/2_intermediate/chapter8/solutions/fortune.py @@ -0,0 +1,22 @@ +""" +Fortune + +Store 6 fortunes cookie messages, and display a +random one each time the user runs the program. + +Assume that you DON'T know how many messages are in the list. + +(Hint: You’re randomly choosing the index to access.) +""" +import random + +messages = [ + "You will meet the love of your life today.", + "You will turn into a fish.", + "It will rain diamonds today.", + "You will encounter a field of mangoes.", + "Look for the light. Underneath it, there will be a chess board.", + "You'll run a sub-6:00 mile today." +] + +print(messages[random.randrange(len(messages))]) From e7ab8711de7a4d5bab2f115828e74d26bad9bfcb Mon Sep 17 00:00:00 2001 From: Lint Action Date: Mon, 27 Jul 2020 01:54:25 +0000 Subject: [PATCH 2/6] Fix code style issues with Black --- 1_beginner/chapter3/solutions/integer.py | 2 +- 1_beginner/chapter3/solutions/names.py | 2 +- 1_beginner/chapter6/solutions/manipulation.py | 2 +- 1_beginner/chapter6/solutions/names.py | 2 +- 1_beginner/chapter6/solutions/restaurant.py | 2 +- 1_beginner/chapter7/solutions/capital.py | 2 +- 2_intermediate/chapter8/solutions/food.py | 2 +- 2_intermediate/chapter8/solutions/fortune.py | 12 ++++++------ 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/1_beginner/chapter3/solutions/integer.py b/1_beginner/chapter3/solutions/integer.py index 4f6b539e..b2f23268 100644 --- a/1_beginner/chapter3/solutions/integer.py +++ b/1_beginner/chapter3/solutions/integer.py @@ -10,6 +10,6 @@ x = float(input("Enter a number: ")) # Compare x to the integer version of itself -is_integer = (x == int(x)) +is_integer = x == int(x) print("Is integer? " + str(is_integer)) diff --git a/1_beginner/chapter3/solutions/names.py b/1_beginner/chapter3/solutions/names.py index 09da01fd..c48cedb3 100644 --- a/1_beginner/chapter3/solutions/names.py +++ b/1_beginner/chapter3/solutions/names.py @@ -10,6 +10,6 @@ name2 = input("Person 2: ") # "False" if name1 and name2 are equal -not_same = (name1 != name2) +not_same = name1 != name2 print("Not the same?", not_same) diff --git a/1_beginner/chapter6/solutions/manipulation.py b/1_beginner/chapter6/solutions/manipulation.py index 43463ed1..77da4574 100644 --- a/1_beginner/chapter6/solutions/manipulation.py +++ b/1_beginner/chapter6/solutions/manipulation.py @@ -45,7 +45,7 @@ if i % 3 == 0: more_nums.remove(i) -more_nums.insert(more_nums.index(20)+1, 21) +more_nums.insert(more_nums.index(20) + 1, 21) for i in range(15): more_nums.insert(-2, 0) diff --git a/1_beginner/chapter6/solutions/names.py b/1_beginner/chapter6/solutions/names.py index 751dddcf..ef98419a 100644 --- a/1_beginner/chapter6/solutions/names.py +++ b/1_beginner/chapter6/solutions/names.py @@ -14,7 +14,7 @@ # Some other possible answers: # group = people[0, 5, 2] or people[0, len(people), 2] -group = people[0:len(people):2] +group = people[0 : len(people) : 2] print(people) print(group) diff --git a/1_beginner/chapter6/solutions/restaurant.py b/1_beginner/chapter6/solutions/restaurant.py index bdd98111..1da48666 100644 --- a/1_beginner/chapter6/solutions/restaurant.py +++ b/1_beginner/chapter6/solutions/restaurant.py @@ -120,7 +120,7 @@ print() # Ask if the user wants to order again - ans = input("Do you want to order again (\"yes\" or \"no\")? ") + ans = input('Do you want to order again ("yes" or "no")? ') print() if ans == "no": break diff --git a/1_beginner/chapter7/solutions/capital.py b/1_beginner/chapter7/solutions/capital.py index f3d06284..6202367f 100644 --- a/1_beginner/chapter7/solutions/capital.py +++ b/1_beginner/chapter7/solutions/capital.py @@ -16,7 +16,7 @@ str = input("Enter a string: ") new_str = "" -letter_count = 0 # Used to avoid counting non-alpha characters. +letter_count = 0 # Used to avoid counting non-alpha characters. for c in str: if c.isalpha(): diff --git a/2_intermediate/chapter8/solutions/food.py b/2_intermediate/chapter8/solutions/food.py index bdcd5fca..9416d970 100644 --- a/2_intermediate/chapter8/solutions/food.py +++ b/2_intermediate/chapter8/solutions/food.py @@ -15,7 +15,7 @@ prompts = [ "Enter a fruit: ", "Enter a vegetable: ", - "Enter a junk food item: " + "Enter a junk food item: ", ] # Choose a random prompt and collect input. diff --git a/2_intermediate/chapter8/solutions/fortune.py b/2_intermediate/chapter8/solutions/fortune.py index d7288e9f..417fd1c1 100644 --- a/2_intermediate/chapter8/solutions/fortune.py +++ b/2_intermediate/chapter8/solutions/fortune.py @@ -11,12 +11,12 @@ import random messages = [ - "You will meet the love of your life today.", - "You will turn into a fish.", - "It will rain diamonds today.", - "You will encounter a field of mangoes.", - "Look for the light. Underneath it, there will be a chess board.", - "You'll run a sub-6:00 mile today." + "You will meet the love of your life today.", + "You will turn into a fish.", + "It will rain diamonds today.", + "You will encounter a field of mangoes.", + "Look for the light. Underneath it, there will be a chess board.", + "You'll run a sub-6:00 mile today.", ] print(messages[random.randrange(len(messages))]) From fcc5a20b5f024916f67e33c3ddb08b03f66d8c77 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Mon, 27 Jul 2020 01:55:34 +0000 Subject: [PATCH 3/6] Fix code style issues with Black --- 1_beginner/chapter3/solutions/temperature.py | 2 +- .../chapter5/practice/add_all_the_way.py | 4 +- 1_beginner/chapter5/practice/alternating.py | 4 +- 1_beginner/chapter5/practice/fibonnaci.py | 8 +- 1_beginner/chapter5/practice/prime.py | 4 +- 1_beginner/chapter5/practice/series.py | 2 +- 2_intermediate/chapter10/practice/img_avg.py | 4 +- 2_intermediate/chapter10/solutions/img_avg.py | 82 +++++++++---------- 8 files changed, 53 insertions(+), 57 deletions(-) diff --git a/1_beginner/chapter3/solutions/temperature.py b/1_beginner/chapter3/solutions/temperature.py index 88387da6..4601f780 100644 --- a/1_beginner/chapter3/solutions/temperature.py +++ b/1_beginner/chapter3/solutions/temperature.py @@ -1,4 +1,4 @@ - # Temperature +# Temperature # Write a program that converts Celsius to Fahrenheit. # It should ask the user for the temperature in # degrees Celsius and then print the temperature in degrees Fahrenheit. diff --git a/1_beginner/chapter5/practice/add_all_the_way.py b/1_beginner/chapter5/practice/add_all_the_way.py index 06b936cc..b91d4817 100644 --- a/1_beginner/chapter5/practice/add_all_the_way.py +++ b/1_beginner/chapter5/practice/add_all_the_way.py @@ -7,6 +7,4 @@ # write code here - - -#Try using the other loop and do the same p[roblem again \ No newline at end of file +# Try using the other loop and do the same p[roblem again diff --git a/1_beginner/chapter5/practice/alternating.py b/1_beginner/chapter5/practice/alternating.py index 1cbabb74..9ae5eb98 100644 --- a/1_beginner/chapter5/practice/alternating.py +++ b/1_beginner/chapter5/practice/alternating.py @@ -9,9 +9,9 @@ """ -#Write code here. +# Write code here. number = int(input("Enter Number Here: ")) -#Now try it with a while loop \ No newline at end of file +# Now try it with a while loop diff --git a/1_beginner/chapter5/practice/fibonnaci.py b/1_beginner/chapter5/practice/fibonnaci.py index 737d3c53..1aa27bde 100644 --- a/1_beginner/chapter5/practice/fibonnaci.py +++ b/1_beginner/chapter5/practice/fibonnaci.py @@ -1,4 +1,4 @@ -''' CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART! +""" CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART! The Fibonacci numbers, discovered by Leonardo di Fibonacci, is a sequence of numbers that often shows up in mathematics and, @@ -12,9 +12,9 @@ The challenge is to use a for loop (not recursion, if you know what that is), to find the 100th Fibonnaci number. -''' +""" -#write code here +# write code here -#Can you do it with a while loop? \ No newline at end of file +# Can you do it with a while loop? diff --git a/1_beginner/chapter5/practice/prime.py b/1_beginner/chapter5/practice/prime.py index fd776fd5..e631641e 100644 --- a/1_beginner/chapter5/practice/prime.py +++ b/1_beginner/chapter5/practice/prime.py @@ -9,7 +9,7 @@ """ -#write code here +# write code here numer = int(input("Enter number here: ")) @@ -25,4 +25,4 @@ """ -#write code here \ No newline at end of file +# write code here diff --git a/1_beginner/chapter5/practice/series.py b/1_beginner/chapter5/practice/series.py index 6628525f..c8ea06e8 100644 --- a/1_beginner/chapter5/practice/series.py +++ b/1_beginner/chapter5/practice/series.py @@ -13,4 +13,4 @@ Also, try adding up 1 -1/3 + 1/5 - 1/7 + 1/9 - 1/11 ... 10 million times. then multiply this result by 4. What number is this close to? -""" \ No newline at end of file +""" diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index 240e66df..34d7c040 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -37,10 +37,10 @@ plt.imshow(img) plt.show() -#write code to create newimg here +# write code to create newimg here plt.imshow(newimg) plt.show() plt.imshow(transpose) -plt.show() \ No newline at end of file +plt.show() diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 10ecfb3e..2961ce16 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -35,50 +35,48 @@ transpose = numpy.transpose(img) -#write code to create newimg here +# write code to create newimg here def solution1(): - """Iterating over the image here. i is a variable from 0 to the width of the image. + """Iterating over the image here. i is a variable from 0 to the width of the image. j is a variable that ranges from 0 to the height of the image. i is associated with values""" - for i in range(len(img)): - for j in range(len(img[0])): - x_n = [0] - y_n = [0] - - if(i == 0): - x_n.append(1) - elif(i == len(img)-1): - x_n.append(-1) - else: - x_n.append(1) - x_n.append(-1) - - if(j == 0): - y_n.append(1) - elif(j == len(img[0])-1): - y_n.append(-1) - else: - y_n.append(1) - y_n.append(-1) - - r_avg = -1*img[i][j][0] - g_avg = -1*img[i][j][1] - b_avg = -1*img[i][j][2] - c = -1 - - for x in x_n: - for y in y_n: - r_avg += img[i+x][j+y][0] - g_avg += img[i+x][j+y][1] - b_avg += img[i+x][j+y][2] - c+=1 - r_avg = r_avg/c - g_avg = g_avg/c - b_avg = b_avg/c - - newimg[i][j] = [r_avg, g_avg, b_avg] - - + for i in range(len(img)): + for j in range(len(img[0])): + x_n = [0] + y_n = [0] + + if i == 0: + x_n.append(1) + elif i == len(img) - 1: + x_n.append(-1) + else: + x_n.append(1) + x_n.append(-1) + + if j == 0: + y_n.append(1) + elif j == len(img[0]) - 1: + y_n.append(-1) + else: + y_n.append(1) + y_n.append(-1) + + r_avg = -1 * img[i][j][0] + g_avg = -1 * img[i][j][1] + b_avg = -1 * img[i][j][2] + c = -1 + + for x in x_n: + for y in y_n: + r_avg += img[i + x][j + y][0] + g_avg += img[i + x][j + y][1] + b_avg += img[i + x][j + y][2] + c += 1 + r_avg = r_avg / c + g_avg = g_avg / c + b_avg = b_avg / c + + newimg[i][j] = [r_avg, g_avg, b_avg] solution1() @@ -87,4 +85,4 @@ def solution1(): plt.show() plt.imshow(transpose) -plt.show() \ No newline at end of file +plt.show() From 91d150826dc3f91c572a0d5ed20c397409526e55 Mon Sep 17 00:00:00 2001 From: Neha Peddinti Date: Sat, 1 Aug 2020 13:45:40 -0400 Subject: [PATCH 4/6] Add practice problems/solutions to Ch.9-11 --- 2_intermediate/chapter10/practice/address.py | 17 +++++ .../chapter10/practice/random_grid.py | 40 ++++++++++++ 2_intermediate/chapter10/solutions/address.py | 32 +++++++++ .../chapter10/solutions/random_grid.py | 56 ++++++++++++++++ 2_intermediate/chapter11/practice/case.py | 24 +++++++ 2_intermediate/chapter11/practice/product.py | 16 +++++ 2_intermediate/chapter11/practice/rect.py | 30 +++++++++ 2_intermediate/chapter11/solutions/case.py | 34 ++++++++++ 2_intermediate/chapter11/solutions/product.py | 29 +++++++++ 2_intermediate/chapter11/solutions/rect.py | 41 ++++++++++++ 2_intermediate/chapter9/practice/catalog.py | 36 ++++++++++ 2_intermediate/chapter9/practice/word.py | 19 ++++++ 2_intermediate/chapter9/solutions/catalog.py | 65 +++++++++++++++++++ 2_intermediate/chapter9/solutions/word.py | 37 +++++++++++ 14 files changed, 476 insertions(+) create mode 100644 2_intermediate/chapter10/practice/address.py create mode 100644 2_intermediate/chapter10/practice/random_grid.py create mode 100644 2_intermediate/chapter10/solutions/address.py create mode 100644 2_intermediate/chapter10/solutions/random_grid.py create mode 100644 2_intermediate/chapter11/practice/case.py create mode 100644 2_intermediate/chapter11/practice/product.py create mode 100644 2_intermediate/chapter11/practice/rect.py create mode 100644 2_intermediate/chapter11/solutions/case.py create mode 100644 2_intermediate/chapter11/solutions/product.py create mode 100644 2_intermediate/chapter11/solutions/rect.py create mode 100644 2_intermediate/chapter9/practice/catalog.py create mode 100644 2_intermediate/chapter9/practice/word.py create mode 100644 2_intermediate/chapter9/solutions/catalog.py create mode 100644 2_intermediate/chapter9/solutions/word.py diff --git a/2_intermediate/chapter10/practice/address.py b/2_intermediate/chapter10/practice/address.py new file mode 100644 index 00000000..3234b99e --- /dev/null +++ b/2_intermediate/chapter10/practice/address.py @@ -0,0 +1,17 @@ +""" +Address + +Create a 2D list where each row represents a +person and has 3 elements: their name, their age, +and their address. + +The entire list should have 4 such entries (4 people), +so it will be a 4x3 list. + +Display the name and address of the 2nd person in the list. + +Then, display the entire list with the format: +name (age): address +""" + +# Insert your code here. diff --git a/2_intermediate/chapter10/practice/random_grid.py b/2_intermediate/chapter10/practice/random_grid.py new file mode 100644 index 00000000..d579d720 --- /dev/null +++ b/2_intermediate/chapter10/practice/random_grid.py @@ -0,0 +1,40 @@ +""" +Random Grid + +Create a 2D list with 4 rows and a randomly +determined number of columns. The column +number should be a random EVEN number between +2 and 16 (inclusive). + +All the even column numbers (including 0) should +be filled with asterisks (*). The odd numbered +columns should be filled with underscores (_). + +Display the grid at the end by printing out +elements individually: don't use print(list). +Assume that you don't know the size of the grid +beforehand. In other words, if you wanted to display +the 2D list without knowing the number of rows and +columns in it, how would you code this? + +For example, a 4x6 grid would display this: +*_*_*_ +*_*_*_ +*_*_*_ + +This might be useful: +print("a") +print("a") +would display the "a"s with newlines: +a +a + +print("a", end=" ") +print("a") +changes the end of the first "a" from a newline to a space. +The output is this: +a a + +""" + +# Insert your code here. diff --git a/2_intermediate/chapter10/solutions/address.py b/2_intermediate/chapter10/solutions/address.py new file mode 100644 index 00000000..6611d996 --- /dev/null +++ b/2_intermediate/chapter10/solutions/address.py @@ -0,0 +1,32 @@ +""" +Address + +Create a 2D list where each row represents a +person and has 3 elements: their name, their age, +and their address. + +The entire list should have 4 such entries (4 people), +so it will be a 4x3 list. + +Display the name and address of the 2nd person in the list. + +Then, display the entire list with the format: +name (age): address +""" + +contacts = [ + ["Jeremy", 10, "45 Pancake Road"], + ["Nicey", 18, "111 Cupcake Street"], + ["Hawthorne", 15, "19 Sinister Avenue"], + ["Nilah", 14, "Banks of the Nile River"] +] + +# 2nd person +print(contacts[1][0] + ": " + contacts[1][2]) +print() + +# Display the entire list. +for contact in contacts: + print( + contact[0] + " (%d): " % contact[1] + contact[2] + ) diff --git a/2_intermediate/chapter10/solutions/random_grid.py b/2_intermediate/chapter10/solutions/random_grid.py new file mode 100644 index 00000000..cf27b122 --- /dev/null +++ b/2_intermediate/chapter10/solutions/random_grid.py @@ -0,0 +1,56 @@ +""" +Random Grid + +Create a 2D list with 4 rows and a randomly +determined number of columns. The column +number should be a random EVEN number between +2 and 16 (inclusive). + +All the even column numbers (including 0) should +be filled with asterisks (*). The odd numbered +columns should be filled with underscores (_). + +Display the grid at the end by printing out +elements individually: don't use print(list). +Assume that you don't know the size of the grid +beforehand. In other words, if you wanted to display +the 2D list without knowing the number of rows and +columns in it, how would you code this? + +For example, a 4x6 grid would display this: +*_*_*_ +*_*_*_ +*_*_*_ + +This might be useful: +print("a") +print("a") +would display the "a"s with newlines: +a +a + +print("a", end=" ") +print("a") +changes the end of the first "a" from a newline to a space. +The output is this: +a a + +""" +import random + +grid = [] +cols = random.randint(1, 8) * 2 + +# Fill the grid 2D list. +for row in range(4): + grid.append([]) + for col in range(cols): + grid[row].append( + "*" if (col % 2 == 0) else "_" + ) + +# Display the grid without knowing the size beforehand. +for row in range(len(grid)): + for col in range(len(grid[row])): + print(grid[row][col], end="") + print() diff --git a/2_intermediate/chapter11/practice/case.py b/2_intermediate/chapter11/practice/case.py new file mode 100644 index 00000000..e5f9622d --- /dev/null +++ b/2_intermediate/chapter11/practice/case.py @@ -0,0 +1,24 @@ +""" +Case + +Display the string "Apple" in the following formats: +1) normally +2) all uppercase +3) all lowercase + +Display the string "mRoWiE" in the same 3 formats. + +Ask the user to input a sentence, and display this +input in the same 3 formats. + +Do this in AT MOST 8 lines of code. +By the end of the program, 9 lines should have been +displayed (3 formats for each of the 3 strings). + +Example of the 3 formats for one string: +Apple +APPLE +apple +""" + +# Insert your code here. diff --git a/2_intermediate/chapter11/practice/product.py b/2_intermediate/chapter11/practice/product.py new file mode 100644 index 00000000..8e8ee4d7 --- /dev/null +++ b/2_intermediate/chapter11/practice/product.py @@ -0,0 +1,16 @@ +""" +Product + +Write a function that takes a list +of numbers as input and returns +the product of all the numbers in +the list. + +Use it to print the products of the +following sets of numbers: +-1, 5, 3, 2, 8 +2.5, 3, 0 +4, 3, 7, 10 +""" + +# Insert your code here. diff --git a/2_intermediate/chapter11/practice/rect.py b/2_intermediate/chapter11/practice/rect.py new file mode 100644 index 00000000..78461ada --- /dev/null +++ b/2_intermediate/chapter11/practice/rect.py @@ -0,0 +1,30 @@ +""" +Rect + +Write a function that takes in 2 integer +parameters: length, and width. + +The function should print out a rectangle of +asterisks (*) with that length and width. + +Example, if the length is 5 and the width is 3, +the function should print: +***** +***** +***** + +Useful information: +1) print("a", end="") + removes the newline from the end of the print statement. +2) print("a" * 5) displays "aaaaa". + +Use the function to display rectangles with +the following dimensions (with a linebreak +between each one): +2x6 +7x4 +3x5 + +""" + +# Insert your code here. diff --git a/2_intermediate/chapter11/solutions/case.py b/2_intermediate/chapter11/solutions/case.py new file mode 100644 index 00000000..0fe9c1f5 --- /dev/null +++ b/2_intermediate/chapter11/solutions/case.py @@ -0,0 +1,34 @@ +""" +Case + +Display the string "Apple" in the following formats: +1) normally +2) all uppercase +3) all lowercase + +Display the string "mRoWiE" in the same 3 formats. + +Ask the user to input a sentence, and display this +input in the same 3 formats. + +Do this in AT MOST 8 lines of code. +By the end of the program, 9 lines should have been +displayed (3 formats for each of the 3 strings). + +Example of the 3 formats for one string: +Apple +APPLE +apple +""" + + +# Define a function that prints the 3 formats. +def display(str): + print(str) + print(str.upper()) + print(str.lower()) + + +display("Apple") +display("mRoWiE") +display(input("Enter a sentence: ")) diff --git a/2_intermediate/chapter11/solutions/product.py b/2_intermediate/chapter11/solutions/product.py new file mode 100644 index 00000000..3318d52e --- /dev/null +++ b/2_intermediate/chapter11/solutions/product.py @@ -0,0 +1,29 @@ +""" +Product + +Write a function that takes a list +of numbers as input and returns +the product of all the numbers in +the list. + +Use it to print the products of the +following sets of numbers: +-1, 5, 3, 2, 8 +2.5, 3, 0 +4, 3, 7, 10 +""" + + +# Define a product() function with a list parameter. +def product(list): + product = 1 + for i in list: + product *= i + return product + + +# Use the function to display products, where +# each set of numbers is given as a list. +print(product([-1, 5, 3, 2, 8])) +print(product([2.5, 3, 0])) +print(product([4, 3, 7, 10])) diff --git a/2_intermediate/chapter11/solutions/rect.py b/2_intermediate/chapter11/solutions/rect.py new file mode 100644 index 00000000..41f48d5e --- /dev/null +++ b/2_intermediate/chapter11/solutions/rect.py @@ -0,0 +1,41 @@ +""" +Rect + +Write a function that takes in 2 integer +parameters: length, and width. + +The function should print out a rectangle of +asterisks (*) with that length and width. + +Example, if the length is 5 and the width is 3, +the function should print: +***** +***** +***** + +Useful information: +1) print("a", end="") + removes the newline from the end of the print statement. +2) print("a" * 5) displays "aaaaa". + +Use the function to display rectangles with +the following dimensions (with a linebreak +between each one): +2x6 +7x4 +3x5 + +""" + + +# Define a function with length and width parameters. +def draw_rect(length, width): + for row in range(width): + print("*" * length) + print() + + +# Use the function to draw rectangles of various sizes. +draw_rect(2, 6) +draw_rect(7, 4) +draw_rect(3, 5) diff --git a/2_intermediate/chapter9/practice/catalog.py b/2_intermediate/chapter9/practice/catalog.py new file mode 100644 index 00000000..3b3938f3 --- /dev/null +++ b/2_intermediate/chapter9/practice/catalog.py @@ -0,0 +1,36 @@ +""" +Catalog + +Write a program that takes asks the +user whether they'd like to add, delete, +or clear the entries in a store catalog. + +After they perform some action, the program +should display the updated dictionary in the +format: + item1: price1 + item2: price2 + etc. + +Keep asking them if they'd like to add, delete, +or clear entries until they enter "q". + +Possible actions: +If they enter "add": + Ask them to enter an item. + Ask them to enter a price. + The item should be the key, and the price + should be the value in the dictionary. + +If they enter "delete": + Ask them to to enter which item + they'd like to delete. + +If they enter "clear": + Clear all the entries from the dictionary. + +If they enter "q": + Display the final dictionary and end the program. +""" + +# Insert your code here. diff --git a/2_intermediate/chapter9/practice/word.py b/2_intermediate/chapter9/practice/word.py new file mode 100644 index 00000000..a11cec0e --- /dev/null +++ b/2_intermediate/chapter9/practice/word.py @@ -0,0 +1,19 @@ +""" +Word + +Store words (keys) and definitions (values) +in a dictionary in a random order. + +Then, ask the user to input a word. +If the word is in the dictionary, +use it to display the definition. +Otherwise, print out a message saying +that the word is not there. + +Display the entire dictionary at the end in the format: +word1: definition1 +word2: definition2 +etc. +""" + +# Insert your code here. diff --git a/2_intermediate/chapter9/solutions/catalog.py b/2_intermediate/chapter9/solutions/catalog.py new file mode 100644 index 00000000..abb304cd --- /dev/null +++ b/2_intermediate/chapter9/solutions/catalog.py @@ -0,0 +1,65 @@ +""" +Catalog + +Write a program that takes asks the +user whether they'd like to add, delete, +or clear the entries in a store catalog. + +After they perform some action, the program +should display the updated dictionary in the +format: + item1: price1 + item2: price2 + etc. + +Keep asking them if they'd like to add, delete, +or clear entries until they enter "q". + +Possible actions: +If they enter "add": + Ask them to enter an item. + Ask them to enter a price. + The item should be the key, and the price + should be the value in the dictionary. + +If they enter "delete": + Ask them to to enter which item + they'd like to delete. + +If they enter "clear": + Clear all the entries from the dictionary. + +If they enter "q": + Display the final dictionary and end the program. +""" + +catalog = {} + +while True: + # Display catalog + print("Current catalog:") + if len(catalog) != 0: + for item, price in catalog.items(): + print(item + ": $" + price) + else: + print("Your catalog is empty.") + print() + + ans = input( + "Would you like to add, delete, " + "or clear entries from your catalog? " + ) + + # Perform actions depending on input + if ans == "q": + break + elif ans == "add": + item = input("Enter an item to add: ") + price = input("Enter the price: $") + catalog[item] = price + elif ans == "delete": + item = input("Enter an item to delete: ") + del catalog[item] + elif ans == "clear": + catalog.clear() + print() diff --git a/2_intermediate/chapter9/solutions/word.py b/2_intermediate/chapter9/solutions/word.py new file mode 100644 index 00000000..280de1c3 --- /dev/null +++ b/2_intermediate/chapter9/solutions/word.py @@ -0,0 +1,37 @@ +""" +Word + +Store words (keys) and definitions (values) +in a dictionary in a random order. + +Then, ask the user to input a word. +If the word is in the dictionary, +use it to display the definition. +Otherwise, print out a message saying +that the word is not there. + +Display the entire dictionary at the end in the format: +word1: definition1 +word2: definition2 +etc. +""" + +dictionary = { + "monkey": "a cute human", + "banana": "sustenance for cute humans", + "neha": "a really strange human", + "phone": "magic communication device", + "cookie": "heaven on Earth" +} + +# Show definition +word = input("Enter a word: ") +if word in dictionary: + print("Definition:", dictionary.get(word)) +else: + print("This word cannot be found.") +print() + +# Display formatted dictionary +for key, value in dictionary.items(): + print(key + ": " + value) From 6c84df764ba9721b0db305db029849d04d1180fc Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sat, 1 Aug 2020 18:12:37 +0000 Subject: [PATCH 5/6] Fix code style issues with Black --- 2_intermediate/chapter10/solutions/address.py | 6 ++---- 2_intermediate/chapter10/solutions/random_grid.py | 4 +--- 2_intermediate/chapter9/solutions/word.py | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/2_intermediate/chapter10/solutions/address.py b/2_intermediate/chapter10/solutions/address.py index 6611d996..50e69f13 100644 --- a/2_intermediate/chapter10/solutions/address.py +++ b/2_intermediate/chapter10/solutions/address.py @@ -18,7 +18,7 @@ ["Jeremy", 10, "45 Pancake Road"], ["Nicey", 18, "111 Cupcake Street"], ["Hawthorne", 15, "19 Sinister Avenue"], - ["Nilah", 14, "Banks of the Nile River"] + ["Nilah", 14, "Banks of the Nile River"], ] # 2nd person @@ -27,6 +27,4 @@ # Display the entire list. for contact in contacts: - print( - contact[0] + " (%d): " % contact[1] + contact[2] - ) + print(contact[0] + " (%d): " % contact[1] + contact[2]) diff --git a/2_intermediate/chapter10/solutions/random_grid.py b/2_intermediate/chapter10/solutions/random_grid.py index cf27b122..20a7e9cc 100644 --- a/2_intermediate/chapter10/solutions/random_grid.py +++ b/2_intermediate/chapter10/solutions/random_grid.py @@ -45,9 +45,7 @@ for row in range(4): grid.append([]) for col in range(cols): - grid[row].append( - "*" if (col % 2 == 0) else "_" - ) + grid[row].append("*" if (col % 2 == 0) else "_") # Display the grid without knowing the size beforehand. for row in range(len(grid)): diff --git a/2_intermediate/chapter9/solutions/word.py b/2_intermediate/chapter9/solutions/word.py index 280de1c3..14a61180 100644 --- a/2_intermediate/chapter9/solutions/word.py +++ b/2_intermediate/chapter9/solutions/word.py @@ -21,7 +21,7 @@ "banana": "sustenance for cute humans", "neha": "a really strange human", "phone": "magic communication device", - "cookie": "heaven on Earth" + "cookie": "heaven on Earth", } # Show definition From 5bdf1b3b5b4d02f207b75286a8b6db768cf285ef Mon Sep 17 00:00:00 2001 From: Neha Peddinti Date: Sat, 1 Aug 2020 14:33:48 -0400 Subject: [PATCH 6/6] Modify random_grid solution --- 2_intermediate/chapter10/solutions/random_grid.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/2_intermediate/chapter10/solutions/random_grid.py b/2_intermediate/chapter10/solutions/random_grid.py index cf27b122..6591d4a8 100644 --- a/2_intermediate/chapter10/solutions/random_grid.py +++ b/2_intermediate/chapter10/solutions/random_grid.py @@ -50,7 +50,7 @@ ) # Display the grid without knowing the size beforehand. -for row in range(len(grid)): - for col in range(len(grid[row])): - print(grid[row][col], end="") +for row in grid: + for col in row: + print(col, end="") print()