-
Notifications
You must be signed in to change notification settings - Fork 63
/
hw02_solution_py2.py
67 lines (47 loc) · 1.32 KB
/
hw02_solution_py2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import math
# ========== HW02 SOLUTION [Python2] ========== #
# ========== 1 ========== #
# You are given the first three numbers of the Fibonacci sequence as F = [0, 1, 1].
# Create a for loop to determine the next 20 numbers of the Fibonacci sequence.
# Print F with the final 23 numbers.
F = [0, 1, 1]
for n in range(3, 23):
f_n = F[n - 1] + F[n - 2]
F.append(f_n)
print F
# ========== 2 ========== #
# Given the list x = [2.0,3.0,5.0,7.0,9.0],
# create a list Y(x) for each float in x.
# Print the list Y .
x = [2.0, 3.0, 5.0, 7.0, 9.0]
Y = []
for v in x:
new_val = (3.0 * v) ** 2 / (99 * v - v ** 3) - 1 / v
Y.append(new_val)
# one-liner with list-comprehension:
Y = [(3.0 * v) ** 2 / (99 * v - v ** 3) - 1 / v
for v in x]
print Y
# ========== 3 ========== #
def quadratic(a, b, c):
x0 = (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
x1 = (-b - (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
return x0, x1
print quadratic(3.3, 1.7, -9.4)
# ========== 4 ========== #
ans = 0
while ans ** 2 < 2000:
ans += 1
ans -= 1
print ans
# ========== 5 ========== #
def volume(r):
return 4.0 / 3 * math.pi * r ** 3
def surface_area(r):
return 4.0 * math.pi * r ** 2
def density(r, m=0.35):
return m / volume(r)
print volume(0.69)
print surface_area(0.4)
print density(0.3)
print density(0.25, m=2.0)