# 1.12. Chapter Assessment¶

Why create an algorithm?

• Because computers are better at solving problems.
• Computers aren't necessarily better at solving problems, though often they can be quicker than humans. Additionally, algorithms can be used to solve non-computer related problems.
• So that you don't have to solve the problem yourself.
• While it is beneficial to have a set of instructions that others can follow, this isn't the best answer. By creating the algorithm, you solve a problem for yourself and others.
• So that you have a general solution to a problem.
• Yes, by creating a general solution you can then express it as a program if you choose, and then use a computer to automate the execution.
• Because you need a set of instructions to follow.
• While it is beneficial to have a set of instructions as that is what an algorithm **is**, it is not **why** we would want to create one.

Write code to print out the phrase “Hello World”.

## 1.12.1. Contributed Exercises¶

Q-1: What is the correct line below for printing I am a string! on the standard output?
• print(I am a string!)
• you need to enclose the string in quotes.
• I am a string!
• there is not function specified for printing.
• print("I am a string!")
• this is correct.
        Q-1: Assume you need to write a program to convert temperature expressed in Fahrenheit into Celsius degrees. The formula is the following: T(°C) = (T(°F) - 32) × 5/9. That is, temperature in Celsius is equal to the given temperature in Fahrenheit minus 32 and then multiplied by 5/9. Choose the right step sequence for the user to use your program.user inputs temperature in Fahrenheit
---
your program stores the value in Fahrenheit into a variable called Fahr
---
you create a variable called Cels
---
Cels = (Fahr - 32) × 5/9.
---
output Cels so that the user sees the result
---

Q-1: Why create an algorithm?
• Because computers are better at solving problems
• Computers aren't necessarily better at solving problems, though often they can be quicker than humans. Additionally, algorithms can be used to solve non-computer related problems.
• So that you don't have to solve the problem yourself
• While it is beneficial to have a set of instructions that others can follow, this isn't the best answer. By creating the algorithm, you solve a problem for yourself and others.
• So that you have a general solution to a problem
• Yes, by creating a general solution you can then express it as a program if you choose, and then use a computer to automate the execution
• Because you need a set of instructions to follow.
• While it is beneficial to have a set of instructions as that is what an algorithm **is**, it is not **why** we would want to create one.

Initialize two variables to values 9 and 2 respectively. Then:

1. increment each variable first by value 2 (means you update those variables)

2. integer divide the two variables (divide the larger value with the lower one), and assign the result to another variable called c

3. print the variable c and make sure you have the desired result

Initialize two variables to values 9 and 2 respectively. Then:

1. increment each variable first by value 2 (means you update those variables)

2. integer divide the two variables (divide the larger value with the lower one), and assign the result to another variable called c

3. print the variable c and make sure you have the desired result

Write a statement to print the message “Hello, WVU!”.

Assign the value 5 to a variable called a.

Assign the value 5 to a variable called a.

available_files

school_prompt.txt

Error Check: The following code has 4 errors that are preventing it from running. Find and correct the errors. (Hint: use print statements to help identify some of the errors). The original task was:Using the file school_prompt.txt, if the character ‘p’ is in a word, then add the word to a list called p_words.

available_files

school_prompt.txt

Error Check: The following code has 4 errors that are preventing it from running. Find and correct the errors. (Hint: use print statements to help identify some of the errors). The original task was:Using the file school_prompt.txt, if the character ‘p’ is in a word, then add the word to a list called p_words.

        Q-1: Piece together a program so that it can successfully print out one print statement, given the value of x.i = 0
---
while i < 10:
---
i = i + 1
---
print(i)


Q-1: Match the mascot to its school. Try again!
• Spartan
• Michigan State University
• Wolverine
• University of Michigan
• Buckeye
• Ohio State University
On a scale of 1-5, how much did you enjoy this course?
• 1
• 2
• 3
• 4
• 5
• Python
• Java
• C++

Write a Python function to sum the first N numbers starting with 0. So if N is 4 then your function should add 0 + 1 + 2 + 3

Write a Python function to sum the first N numbers starting with 0. So if N is 4 then your function should add 0 + 1 + 2 + 3

Q-1: Consider the following problem:

line 1: a = 2 (the value of a is 2)

line 2: b = 3 (the value of b is 3)

line 3: c = a + b (the value of c is a + b)

Can you predict what could be the value of c in line 3?

• 5
• Correct! It's 5 because a is 2, b is 3 and a + b is 2 + 3 = 5.
• 2
• Oh no. c is assigned the result of a + b.
• 3
• Oh no. c is assigned the result of a + b.

Q-1: Consider the following program (you read line by line, top to bottom):

line 1: m = 3
line 2: a = 0
line 3: b = 10
line 4: c = b / a
line 5: d = c + 3


But oops, you realize that in line 4 you cannot divide the value of b by 0 (not allowed in math). Which line of code will you need to change in order to succesfully divide b by something that doesn’t have the value of 0?

• Line 1
• Oh no. The value of m doesn't need changes since it is not involved in the division in Line 4.
• Line 2
• Correct! Namely you can change the value of a to be anything but a 0, and the error in line 4 will be avoided. Well done!
• Line 3
• Oh no. The value of b doesn't need since it is being divided.
• Line 5
• Oh no. Line 5 is after the problematic line 4 and even if you fix it, the error will still be in line 4. You need to change the code AT or BEFORE line 4 in order to fix it.

Q-1: Consider the following program you read line by line, top to bottom:

line 1: my_name = "Harry"
line 2: your_name = "Hermione"
line 3: print(my_name)


Python will execute the program starting from line 1, to line 2 and then to line 3 (top to bottom). Since there is no more code it will stop. What will Python print on your console when it executes the final line (line 3)?

