-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions
233 lines (196 loc) · 6.92 KB
/
Functions
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def clip(lo, x, hi):
'''
Takes in three numbers and returns a value based on the value of x.
Returns:
- lo, when x < lo
- hi, when x > hi
- x, otherwise
'''
# Your code here
return min(hi,max(lo,x))
def evalQuadratic(a, b, c, x):
'''
a, b, c: numerical values for the coefficients of a quadratic equation
x: numerical value at which to evaluate the quadratic.
'''
# Your code here
return a * x**2 + b * x + c
def square(x):
'''
x: int or float.
'''
# Your code here
return x**2
def odd(x):
'''
x: int or float.
returns: True if x is odd, False otherwise
'''
# Your code here
return (x%2)==1
def isVowel(char):
'''
char: a single letter of any case
returns: True if char is a vowel and False otherwise.
'''
# Your code here
return char in "aeiouAEIOU"
def isVowel(char):
'''
char: a single letter of any case
returns: True if char is a vowel and False otherwise.
'''
# Your code here
return "aeiouAEIOU".find(char) >=0
def credit_card(balance, annualInterestRate, monthlyPaymentRate) :
"""
balance : float, outstanding balance on the credit card
annualInterestRate : annual interest rate as a decimal
monthlyPaymentRate : minimum monthly payment rate as a decimal
"""
MONTHS = 12
monthlyInterestRate = annualInterestRate / 12.0
totalPaid = 0
for month in range(MONTHS):
monthlyPayment = round(balance * monthlyPaymentRate,2)
totalPaid += monthlyPayment
unpaidBalance = balance - monthlyPayment
balance = unpaidBalance * (1 + monthlyInterestRate)
print "Month:", month+1
print "Minimum monthly payment:", monthlyPayment
print "Remaining balance:", round(balance, 2)
print "Total paid:", totalPaid
print "Remaining balance:", round(balance, 2)
def payment12_10(balance, annualInterestRate) :
"""
balance : float, outstanding balance on the credit card
annualInterestRate : float, annual interest rate as a decimal
Calculates the minimum fixed monthly payment needed in order pay off
a credit card balance within 12 months.
By a fixed monthly payment, we mean a single number which does not change
each month, but instead is a constant amount that will be paid each month.
The monthly payment must be a multiple of $10 and is the same for all months.
Notice that it is possible for the balance to become negative using
this payment scheme, which is okay.
"""
MONTHS = 12
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = ((balance / MONTHS) // 10) * 10
endBalance = balance
while endBalance > 0 :
#print "monthlyPayment :", monthlyPayment
for dummy_month in range(MONTHS):
endBalance = (endBalance - monthlyPayment) * (1 + monthlyInterestRate)
if endBalance > 0 :
monthlyPayment += 10
endBalance = balance
print "Lowest Payment:", monthlyPayment
def payment12(balance, annualInterestRate) :
"""
balance : float, outstanding balance on the credit card
annualInterestRate : float, annual interest rate as a decimal
Calculates the minimum fixed monthly payment needed in order pay off
a credit card balance within 12 months.
By a fixed monthly payment, we mean a single number which does not change
each month, but instead is a constant amount that will be paid each month.
The monthly payment WILL NOT BE LONGER a multiple of $10,
but is the same for all months.
Notice that it is possible for the balance to become negative using
this payment scheme, which is okay.
"""
MONTHS = 12
monthlyInterestRate = float(annualInterestRate) / MONTHS
minMonthlyPayment = (balance / MONTHS)
maxMonthlyPayment = (balance * (1 + monthlyInterestRate) ** MONTHS) / MONTHS
endBalance = balance
epsilon = 0.01
monthlyPayment = (maxMonthlyPayment+minMonthlyPayment)/2
while abs(endBalance) > epsilon :
#print "monthlyPayment :", monthlyPayment
for dummy_month in range(MONTHS):
endBalance = (endBalance - monthlyPayment) * (1 + monthlyInterestRate)
if abs(endBalance) > epsilon :
if endBalance > 0 :
minMonthlyPayment = monthlyPayment
else :
maxMonthlyPayment = monthlyPayment
monthlyPayment = (maxMonthlyPayment+minMonthlyPayment)/2
endBalance = balance
print "Lowest Payment:", round(monthlyPayment, 2)
#payment12(balance, annualInterestRate)
payment12(320000, 0.2)
payment12(999999, 0.18)
# Radiation Exposure
def f(x):
import math
return 10*math.e**(math.log(0.5)/5.27 * x)
def radiationExposure(start, stop, step):
count = 0
i = start
while i < stop:
count += f(i)*step
i += step
return count
###
# Hangman
def isWordGuessed(secretWord, lettersGuessed):
'''
secretWord: string, the word the user is guessing
lettersGuessed: list, what letters have been guessed so far
returns: boolean, True if all the letters of secretWord are in lettersGuessed;
False otherwise
'''
# FILL IN YOUR CODE HERE...
for letter in secretWord:
if not letter in lettersGuessed:
return False
return True
def getGuessedWord(secretWord, lettersGuessed):
'''
secretWord: string, the word the user is guessing
lettersGuessed: list, what letters have been guessed so far
returns: string, comprised of letters and underscores that represents
what letters in secretWord have been guessed so far.
'''
# FILL IN YOUR CODE HERE...
guessedWord = ''
for letter in secretWord:
if letter in lettersGuessed:
guessedWord += letter + ' '
else:
guessedWord += '_ '
return guessedWord
def getAvailableLetters(lettersGuessed):
'''
lettersGuessed: list, what letters have been guessed so far
returns: string, comprised of letters that represents what letters have not
yet been guessed.
'''
# FILL IN YOUR CODE HERE...
availableLetters = ''
for l in [l for l in string.ascii_lowercase if l not in lettersGuessed]:
availableLetters += l
return availableLetters
def sharp(x):
"""
(int) -> int
x# = ((((((2 ^ 3) ^ 4) ^ 5) ...) ...) x-1) ^ x)
"""
assert type(x)==int, "Argument should be an int"
assert x>=2, "Argument should be >=2"
if x == 2:
return 2
else:
return sharp(x-1) ** x
def ndigits(x):
"""
(int) -> int
This function should return the number of digits in the integer x.
"""
assert type(x)==int, "Argument should be an int"
if x<0: # Check for negative ints
x=-x
if x<10:
return 1
else:
return 1 + ndigits(x / 10)