-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem set3.txt
227 lines (193 loc) · 7.47 KB
/
problem set3.txt
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
# - ProblemSet3.py *- coding: utf-8 -*-
"""
Problem 3_1:
Write a function that reads a text file and counts the number of characters in
it. Print both the text file and the number of characters. Do this so that the
printout isn't doubled space (use an end="" argument in the print statement).
Also, skip a line before printing the count. Note that it is easy to get the
number of characters in each line using the len() function.
Here is my run for HumptyDumpty.txt. Let me point out one thing that is not
visible here and is a bit technical. At the end of each of the first three
lines there is a <newline> character. These are invisible. If you do the count
by eye, you are likely to come up short by these three characters, but they
are visible to len() and you should count them -- they are part of the 141
"letters" in the humptydumpty.txt file. Counting them makes this an easier
function for you to write.
Your output should look just like this for the autograder:
problem3_1("humptydumpty.txt")
Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again.
There are 141 letters in the file.
"""
#%%
def problem3_1(txtfilename):
pass # replace this pass (a do-nothing) statement with your code
#%%
"""
Problem 3_2:
Below you see three objects which are of collection data type: a list, a tuple,
and a string. Although different in many ways, you can write a 'for' loop that
steps through a "collection" and it will work with all three. This problem
is started for you. Finish it by writing the loop that will step through the
collection and print out its items, one per line. Test it and make sure that
it works for all three of the following data objects.
Be sure that your code does not include the name of any one of these data
collections. That would stop it from being general enough to deal with a
different collection. The grader will use different data.
There is a printout of my run after the problem starter.
"""
#%%
nlis = [23,64,23,46,12,24] # list
atup = ("c","e","a","d","b") # tuple
str1 = "Rumplestilskin" # string
#%%
def problem3_2(collection):
pass # replace this pass (a do-nothing) statement with your code
#%%
"""
My runs
problem3_2(nlis)
23
64
23
46
12
24
problem3_2(atup)
c
e
a
d
b
problem3_2(str1)
R
u
m
p
l
e
s
t
i
l
s
k
i
n
"""
"""
Problem 3_3:
Write a function that will convert a date from one format to another.
Specifically, 06/10/2016 should convert to June 17, 2016. Actually, you
will input the 6, the 17, and the 2016 as separate integers (numbers) and
the function will assemble and print the date as June 17, 2016. I suggest
that you create a tuple months = ("January", "February", "March", ...) to
store the names of the months. Then it is easy to access the name February
as months[1] and so on.
Here is printout of my run.
problem3_3(6,17,2016)
June 17, 2016
*** Note: for simplicity use 6 and not 06 for numbers; otherwise, the
function will confuse Python and will have to be more complex to work. ***
*** Tip: In print statements, commas create a space. So you may have difficulty
avoiding a space between the 17 above and the following comma. I suggest
that you build the output as a single string containing the properly formatted
date and then print that. You can convert any number to string by using str()
and tie the parts together using +. Duplicate the format of the example output
exactly. Everything you need to do this is covered in the lectures. ***
"""
#%%
def problem3_3(month, day, year):
""" Takes date of form mm/dd/yyyy and writes it in form June 17, 2016
Example3_3: problem3_3(6, 17, 2016) gives June 17, 2016 """
pass # replace this pass (a do-nothing) statement with your code
#%%
"""
Problem 3_4:
Write a function that is complementary to the one in the previous problem that
will convert a date such as June 17, 2016 into the format 6/17/2016. I
suggest that you use a dictionary to convert from the name of the month to the
number of the month. For example months = {"January":1, "February":2, ...}.
Then it is easy to look up the month number as months["February"] and so on.
Note that the grader will assume that month names begin with capital letters.
*** Tip: In print statements, commas create a space. So you may have difficulty
avoiding a space between the 7, 17, and 2016 below and the following comma. I
suggest that you build the output as a single string containing the properly
formatted date and then print that. You can convert any number to string by
using str() and tie the parts together using +. Duplicate the format of the
example output exactly. Everything you need to do this is covered in the
lectures. ***
Here is a printout of my run for June 17, 2016.
problem3_4("July",17, 2016)
7/17/2016
"""
#%%
def problem3_4(mon, day, year):
""" Takes date such as July 17, 2016 and write it as 7/17/2016 """
pass # replace this pass (a do-nothing) statement with your code
#%%
"""
Problem 3_5:
Write a function that will look up a phone number given a name. Use this
dictionary of phone numbers in your program, so that the grader will know
what phone numbers are available. In it's simplest form, the program will
crash if a name that isn't in its dictionary is asked for.
Here is a printout of one of my runs.
problem3_5("james")
(212) 567-8149
Below is the start of the program including the dictionary.
"""
#%%
def problem3_5(name):
""" Looks up the phone number of the person whose name is name """
phone_numbers = {"abbie":"(860) 123-4535", "beverly":"(901) 454-3241", \
"james": "(212) 567-8149", "thomas": "(795) 342-9145"}
pass # replace this pass (a do-nothing) statement with your code
#%%
"""
Problem 3_6:
Write a program (not just a function, but a stand alone program or script) that
reads through a file and writes another file that gives the length of each line
in the first file. If line is the line that you've read into your program, use
line.strip("\n") to strip away the invisible newline character at the end of
each line. Otherwise, your count will be one higher than the autograder's.
Note that since this is a program running from the Command Window (or terminal
or cmd.exe), it won't be runnable as our usual functions are by entering
Shift-Enter. You should use the File menu in Spyder to create you own file.
But, if you prefer, there is a starter file called problem3_6starter.py.
Here is a run of my solution program using the HumptyDumpty.txt file. The run
is followed by a printout of HumptyDumpty.txt and the written file
HumptyLength.txt. Note that your program does not print anything out. It does
write a text file though. To see these files we have to use type on a PC ( but
it would be cat for Mac or Linux).
C:>python problem3_6.py humptydumpty.txt humptylength.txt
C:>type humptydumpty.txt
Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again.
C:>type humptylength.txt
28
31
44
35
Problem 3_7:
Write a function that would read a CSV file that looks like this, flowers.csv:
petunia,5.95
alyssum,3.95
begonia,5.95
sunflower,5.95
coelius,4.95
and look up the price of a flower and print out that price. Remember to import
the necessary library.
Here is my run on the above CSV file:
problem3_7("flowers.csv","alyssum")
3.95
Solution starter:
"""
#%%
def problem3_7(csv_pricefile, flower):
pass # replace this pass (a do-nothing) statement with your code
#%%