159.171 – Computational Thinking and Software Development
Assignment 2B – Semester 1, 2018
Question 1 - Currency Conversion Table – 5 marks
Using for loops, create a program to show how much a certain number of NZ$ will buy of four other
currencies.
Say the currencies and their conversion to $NZ are
Currency Symbol Conversion Rate (amount NZ$1 will buy)
Australian Dollar AUD 0.96
US Dollar USD 0.75
Euro Euro 0.67
Great British Pound GBP 0.496
Sample Output
NZ$ 10.00 AUD 9.60 USD 7.50 EURO 6.70 GBP 4.96
NZ$ 20.00 AUD 19.20 USD 15.00 EURO 13.40 GBP 9.92
NZ$ 30.00 AUD 28.80 USD 22.50 EURO 20.10 GBP 14.88
NZ$ 40.00 AUD 38.40 USD 30.00 EURO 26.80 GBP 19.84
NZ$ 50.00 AUD 48.00 USD 37.50 EURO 33.50 GBP 24.80
NZ$ 60.00 AUD 57.60 USD 45.00 EURO 40.20 GBP 29.76
NZ$ 70.00 AUD 67.20 USD 52.50 EURO 46.90 GBP 34.72
NZ$ 80.00 AUD 76.80 USD 60.00 EURO 53.60 GBP 39.68
NZ$ 90.00 AUD 86.40 USD 67.50 EURO 60.30 GBP 44.64
NZ$ 100.00 AUD 96.00 USD 75.00 EURO 67.00 GBP 49.60
So NZ$100 will buy US$75 or Euro 67.00
Question 2 – Text file statistics – 5 Marks
Your task is to read a short text file, clean it up by removing punctuation (e.g. commas, full-stops ...), and
then find and display following statistics:
1. the number of lines in the file
2. the total number of words in the file.
3. the average number of characters per word.
4. the number of times the letter ‘e’ occurs in the file.
When running your program, it should load and display the file, then display the statistics.
Suggestions:
• create a small text file (use PyCharm’s File not Python File) of about 5-7 lines and work out the
statistics manually, and make sure your program gives the same results.
• The string.replace() method is useful for getting rid of punctuation. Just replace each of the
following characters : , . ! ? with a space.
159.171 – Assignment 2B 1 © Giovanni Moretti 2018
Question 3 - A Tiny Editor – 5 marks
This task is to write a very basic text editor. It allows you to add, delete and modify lines of text.
Use a list of strings to store the lines, with each list element being one line. The elements of the list are
modified according the commands given by the user.
The editor repeatedly
1. displays the lines entered so far prefixed with a line number in front of each.
2. it waits for the user to enter a command
3. if it's a valid command, it modifies the list appropriately
4. go back to step #1
Initially, the list of lines will be empty.
The valid commands are:
a - add
lines from the user are added to the lines collected so far.
All lines entered are added to the list until the user enters a line containing a single #.
d - delete a line
prompt for a line number and then delete that line.
Use the Python command del to delete a line from the list of lines
lines = ["A", "hearty", "breakfast"]
del lines[1] # delete the 2nd item ("hearty")
r - replace a line
prompt for a line number and then replace that line with a new one from the user
lines = ["A", "hearty", "breakfast"]
lines[1] = "big" # will replace "hearty" with "big"
f - find and replace a string
prompt for one string (the search pattern) and then a second (the replacement).
Replace all occurrences of the first string with the second, using string.replace() .
c - Clear
delete all lines, so the state is like when the program starts.
s - Save
prompt for a filename and save the text to this file inserting "\n" where appropriate.
OPTIONAL: while not required, having a help command (e.g. '?' or 'h') that displays something like the
following would be a nice touch
*** TinyEd commands ***
a Add - Start adding lines. A single # on a line by itself exits add.
d Delete a numbered line
r replace a line
f Find replace a string
c Clear
s Save
q Quit
159.171 – Assignment 2B 2 © Giovanni Moretti 2018
Here's an example of this editor in use
Several commands are shown. Initially, before the "a" (add) command is used, there aren't any lines, so
nothing is output before the command prompt.
$ python tinyEd.py # or run it inside PyCharm
==============================
Command a, d, r, f, c, s ?: a
> It's a sunny day
> and I've just had
> lunc
> #
==============================
1 ) It's a sunny day
2 ) and I've just had
3 ) lunc
Command a, d, r, f, c, s ?: r
Replace line no: 3
Replacement : lunch.
==============================
1 ) It's a sunny day
2 ) and I've just had
3 ) lunch.
Command a, d, r, f, c, s ?: f
Find string : day
Replace with: afternoon
==============================
1 ) It's a sunny afternoon
2 ) and I've just had
3 ) lunch.
Command a, d, r, f, c, s ?: r
Replace line no: 3
Replacement line: a good lunch.
==============================
1 ) It's a sunny afternoon
2 ) and I've just had
3 ) a good lunch.
Command a, d, r, f, c, s ?: f
Find string : u
Replace with: UUU
==============================
1 ) It's a sUUUnny afternoon
2 ) and I've jUUUst had
3 ) a good lUUUnch.
Command a, d, r, f, c, s ?: f
Find string : UUU
Replace with: u
==============================
1 ) It's a sunny afternoon
2 ) and I've just had
3 ) a good lunch.
Command a, d, r, f, c, s ?: q
What and how to submit
How: ALL submissions must be via Stream (not email) using the Assignment 2B submission link.
What: Submit your Python programs, each named with .py extensions.
You should have two .py files to submit, one for each question.
Do not submit Word documents (.doc or .docx) or .zip files.
Check:
that your files have a .py extension
that all programs display your name and ID number when starting.
159.171 – Assignment 2B 3 © Giovanni Moretti 2018