# 1.1
import math

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

# 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.3

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

# 1.4
print('{:b}'.format(65535))

"""
Explanation: Homework 3 Key
CHE 116: Numerical Methods and Statistics
1/31/2020

1. String Formatting
Answer in Python

[2 points] Print $\sin 0.5$ to 5 digits of precision
[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

[2 points] Create a variable with a value of pi using the math module. Print it with 3 digits of precision
[2 points] Print out the number 65,535 as binary using string formatting.
End of explanation
"""


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

"""
Explanation: 2. Representing Numbers
Answer in Markdown. You can use Python for supporting your answer.

[2 points] The total number of people in a group chat is limited 256. What could be the reason for this very specific number?
[3 points] What is the minimum number of bytes it will it take to encode a phone number with an area code?
[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?
[1 point] What is the mantissa for 1.0 in decimal? 
[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.
[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.
End of explanation
"""


# 3.1

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

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


#3.2
s = 'foobar'

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

#3.3
s = 'fizz*buzz'
if '*' in s:
    print('hello')

"""
Explanation: 3. Booleans
Answer in Python


[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 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. 


[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.
End of explanation
"""


#4.1

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

print(v[-2])

#4.2
print(v[-3:])

#4.3
print(v[len(v) // 2])

#4.4
print(v[::2])

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

"""
Explanation: 4. Lists
Answer in Python
Use this list to answer the questions: ['A', 'B', 'C', 'D', 'E', 'F', 'G']

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


