forked from paulruvolo/SoftwareDesign
/
jmorris.homework2.py
195 lines (139 loc) · 2.82 KB
/
jmorris.homework2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
SoftwareDesign
==============
The base repository for Olin College's Software Design Spring 2014\
SoftDes
Julian Morris
Homework 2
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 29 09:38:52 2014
@author: julian
"""
"""HW 2 Exercise 2.4"""
"""part 1"""
"""The volume of a sphere with radius r is 4/3 π r3.
What is the volume of a sphere with radius 5?
"""
import math
r=5.0
v=4.0/3.0*math.pi*r**3.0
print v
"""part 2"""
"""Suppose the cover price of a book is $24.95,
but bookstores get a 40% discount.
Shipping costs $3 for the first copy and 75 cents for each additional copy.
What is the total wholesale cost for 60 copies?"""
c=24.95*0.6
e=c+3+59*(c+0.75)
print "$"+str(e)
"""part 3"""
"""If I leave my house at 6:52 am and
run 1 mile at an easy pace (8:15 per mile),
then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again,
what time do I get home for breakfast?"""
"""
using unum
"""
from unum.units import *
runtime=(8*min+15*s)+3*(7*min+12*s)+(8*min+15*s)
minutes=(52*min)+runtime
total=((6*h)+minutes)
print total
"""
I couldn't figure out how to make the times in a neater format, so I decided not to use unum
"""
"""
without using unum
"""
minutes=((8*2+7*3)+(52)+(15.0/60*2)+(12.0/60*3))
finish_minutes=minutes%60
finish_hour=6+int(minutes/60)
print '%d:%dam' %(finish_hour, finish_minutes)
"""
Exercise 3.5 part 1
"""
def twice(f):
f()
f()
def four_times(f):
f()
f()
f()
f()
def pm():
print '+ - - - -' ,
def plusminus():
twice(pm)
print '+'
def v():
print '| ',
def vertical():
twice(v)
print '|'
def sets():
plusminus()
four_times(vertical)
def result():
twice(sets)
plusminus()
result()
"""
Exercise 3.5 part 2
"""
def twice2(f):
f()
f()
def four_times2(f):
f()
f()
f()
f()
def pm2():
print '+ - - - -' ,
def plusminus2():
four_times2(pm2)
print '+'
def v2():
print '| ',
def vertical2():
four_times2(v2)
print '|'
def section2():
plusminus2()
four_times2(vertical2)
def result2():
four_times2(section2)
plusminus2()
result2()
"""
Exercise 5.3 part 1
"""
def check_fermat(a,b,c,n):
if n>2:
if a**n+b**n==c**n:
print "Holy smokes, Fermat was wrong!"
else:
print "No, that doesn't work."
check_fermat(2,3,4,5)
"""
Exercise 5.3 part 2
"""
def check_input():
a = int(raw_input("a: "))
b = int(raw_input("b: "))
c = int(raw_input("c: "))
n = int(raw_input("n: "))
print check_fermat(a, b, c, n)
check_input()
"""
Exercise 6.1. Write a compare function that returns 1 if x>y,
0 if x==y, and -1 if x<y.
"""
def compare(x,y):
if x>y:
return 1
elif x==y:
return 0
elif x<y:
return -1
print compare(2,2)