1.12. Chapter Assessment

Check your understanding

    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: 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: 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
  • Drag to Answer B
  • Answer B
  • 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.

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)
        

Q-1: What’s your favorite part about Technovation?

    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
    What's your favorite programming language?
  • 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

    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
---
        
You have attempted of activities on this page