• Harry
• Correct! It will print whatever value the variable my_name holds, and that is Harry.
• Hermione
• Look a bit more careful. Python prints the value of the variable called my_name. What is the value of my_name in the program?

Q-1: Consider the following program you read top to bottom:

line 1: a = 2
line 2: b = 3
line 3: c = a + b


What would be the final value of c if the value for a is changed to 7.

• 5
• Oh no. The value of a is now 7, the value of b is still 3, so what is a + b equal to?
• 10
• Correct. c = a + b = 7 + 3 = 10
• 3
• Oh no. The value of a is now 7, the value of b is still 3, so what is a + b equal to?

Q-1: Consider the following program (you read line by line, top to bottom):

line 1: m = 3
line 2: a = 0
line 3: b = 10
line 4: c = b / a
line 5: d = c + 3


But oops, you realize that in line 4 you cannot divide the value of b by 0 (not allowed in math). Which line of code will you need to change in order to successfully divide b by something that doesn’t have the value of 0?

• Line 1
• Oh no. The value of m doesn't need changes since it is not involved in the division in Line 4.
• Line 2
• Correct! Namely you can change the value of a to be anything but a 0, and the error in line 4 will be avoided. Well done!
• Line 3
• Oh no. The value of b doesn't need changes since it is being divided.
• Line 5
• Oh no. Line 5 is after the problematic line 4 and even if you fix it, the error will still be in line 4. You need to change the code AT or BEFORE line 4 in order to fix it.

Write code to print out the following in complete sentences on separate lines: your name and pronunciation, your programming experience, hometown, class standing (Freshman, Sophomore, Junior, Senior), major, and minors. Example output:

My name is Hermione Granger (her-my-oh-nee).

I have programmed using Java.

I come from Heathgate, Hamstead. …

Q-1: Consider the following problem:

line 1: a = 2 (the value of a is 2)

line 2: b = 3 (the value of b is 3)

line 3: c = b - a (the value of c is b - a)

Predict the value of c in line 3.

• 1
• Correct! It's 1 because a is 2, b is 3 and b - a is 3 - 2 = 1.
• 2
• Oh no. c is assigned the result of b - a.
• 3
• Oh no. c is assigned the result of b - a.
        Q-1: Assume you need to write a program to compute the total sale amount of a purchase including sales tax.
The formula in Idaho is the following: total = purchase + purchase * 0.06. That is, the total sale amount
is the purchase amount plus 6% = 0.06 of the purchase amount.
Choose the right step sequence for the user to use your program. Note that your first Checked answer will be graded for the Quiz.user inputs the total purchase price
---
your program stores the provided value into a variable called purchase
---
you create a variable called total_sale
---
total_sale = purchase + purchase * 0.06
---
output total_sale so that the user sees the result
---

        Q-1: Assume you need to write a program to compute the sale price of an item which is advertised as 40% off.
The formula for the sale price is the following: sale price = original price * 0.6. That is, the sale price of the item
is 60% of the original price or original price times 0.6.
Choose the right step sequence for the user to use your program. Note that your first Checked answer will be graded for the Quiz.user inputs the original price
---
your program stores the provided value into a variable called original
---
you create a variable called sale_price
---
sale_price = original * 0.6
---
output sale_price so that the user sees the result
---

        Q-1: Assume you need to write a program to compute the total sale amount of a purchase including sales tax.
The formula in Idaho is the following: total = purchase + purchase * 0.06. That is, the total sale amount
is the purchase amount plus 6% = 0.06 of the purchase amount.
Choose the right step sequence for the user to use your program.user inputs the total purchase price
---
your program stores the provided value into a variable called purchase
---
you create a variable called total_sale
---
total_sale = purchase + purchase * 0.06
---
output total_sale so that the user sees the result
---


Q-1: Given the following sequence of stack operations, what is the top item on the stack when the sequence is complete?

m = Stack()
m.push('x')
m.push('y')
m.push('z')
while not m.isEmpty():
m.pop()
m.pop()

• 'x'
• You may want to check out the docs for isEmpty
• the stack is empty
• There is an odd number of things on the stack but each time through the loop 2 things are popped.
• an error will occur
• Good Job.
• 'z'
• You may want to check out the docs for isEmpty
        Q-1: Solve my really cool parsons problem...if you can.num1 = int( input( "enter the first number "))
num2 = int( input( "enter the second number "))
---
---

        Q-1: This one is harder - not all the blocks are needednum1 = int( input( "enter the first number "))
---
num2 = int( input( "enter the second number "))
---
---
---
num1 = input( "enter the first number ") #distractor
---
num2 = input( "enter the second number ") #distractor


Write a Python function to sum the first N numbers starting with 0. So if N is 4 then your function should add 0 + 1 + 2 + 3

        Q-1: This one is harder - for some of the blocks you need to choose between twonum1 = int( input( "enter the first number "))
---
num1 = input( "enter the first number ") #paired
---
num2 = int( input( "enter the second number "))
---
num2 = input( "enter the second number ") #paired
---
---


Q-1: The word algorithm comes from what?

• The Latinized name of Muḥammad ibn Mūsā al-Khwārizmī, a Persian mathematician.
• The Roman god of Math, Algormiti.
• The name of Charles Babbage's computing machine.
• The English translation of .Entscheidungsproblem.
• The title, in Farsi, of a legendary lost Queen album.

• Python

• Yes, Python is a great language to learn, whether you are a beginner or an experienced programmer. The correct answer is designated by using a plus sign before the answer.

• Java

• Java is a good object oriented language but it has some details that make it hard for the beginner.

• C

• C is an imperative programming language that has been around for a long time, but it is not the one that we use.

• ML

• No, ML is a functional programming language. You can use Python to write functional programs as well.