Homework 3 Key

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.

In [1]:
# 1.1
import math

print('{:.5}'.format(math.sin(0.5)))


0.47943

In [40]:
# 1.2
# clean way
print('{:5.2f}\n{:5.2f}\n{:5.2f}'.format(1.3, 21.83, 0.21))

# kind of hack
print('{:4.2f}\n{:5}\n{:5}'.format(1.3, 21.83, 0.21))


 1.30
21.83
 0.21
1.30
21.83
 0.21

In [34]:
# 1.3

v = math.pi
print('{:.3}'.format(v))


3.14

In [36]:
# 1.4
print('{:b}'.format(65535))


1111111111111111

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?

2.1

This is $2^8$, so probably 1 byte is used for this

2.2

Let's try 4 bytes, that is $2^{32} = 4,294,967,296$ which wouldn't include the Rochester area code. Instead $2^{40}$ (5 bytes) exceeds 10 digis. So we'll need 5 bytes.

2.3

Integer overflow because only 2 bytes are available (max 32,768 because $2^{15}$) are in use for health. The health point value rolls over to be negative

2.4

1

2.5

0.9 cannot be represented exactly in binary

2.6

0.875 can be represented exactly.


In [13]:
# code for 2.5, 2.6
print('{:.50f}'.format(0.9))
print('{:.50f}'.format(0.875))


0.90000000000000002220446049250313080847263336181641
0.87500000000000000000000000000000000000000000000000

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.


In [14]:
# 3.1

v = -1
if v < 0:
    print('negative')
else:
    print('positive')

v = 1
if v < 0:
    print('negative')
else:
    print('positive')


negative
positive

In [41]:
#3.2
s = 'foobar'

if len(s) > 5:
    print('foo')
else:
    print('bar')


foo

In [16]:
#3.3
s = 'fizz*buzz'
if '*' in s:
    print('hello')


hello

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.

In [17]:
#4.1

v = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

print(v[-2])


F

In [18]:
#4.2
print(v[-3:])


['E', 'F', 'G']

In [19]:
#4.3
print(v[len(v) // 2])


D

In [20]:
#4.4
print(v[::2])


['A', 'C', 'E', 'G']

In [21]:
#4.5
print(v[1::2])


['B', 'D', 'F']