Skip to document

10. File IO - This is the last assignment of the course

This is the last assignment of the course
Course

Computer science1015 (CSC1015F)

257 Documents
Students shared 257 documents in this course
Academic year: 2022/2023
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Cape Town

Comments

Please sign in or register to post comments.

Preview text

Version 28/04/2022 11:52:

CSC1015F Assignment 10

File Input and Output

Assignment Instructions

This assignment involves constructing Python programs that perform file input and output.

Furthermore , your solutions to this assignment will be evaluated for correctness and for the following qualities: - Documentation o Use of comments at the top of your code to identify program purpose, author and date. o Use of comments within your code to explain each non-obvious functional unit of code. - General style/readability o The use of meaningful names for variables and functions. - Algorithmic qualities o Efficiency, simplicity These criteria will be manually assessed by a tutor and commented upon. Up to 10 marks will be deducted for deficiencies.

Question 1 [35 marks]

Write a Python program called 8anagramsearch that searches a file for anagrams of a given

word, printing the results in alphabetical order.

Given two words, each is an anagram of the other if they contain the same letters in the same

quantities. For example, 'green' and 'genre'.

Here are 4 examples of intended program behaviour:

( NOTE: the input from the uses is shown in bold font .)

Sample I/O :

***** Anagram Finder ***** Enter a word: triangle ['alerting', 'altering', 'integral']

Sample I/O:

***** Anagram Finder ***** Enter a word: Orange ['onager']

Sample I/O:

***** Anagram Finder ***** Enter a word: back Sorry, anagrams of 'back' could not be found.

Sample I/O:

***** Anagram Finder ***** Sorry, could not find file 'EnglishWords'.

A lexicon (words file) has been provided on the Vula assignment page (8EnglishWords).

NOTE: This file begins with a copyright notice that should not be removed. Your program must skip

this notice by reading lines until it encounters one consisting of the word "START".

NOTE: The last example of program behaviour shows what the program should do if it does not find

the words file.

HINT: To determine if one word is an anagram of another, construct a dictionary of letter

frequencies for each word and compare e. given 8act9, 8cat9, and 8tact9:

>>> w1 = { 'a':1, 'c':1, 't':1} >>> w2 = {'c':1, 'a':1, 't':1} >>> w3 = { 'a':1, 'r':1, 't':2} >>> w1==w True >>> w1==w False

Question 2 [35 marks]

Write a program called 8tracer that may be used to assist with debugging Python programs.

  • Given the name of a Python program as input, 8 tracer will insert a trace statement at the beginning of each function definition.
  • Given the name of a program that already contains trace statements, 8tracer will remove them.

Say, for example, we have the following program, 8rfunction:

reverse a string, Hussein Suleman, 18 march 2015.

def reverse_string (sentence): new_sent = "" for i in range (len (sentence)-1,-1,-1): new_sent = new_sent + sentence[i] return new_sent

def main (): sent = input ("Enter a sentence: ") print (reverse_string (sent)) print (reverse_string (sent+sent))

main()

To complete the example, here is the expected user interaction for each execution of 8 trace:

***** Program Trace Utility ***** Enter the name of the program file: rfunction Inserting..

***** Program Trace Utility ***** Enter the name of the program file: rfunction Program contains trace statements Removing..

Question 3 [30 marks]

Write a program called 8anagramsets that asks the user to enter a word length and a filename. The program will search the 8EnglishWords file (of question 1) for sets of words

that are that length and are anagrams of each other, and will write the results to a file with the given

filename.

( NOTE: the input from the uses is shown in bold font .)

Sample User I/O:

***** Anagram Set Search ***** Enter word length: 12 Searching... Enter file name: twelve Writing results...

Expected output to 8twelve:

['abolitionism', 'mobilisation'] ['accouterment', 'accoutrement'] ['alterability', 'bilaterality'] ['amphitheater', 'amphitheatre'] ['behaviourism', 'misbehaviour'] ['commissioned', 'decommission'] ['conservation', 'conversation'] ['discreetness', 'discreteness'] ['impressively', 'permissively'] ['inactivation', 'vaticination'] ['microcephaly', 'pyrochemical'] ['paradisaical', 'paradisiacal'] ['restrengthen', 'strengthener'] ['unimpressive', 'unpermissive']

NOTE: Results are presented in alphabetical order.

Submission

Create and submit a Zip file called 8 ABCXYZ123 9 (where ABCXYZ123 is YOUR student number)

containing anagramsearch, tracer, and anagramsets.

Was this document helpful?

10. File IO - This is the last assignment of the course

Course: Computer science1015 (CSC1015F)

257 Documents
Students shared 257 documents in this course
Was this document helpful?
Version 28/04/2022 11:52:33
Page 1 of 4
CSC1015F Assignment 10
File Input and Output
Assignment Instructions
This assignment involves constructing Python programs that perform file input and output.
Furthermore, your solutions to this assignment will be evaluated for correctness and for the following
qualities:
Documentation
o Use of comments at the top of your code to identify program purpose,
author and date.
o Use of comments within your code to explain each non-obvious functional
unit of code.
General style/readability
o The use of meaningful names for variables and functions.
Algorithmic qualities
o Efficiency, simplicity
These criteria will be manually assessed by a tutor and commented upon. Up to 10 marks will be
deducted for deficiencies.
Question 1 [35 marks]
Write a Python program called 8anagramsearch.py9 that searches a file for anagrams of a given
word, printing the results in alphabetical order.
Given two words, each is an anagram of the other if they contain the same letters in the same
quantities. For example, 'green' and 'genre'.
Here are 4 examples of intended program behaviour:
(NOTE: the input from the uses is shown in bold font.)
Sample I/O:
***** Anagram Finder *****
Enter a word: triangle
['alerting', 'altering', 'integral']
Sample I/O:
***** Anagram Finder *****
Enter a word: Orange
['onager']
Sample I/O:
***** Anagram Finder *****
Enter a word: back
Sorry, anagrams of 'back' could not be found.