1
+ # - ProblemSet1.py *- coding: utf-8 -*-
2
+ """
3
+ Each problem will be a function to write.
4
+ Remember that you can execute just the code between the #%% signs by clicking
5
+ somewhere in that space and then using Ctrl-Enter (Cmd-Return on a Mac). An
6
+ alternative is to use the second toolbar green triangle or Menu>Run>Run cell.
7
+ On loops especially, you can make an error that causes the program to run
8
+ forever. If you don't get immediate response, then this is probably happening.
9
+ In that case, try Ctrl-C. If that doesn't stop it, click your IPython console
10
+ away and open a new one from the Consoles menu. Look over your code and see
11
+ why the termination condition can't be met and fix it. Then run again.
12
+ You can submit your work in two ways. One is simply to upload this file. This
13
+ is the easiest way to go provided that you haven't corrupted it. To make sure
14
+ that it isn't corrupted, run the whole file by clicking the left green triangle
15
+ above. If it is corrupted, you can fix it or submit just your function as
16
+ described next.
17
+ To submit only a single function, copy the material between the two #%% into
18
+ a text file using this Spyder editor (Choose menu File>New File... to get a
19
+ new file and then copy your function from between the #%%'s. Don't use a word
20
+ processor, but you can use a text editor such as Notepad in Windows or TextEdit
21
+ on the Mac. Save your new file as problemx.py (where x is the problem number).
22
+ Upload this file to Coursera. Note that the grading program is going to run
23
+ the function with the name specified, so don't change the function name (it
24
+ doesn't matter what you name the file itself).
25
+ Note: each of the functions below is made runnable by adding the statement
26
+ pass. This is a do-nothing statement. You should replace it with your code,
27
+ but its present doesn't affect how your code runs.
28
+ """
29
+ """
30
+ IMPORTANT TIPS:
31
+ 1) When you upload your problem(s) you click on SUBMIT at the bottom of the
32
+ page. That will start the auto-grader. It should return with your grade
33
+ within a few minutes.
34
+
35
+ 2) You can resubmit any problem as often as you would like and find out how
36
+ you did in minutes. This is the advantage of the autograder over peer grading
37
+ used in many courses. With peer grading you'd have to grade several other
38
+ students' problems at the end of the week, you wouldn't find out how you did
39
+ for at least a week, and you couldn't resubmit after seeing your result.
40
+ 3) The downside of the auto-grader is that it is very strict and literal. You
41
+ should format your output EXACTLY as shown in the example runs.
42
+ 4) Common mistakes: including an extra space in the printout, mis-spelling a
43
+ word, changing the punctuation that the example uses, and using end=" " when
44
+ not needed.
45
+ 5) The example run uses example data. The auto-grader will always use
46
+ different data, so don't write functions that are specific to the example data.
47
+ In particular, don't use the variable name of the example data within your
48
+ function --- it will likely be completely undefined and unknown to the grader.
49
+ 6) These are test questions that can be resubmitted. So the auto-grader gives
50
+ fairly generic error messages -- it does not tell you how to fix your function.
51
+ It will say that your code produced too few lines or too many lines; it will
52
+ say whether your code failed x number of test cases and give you a fractional
53
+ score out of 10 rounded to the nearest integer. For example, if the function
54
+ should output 3 lines and you got 2 of the 3 right, it would say failed one
55
+ case and give you a score of 7 (2/3 of 10 rounded)). Sometimes, we run your
56
+ function several times on different data. In this case it will tell you the
57
+ number of cases (i.e., runs) that failed and give you a fractional score. It
58
+ may tell you that your function failed to run. Often if your program crashes,
59
+ there will be lots of error messages produced and the grader will tell you it
60
+ produced too many lines.
61
+ 7) Run your program on the example data and make sure it matches the example
62
+ output. If you program doesn't get full credit when graded, check the format
63
+ of the print statements first.
64
+ 8) All of the exercises in this course can be solved using the material in the
65
+ course lectures. There is no need to look to outside sources of more material.
66
+ In fact, a sophisticated outside technique can sometimes have side-effects that
67
+ you aren't aware of that causes your function to fail the grader.
68
+ """
69
+
70
+ """
71
+ Problem 1_1:
72
+ Write a function problem1_1() that prints "Problem Set 1".
73
+ Tip: Be careful that your program outputs this exact phrase with no additional
74
+ characters. It will be graded automatically and must be precise. Here is the
75
+ run of my solved problem 1_1:
76
+ problem1_1()
77
+ Problem Set 1
78
+ Note the problem1_1() is what I typed to run the problem and "Problem Set 1" is
79
+ what it printed out. There will typically be a sample run such as this either
80
+ before or after the statement of each problem. This helps clarify what you
81
+ are expected to do and shows how the auto-grader expects it to look.
82
+ """
83
+ #%%
84
+ def problem1_1 ():
85
+ print ("Problem Set 1" )
86
+
87
+
88
+ #%%
89
+
90
+ """
91
+ Problem 1_2:
92
+ Write a function problem1_2(x,y) that prints the sum and product of the
93
+ numbers x and y on separate lines, the sum printing first.
94
+ """
95
+ #%%
96
+ def problem1_2 (x ,y ):
97
+ print (x + y )
98
+ print (x * y )
99
+
100
+ #%%
101
+ """
102
+ Test run. Note that the grader program will use different numbers:
103
+ problem1_2(3,5)
104
+ 8
105
+ 15
106
+ """
107
+ """
108
+ Problem 1_3:
109
+ Write a function problem1_3(n) that adds up the numbers 1 through n and
110
+ prints out the result. You should use either a 'while' loop or a 'for' loop.
111
+ Be sure that you check your answer on several numbers n. Be careful that your
112
+ loop steps through all the numbers from 1 through and including n.
113
+ Tip: As this involves a loop you could make an error that causes it to run
114
+ forever. Usually Control-C will stop it. See suggestions at the beginning of
115
+ this document. With loops take care that your first and last iterations are
116
+ what you expect. A print statement can be inserted in the loop to monitor it,
117
+ but be sure this isn't in the submitted function.
118
+ """
119
+ #%%
120
+ def problem1_3 (n ):
121
+ my_sum = 0
122
+ while my_sum <= n :
123
+ my_sum = sum (range (n + 1 ))
124
+ print (my_sum )
125
+
126
+
127
+ #%%
128
+ """
129
+ Test run. Note that the grader program will use a different number for n:
130
+ problem1_3(6)
131
+ 21
132
+ """
133
+ """
134
+ Problem 1_4:
135
+ Write a function 'problem1_4(miles)' to convert miles to feet. There are
136
+ 5280 feet in each mile. Make the print out a statement as follows:
137
+ "There are 10560 feet in 2 miles." Except for the numbers this statement
138
+ should be exactly as written.
139
+ Tip: Watch the spacing before and after your numbers. Make sure that it is
140
+ just one space or the auto-grader may not give you credit.
141
+ """
142
+ #%%
143
+ def problem1_4 (miles ):
144
+ fixed = 5280
145
+ feet = miles * fixed
146
+ print (f'There are { feet } feet in { miles } miles.' )
147
+
148
+
149
+ #%%
150
+ """
151
+ Test run. Note that the grader program will use different numbers:
152
+ problem1_4(5)
153
+ There are 26400 feet in 5 miles.
154
+ """
155
+ """
156
+ Problem 1_5:
157
+ Write a function 'problem1_5(age)'. This function should use if-elif-else
158
+ statement to print out "Have a glass of milk." for anyone under 7; "Have
159
+ a coke." for anyone under 21, and "Have a martini." for anyone 21 or older.
160
+ Tip: Be careful about the ages 7 (a seven year old is not under 7) and 21.
161
+ Also be careful to make the phrases exactly as shown for the auto-grader.
162
+ """
163
+ #%%
164
+ def problem1_5 (age ):
165
+ msg = ""
166
+ if age < 7 :
167
+ msg = "Have a glass of milk."
168
+ elif age < 21 :
169
+ msg = "Have a coke."
170
+ elif age >= 21 :
171
+ msg = "Have a martini"
172
+ print (msg )
173
+
174
+
175
+
176
+
177
+
178
+ #%%
179
+ """
180
+ Test runs (3 of them). Note that the grader program will use different numbers:
181
+ problem1_5(5)
182
+ Have a glass of milk.
183
+ problem1_5(10)
184
+ Have a coke.
185
+ problem1_5(25)
186
+ Have a martini.
187
+ """
188
+ """
189
+ Problem 1_6:
190
+ Write a function 'problem1_6()' that prints the odd numbers from 1 through 100.
191
+ Make all of these numbers appear on the same line (actually, when the line
192
+ fills up it will wrap around, but ignore that.). In order to do this, your
193
+ print statement should have end=" " in it. For example, print(name,end=" ")
194
+ will keep the next print statement from starting a new line. Be sure there is a
195
+ space between these quotes or your numbers will run together. Use a single
196
+ space as that is what the grading program expects. Use a 'for' loop
197
+ and a range() function.
198
+ Things to be careful of that might go wrong: You print too many numbers, you
199
+ put too much or too little space between them, you print each number on its
200
+ own line, you print even numbers or all numbers, your first number isn't 1 or
201
+ your last number isn't 99. Always check first and last outputs when you write
202
+ a loop.
203
+ """
204
+ #%%
205
+ def problem1_6 ():
206
+ for n in range (101 ):
207
+ if n % 2 == 1 :
208
+ print (n , end = ' ' )
209
+
210
+
211
+
212
+
213
+ #%%
214
+ """
215
+ Test run (I've inserted a newline here to cause wrapping in the editor):
216
+ problem1_6()
217
+ 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55
218
+ 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
219
+ """
220
+ """
221
+ Problem 1_7:
222
+ Write a function problem1_7() that computes the area of a trapezoid. Here is the
223
+ formula: A = (1/2)(b1+b2)h. In the formula b1 is the length of one of the
224
+ bases, b2 the other. The height is h and the area is A. Basically, this
225
+ takes the average of the two bases times the height. For a rectangle b1 = b2,
226
+ so this reduces to b1*h. This means that you can do a pretty good test of the
227
+ correctness of your function using a rectangle (that way you can compute the
228
+ answer in your head). Use input statements to ask for the bases and the height.
229
+ Convert these input strings to real numbers using float(). Print the output
230
+ nicely EXACTLY like mine below.
231
+ Tip: Be careful that your output on the test case below is exactly as shown
232
+ so that the auto-grader judges your output correctly. The auto-grader does
233
+ not look at your input statements, so you don't have to use my input prompts
234
+ if you don't want to. However, the auto-grader will enter the three inputs in
235
+ the order shown. See the other test run below.
236
+ problem1_7()
237
+ Enter the length of one of the bases: 3
238
+ Enter the length of the other base: 4
239
+ Enter the height: 8
240
+ The area of a trapezoid with bases 3.0 and 4.0 and height 8.0 is 28.0
241
+ """
242
+ #%%
243
+ def problem1_7 ():
244
+ b1 = float (input ("Enter the length of one of the bases" ))
245
+ b2 = float (input ("Enter the length of the other base" ))
246
+ h = float (input ("Enter the height" ))
247
+ print ("The area of a trapezoid with bases" ,b1 ,"and" ,b2 ,"and height" ,h ,"is" ,(b1 + b2 )* h / 2 )
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+ #%%
256
+ """
257
+ Another test run. In grading, expect different input numbers to be used.
258
+ problem1_7()
259
+ Enter the length of one of the bases: 10
260
+ Enter the length of the other base: 11
261
+ Enter the height: 12
262
+ The area of a trapezoid with bases 10.0 and 11.0 and height 12.0 is 126.0
263
+ """
0 commit comments