XJCO 1012辅导、Python程序设计辅导

- 首页 >> Database作业
Page 1 of 4
Coursework 1
Introduction to Programming (XJCO 1012)
Please follow the instructions on preparing your submission. You are NOT ALLOWED to
import modules for this coursework. Comments and conformance to PEP 8 will be marked.
Standard university penalty of 5% of available marks per day, or part of a day, will apply to
late work. Late submissions are acceptable up to 7 days late. Feedback on late submissions
may not be provided within 3 weeks of submission.
Submission: You must submit your work via Gradescope.
Deadline: 1000 2/11/22.
Weighting: This coursework is worth 30% of the module grade.
1 Introduction
You are required to implement a word search puzzle checker. If you are unfamiliar with word
search puzzles, refer to https://en.wikipedia.org/wiki/Word_search and read the section
headed “Strategies”.
2 Preparation
Please follow the following instructions.
 Download the template file cwk1-files.zip from Minerva.
 Unzip cwk1-files.zip and you should have the file wordsearch.py, in which
you should write your code and submit to Gradescope.
 Write your name at the top of the file indicated by @author.
3 Basic Solution (25 marks)
For the basic solution, you need to implement the following functions.
3.1 Implementing valid_puzzle(puzzle: list) -> bool function (4 marks)
This function takes one argument puzzle which is a list of strings. This function returns
Boolean True if the puzzle is valid and Boolean False otherwise. A valid puzzle contains
strings of equal length. For example, given the following three puzzles, puzzle1 is valid
whilst puzzle2 (not all strings are the same length) and puzzle3 (contain item that is not a
string) are invalid. The strings in the puzzle are case insensitive. You may decide to convert
the strings to uppercase.
puzzle1 = ['RUNAROUNDDL', 'EDCITOAHCYV', 'ZYUWSWEDZYA', 'AKOTCONVOYV',
'LSBOSEVRUCI', 'BOBLLCGLPBD', 'LKTEENAGEDL', 'ISTREWZLCGY',
'AURAPLEBAYG', 'RDATYTBIWRA', 'TEYEMROFINU']
puzzle2 = ['RUNAROUNDDL', 'EDCIT', 'ZYUWSWEDZYA', 'AKOTCONVOYV',
'LSBOSEVRUCI', 'BOBLLCGLPBD', 'LKTEENAGEDL', 'ISTREWZLCGY',
'AURAPLEBAYG', 'RDATYTBIWRA', 'TEYEMROFINU']
puzzle3 = ['RUNAROUNDDL', ['EDCITOAHCYV'], ('ZYUWSWEDZYA'), 'AKOTCONVOYV',
'LSBOSEVRUCI', 'BOBLLCGLPBD', 'LKTEENAGEDL', 'ISTREWZLCGY',
'AURAPLEBAYG', 'RDATYTBIWRA', 'TEYEMROFINU']
Page 2 of 4
3.2 Implementing valid_wordlist(wordlist: list) -> bool function (3 marks)
This function takes one argument wordlist which is a list of strings such as ["SCALAR",
"TRAY", "BLEW", "SEVRUC", "TESTING"]. The function returns Boolean True if
wordlist is valid (a list of strings) and Boolean False otherwise. As in 3.1, the strings are
case insensitive.
3.3 Implementing basic_display(grid: list) -> None function (3 marks)
This function takes one argument grid which is a list of strings/lists. The function prints a
single blank space before and after each item in the string/sublist. Each string/sublist is
printed on the same line as shown in the following examples with grid=puzzle1 (from 3.1)
and grid= [['a', 'b', 'c', 'd', 'e'], ['h', 'l', 'j', 'k', 'l']]. As in 3.1, the characters/strings are case
insensitive.
Figure 1: grid=puzzle1 (from 2.1).
Figure 2: grid= [['a', 'b', 'c', 'd', 'e'], ['h', 'l', 'j', 'k', 'l']].
3.4 Implementing get_positions(puzzle: list, word: str) -> list
function (15 marks)
This function takes two arguments puzzle (assumed to be valid puzzle as in 3.1) and word
(a string). The function returns the indexes of characters of a word in the puzzle. The word
may be found horizontally, vertically, or diagonally, and in both forwards and backwards. If a
word is found in the puzzle, return a list on the position (as a tuple) of all characters in the
word, starting from the first character. For example, “TRAY” can be found in puzzle1 at
position (7, 2), (8, 2), (9, 2), (10, 2) as shown in Figure 3 and the function will return [(7, 2),
(8, 2), (9, 2), (10, 2)]. Hint: index starts from 0 instead of 1.
Figure 3: position for “TRAY”.
Page 3 of 4
If a word is not found in the puzzle, the function prints '{word}' not found. as shown in
Figure 4 for the word “TESTING” that is not found in puzzle1.
Figure 4: The word “TESTING” is not found in puzzle1.
4 Full Solution (13 marks)
4.1 Improve the get_positions(puzzle: list, word: str) -> list function
(5 marks)
Improve this function for scenarios in which a word is found at multiple positions. The
function returns all positions with each position as a separate list. For example, if a word is
found at 2 different positions, the function returns the positions as [[(5, 4), (6, 3), (7, 2)], [(7,
7), (8, 6), (9, 5)]]. What is that word in puzzle1?
4.2 Implementing coloured_display(grid: list, positions: list) ->
None function (5 marks)
This function takes two arguments puzzle (assumed to be valid puzzle as in 3.1) and
positions (returned positions in 3.4 and 4.1). The function will print the characters at
positions with a green background using ANSI escape codes as shown in Figure 5. For
example, to print character B in with green background, use print("\033[42m B
\033[0m "). The code \033[42m is for green background whilst \033[0m at the end is to
reset styles and colours. You can find out more colours at
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797.
Figure 5: coloured puzzle
4.3 Implementing wordsearch(puzzle: list, wordlist: list) -> None
function (3 marks)
This function takes two arguments puzzle and wordlist and check if both puzzle and
wordlist are valid with the valid_puzzle() and valid_wordlist() functions. If either
puzzle or wordlist is invalid, the function prints and returns ValueError, invalid
puzzle or wordlist. If both puzzle and wordlist are valid, display the solved puzzle in
colour with the coloured_display() function as shown in Figure 5.
Page 4 of 4
5 Short Report (7 marks)
Write a short report (max 2 pages, in PDF) on how to (1) automate the generation of valid
puzzles as those in 3.1 and (2) generate a list of all words that can be found in puzzle
generated in (1). You can get higher mark with working code implementations for both in a
separate python file.
6 Marking
Basic solution 25
Full solution 13
Coding style and comments 10
Report 7
Total 55

站长地图