Homework 3

CHE 116: Numerical Methods and Statistics

1/31/2020


1. String Formatting

Answer in Python

  1. [2 points] Print $\sin 0.5$ to 5 digits of precision
  2. [2 points] Print 1.30, 21.83, and 0.21 on separate lines and aligned on the decimal place using only one print statement like this:
     1.30
    21.83
     0.21
  1. [2 points] Create a variable with a value of pi using the math module. Print it with 3 digits of precision
  2. [2 points] Print out the number 65,535 as binary using string formatting.

2. Representing Numbers

Answer in Markdown. You can use Python for supporting your answer.

  1. [2 points] The total number of people in a group chat is limited 256. What could be the reason for this very specific number?
  2. [3 points] What is the minimum number of bytes it will it take to encode a phone number with an area code?
  3. [4 points] In a certain video game, a very powerful boss has 32,000 health points represented as a signed integer. You dicover that if instead of fighting the boss, you cast a spell that heals it for 800 points you immediately kill it. Why might this be?
  4. [1 point] What is the mantissa for 1.0 in decimal?
  5. [4 points] We want to write a computer program to detect if a student is at the boarder between an A- and B+ (90% score) so that we can decide which grade better represents their performance. Essentially the program should check if the grade is right at the 90% cutoff. Why is it impossible for a computer program to accomplish this seemingly simple task? Prove your hypothesis with python code.
  6. [1 point] We want to write a computer program to detect if a student is at the boarder between B and B+ (87.5%) score. Why is this a trivial task for a computer program?

3. Booleans

Answer in Python

  1. [2 points] Write an if statement that prints if a variable v is positive or negative. Demonstrate it by setting v to -1 and then 1.

  2. [2 points] Write an if statement that will print foo if a string has more than 5 characters and bar otherwise. Demonstrate with a variable whose value is foobar.

  3. [2 points] Write an if statement to check if the character * is in a string and prints hello if it is. Demonstrate with a varaible whose value is fizz*buzz.

4. Lists

Answer in Python

Use this list to answer the questions: ['A', 'B', 'C', 'D', 'E', 'F', 'G']

  1. [2 points] Print out the second to last element of the list using a negative index.
  2. [2 points] Print out the last three elements of the list using negative indices.
  3. [2 points] Print out the middle element of the list using the len function
  4. [2 points] Print every other element of the list.
  5. [2 points] Print every other element of the list starting from the second element.