# 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¶

Write a program that extracts the last 5 items in the list players and assigns it to a new list named last_players.
Write one for loop to print out each character in the string team on a separate line.
Write code that uses iteration to print out the length of each element of the list stored in players.
Write code to count the number of characters in player_str using the accumulation pattern and assign the answer to a variable num_chars. DO NOT use the len function to solve the problem.
player_avg is a string that contains batting averages for baseball players separated by commas. Write code to compute the number of players that batted greater than 0.299. Store the result in the variable num_players_over_300.
Write code that count the number of words in player_str that contains either an “o” or an “e”. Store the result in the variable num_o_or_e.
Write code so that if “RED SOX” is in the list of best sports franchises, then the string “There is justice in the world!” is assigned to the variable resp. Otherwise, the string “We were robbed!” should be assigned to resp.
Write code to create a list of word lengths for the words in player_str using the accumulation pattern and assign the answer to variable num_words_list. Use the len function.
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: 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: 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: 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: 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: Put in order the elements of the pipeline for translating high-
level languages into low-level languages:
-----
source code
=====
compiler
=====
object code
=====
executor
=====
output


Q-1: baypath15: 67%, baypath20: 37%, baypath12: 67%, baypath18: 73%, baypath11: 63%, baypath14: 53%, baypath17: 53%, baypath13: 40%

        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: 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 provided value 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.
Q-1: Junte os pares corretamente. Feedback that is displayed if things are incorrectly matched
• Draggable element text
• Dropzone to be matched with text
• Draggable text
• Text of dropzone
Q-1: Junte os pares corretamente. Feedback that is displayed if things are incorrectly matched
• arraste para opção a
• A
• arraste para opção b
• B
• arraste para opção c
• C
        Q-1: Put in order the elements of the pipeline for translating high-
level languages into low-level languages:source code
---
compiler
---
object code
---
executor
---
output
---

        Q-1: Construct a program that creates a pointer called foobar and compares its values with the int foo.  Print "Hello World" if they are equal.
print("Hello world")

        Q-1: Construct a block of code that correctly implements the accumulator pattern.template < class InputIt, class T >
T sum(InputIt first, InputIt last, T value)
{
while (first != last) {
value = value + *first;
++first;
}
return value;
}

        Q-1: Construct a block of code that correctly implements the accumulator pattern.template < class InputIt, class T >
T sum(InputIt first, InputIt last, T value)
{
while (first != last) {
value = value + *first;
++first;
}
return value;
}


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.

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.

Turtles and Random: Write a turtle program that generates a random number of sides for a polygon between 3 and 8 (from triangle to octagon). If the number of sides is even, draw two regular polygons with that many sides, side by side, colored blue. If the number of sides is odd, draw two regular polygons with that many sides, side by side, one colored blue and the other red. Extra Credit: employ functions in your program.

Dictionaries: Write a program that accepts a string as an input and creates a dictionary that counts the number of times each letter is present in the string and is NOT case sensitive (i.e. ‘Aa’ would count as 2 a’s). The keys should be the letters and the values should be the number of times that letter is in the string. For example an input of ‘aabbcAa’ would return {‘a’:4,’b’:2,’c’:1}. The alphabet string is already provided for you. (Hint: consider converting input string to lower case.)

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.

Turtles and Random: Write a turtle program that generates a random number of sides for a polygon between 3 and 8 (from triangle to octagon). If the number of sides is even, draw two regular polygons with that many sides, side by side, colored blue. If the number of sides is odd, draw two regular polygons with that many sides, side by side, one colored blue and the other red. Extra Credit: employ functions in your program.

Dictionaries: Write a program that accepts a string as an input and creates a dictionary that counts the number of times each letter is present in the string and is NOT case sensitive (i.e. ‘Aa’ would count as 2 a’s). The keys should be the letters and the values should be the number of times that letter is in the string. For example an input of ‘aabbcAa’ would return {‘a’:4,’b’:2,’c’:1}. The alphabet string is already provided for you. (Hint: consider converting input string to lower case.)

        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: 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)