Merge lp:~nickmeek/practical-programming/2014 into lp:practical-programming

Proposed by Nick Meek
Status: Needs review
Proposed branch: lp:~nickmeek/practical-programming/2014
Merge into: lp:practical-programming
Diff against target: 156 lines (+64/-9)
3 files modified
files_modules.tex (+48/-5)
iteration.tex (+15/-3)
strings.tex (+1/-1)
To merge this branch: bzr merge lp:~nickmeek/practical-programming/2014
Reviewer Review Type Date Requested Status
Brendan McCane Pending
Review via email: mp+199207@code.launchpad.net

Description of the change

Lectures 8 and 9

To post a comment you must log in.
102. By Nick Meek

Added a few easy exercises

103. By Nick Meek

Pull from repo

Unmerged revisions

103. By Nick Meek

Pull from repo

102. By Nick Meek

Added a few easy exercises

101. By Nick Meek

Pull from repo

100. By Nick Meek

Editing exercises again

99. By Nick Meek

Mainly changes to exercises. I inserted a few at the beginning to emphasis the read a file, process the contents, write out a new file process.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'files_modules.tex'
2--- files_modules.tex 2013-12-16 00:09:32 +0000
3+++ files_modules.tex 2013-12-17 02:10:38 +0000
4@@ -126,6 +126,7 @@
5 \end{pythonInteractive}
6
7 \section{Processing things from a file}
8+\label{section-processing-from-file}
9
10 You would normally read information from a file in a Python program
11 because you want to do something with it. Let's try counting the
12@@ -371,41 +372,83 @@
13 location of a file.
14 \item[text file:] A file that contains printable characters organized
15 into lines separated by newline characters.
16-\item[list:] A sequence of values separated by commas and surrounded by square braces. E.g. \pythoninline{[1, 2, 3]} or \pythoninline{['comma', 'separated', 'values']}.
17+\item[list:] A sequence of values separated by commas and surrounded
18+by square braces. E.g. \pythoninline{[1, 2, 3]} or \pythoninline{['comma', 'separated', 'values']}.
19 \end{description}
20
21 \section{Laboratory exercises}
22
23 \begin{enumerate}
24+
25 \item Write a program/script that will open a file (you can specify
26 the filename in your code) for reading and print its
27 contents to the screen.
28-\item Extend your program from the first question so that it asks the
29- user for a file to print, and then prints out the contents of that
30- file.
31+
32+\item Extend your program so that at the end of printing out the
33+contents of the file it also prints the number of words in the string.
34+For instance if the file contained
35+the string "Be as happy as possible" the output would look like:
36+
37+\begin{minipage}{\textwidth}
38+\begin{pythonInteractive}
39+Be as happy as possible
40+Words = 5
41+\end{pythonInteractive}
42+\end{minipage}
43+
44+\item Extend your program so that at the end of printing out the
45+contents of the file prints the number of words in the string
46+\emph{and} the average word length.
47+For instance if the file contained
48+the string "Be as happy as possible" the output would look like:
49+
50+\begin{minipage}{\textwidth}
51+\begin{pythonInteractive}
52+Be as happy as possible
53+Words = 5 Average Length = 3.8
54+\end{pythonInteractive}
55+\end{minipage}
56+\emph{Hint}: Once you have the words in a list you can iterate (step) through
57+the list using a \pythoninline{for} loop to access each word in turn and add its
58+length to a running total.
59+
60+\item Extend your program so that it creates a new string containing the
61+text from the file with the word count and average word length added to the end.
62+ It should then write the string to a new file. For instance, if the original input
63+ file was called \pythoninline{infile.txt} then the new file should be
64+ called \pythoninline{infile\_new.txt}.
65+
66+\item Extend your program further so that it asks the
67+ user for a file to print - rather than having it hard-coded in the program.
68+
69 \item Extend your program so that it first prints out all the files in
70 the current working directory before asking for a file to print.
71 \emph{Hint:} use the \pythoninline{os.listdir(".")} function to list
72 the files in the current directory (don't forget to
73 \pythoninline{import os} first). \label{ex-fm-listfiles}
74+
75 \item Put the code from Section \ref{sec-files-unifying} into a script and run
76 it to see that it works. Now extend the program so that it asks the
77 user for a web-page to open, then opens it and prints all the links
78 in that page. \label{ex-fm-web}
79+
80 \item \emph{Extension Exercise:} Extend your answer from Question
81 \ref{ex-fm-listfiles} so that if the requested file is a directory
82 it will list the contents of the directory instead of printing the
83 contents of a file. Make use of the function
84 \pythoninline{os.path.isdir} to tell if a given file is a directory.
85+
86 \item \emph{Extension Exercise:} Modify your program from Question
87 \ref{ex-fm-web} so that it lists all links in the web page, then
88 prints all the links in one of those web pages as well.
89+
90 \item \emph{Extension Exercise:} Adapt \myurl{film\_critic.py} from
91 Exercise \ref{sec-ff-critic} and instead of printing the program's
92 output to the screen, write it to a new file called
93 \myurl{new\_file.txt}.
94 Make sure the output still contains line breaks in the appropriate
95- places.
96+ places.
97+
98 \item \emph{Extension Exercise:} Write a program that reads in a file
99 and calculates the average word length. The code at the end of
100 Section \ref{sec-files-unifying} in the book is a good starting place.
101
102=== modified file 'iteration.tex'
103--- iteration.tex 2013-11-21 22:47:36 +0000
104+++ iteration.tex 2013-12-17 02:10:38 +0000
105@@ -51,7 +51,8 @@
106 x = x + 1
107 \end{python}
108 This means get the current value of \pythoninline{x}, add one, and
109-then update \pythoninline{x} with the new value.
110+then update \pythoninline{x} with the new value. We have seen this before
111+ in Section \ref{section-looping-and-counting}.
112 If you try to update a variable that doesn't exist, you get an error,
113 because Python evaluates the expression on the right side of the
114 assignment operator before it assigns the resulting value to the name
115@@ -127,8 +128,10 @@
116 computers do well and people do poorly.
117 Repeated execution of a set of statements is called \myidx{iteration}.
118 Because iteration is so common, Python provides several language
119-features to make it easier. The first feature we are going to look at
120-is the \pythoninline{while} statement.
121+features to make it easier. We have already seen this with the \pythoninline{for} loop.
122+The \pythoninline{for} loop is a specialist loop that hides some detail from us and can
123+only be used to iterate through compound data types, like strings and lists.
124+Now we will look at a more general form of the loop the \pythoninline{while} statement.
125
126 Here is a function called countdown that demonstrates the use of the
127 \pythoninline{while} statement:
128@@ -410,6 +413,15 @@
129
130 \begin{enumerate}
131
132+\item Recall the \pythoninline{pow} function. Write a function \pythoninline{my\_pow}
133+which takes two integer argument and using a \pythoninline{while} loop
134+raises the first to the power of the second.
135+
136+\item Recall the \% (mod) operator which calculates the remainder when one number is
137+divided by another; for instance 10 \% 3 = 1. Write a function \pythoninline{my\_mod}
138+which provides the same functionality but uses a \pythoninline{while} loop. For example
139+\pythoninline{my\_mod(10, 3)} would return 1.
140+
141 \item Recall \pythoninline{num\_digits} from page
142 \pageref{sec-counting-digits}. What will
143 \pythoninline{num\_digits(0)} return? Modify it to return
144
145=== modified file 'strings.tex'
146--- strings.tex 2013-12-16 00:56:20 +0000
147+++ strings.tex 2013-12-17 02:10:38 +0000
148@@ -311,7 +311,7 @@
149 \end{minipage}
150 Test this function to confirm that it does what we wanted it to do.
151
152-\section{Looping and counting}
153+\section{Looping and counting}\label{section-looping-and-counting}
154 The following program counts the number of times the letter
155 \pythoninline{a} appears
156 in a string and is an example of a counter pattern (more counter

Subscribers

People subscribed via source and target branches

to all changes: