Answer in Python
1.30
21.83
0.21
math
module. Print it with 3 digits of precision
In [1]:
# 1.1
import math
print('{:.5}'.format(math.sin(0.5)))
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))
In [34]:
# 1.3
v = math.pi
print('{:.3}'.format(v))
In [36]:
# 1.4
print('{:b}'.format(65535))
Answer in Markdown. You can use Python for supporting your answer.
This is $2^8$, so probably 1 byte is used for this
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.
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
1
0.9 cannot be represented exactly in binary
0.875 can be represented exactly.
In [13]:
# code for 2.5, 2.6
print('{:.50f}'.format(0.9))
print('{:.50f}'.format(0.875))
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
.
In [14]:
# 3.1
v = -1
if v < 0:
print('negative')
else:
print('positive')
v = 1
if v < 0:
print('negative')
else:
print('positive')
In [41]:
#3.2
s = 'foobar'
if len(s) > 5:
print('foo')
else:
print('bar')
In [16]:
#3.3
s = 'fizz*buzz'
if '*' in s:
print('hello')
Answer in Python
Use this list to answer the questions: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
len
function
In [17]:
#4.1
v = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
print(v[-2])
In [18]:
#4.2
print(v[-3:])
In [19]:
#4.3
print(v[len(v) // 2])
In [20]:
#4.4
print(v[::2])
In [21]:
#4.5
print(v[1::2])