代写 python CompSci 101 – Assignment 05

CompSci 101 – Assignment 05
Due: 4:30pm, Sunday 17th February 2019.
Worth: This assignment is marked out of 35 and is worth 3.5% of your final mark.
Topics covered:
• tkinter, lists VERY IMPORTANT:
• In your program there must not be any variables used outside any of the functions.
• From the CompSci 101 Assignments web page, download the skeleton Python program (shown
at the back of this document):
https://www.cs.auckland.ac.nz/courses/compsci101ssc/assignments/
and rename the SkeletonA5.py file to “YourUsernameA5.py”, e.g., afer023A5.py.
• Your program must include a docstring at the top of the file containing your name, your username and a correct description of the program.
• Only use the features taught in CompSci 101.
Submission
Submit your Python program using the Assignment Dropbox: https://adb.auckland.ac.nz/Home/. Assignment 5 (35 marks)
This assignment creates a list of strings where each element has a random length and is made up of random digits. On the left hand side of the Canvas area the program draws a grid of coloured rectangles where each row corresponds to each list element and each rectangle along the row corresponds to each digit of the list element. On the right hand side of the Canvas area the program draws a mirror image of the grid of coloured rectangles.
1. Add your username to the title bar of the window
Currently the title bar of the program window displays “A5 by”. Add your username to the title bar of the program, i.e., the title bar should display the string, “A5 by yourUsername”, e.g., A5 by afer023.
2. get_list_of_rows(number_of_rows, min_in_each_row, max_in_each_row)
This function returns a list of strings. Each element of the list is made up of random digits (1, 2, 3, 4 or 5). The function is passed three integer parameters:
• The length of the list which is returned by the function,
• The minimum length of each string element of the list which is returned by the function,
• The maximum length of each string element of the list which is returned by the function,
The length of each string element of the list is a random number between the second and third parameter values (both inclusive).
CompSci 101, 2019 – 1 – Assignment Five

For example, a possible output produced by the following code is shown in the text box:
print(“1.”, get_list_of_rows(3, 5, 10)) print(“2.”, get_list_of_rows(2, 3, 7))
<--------------------------->
3. get_length_of_longest(pattern_list)
This function is passed a list of strings as a parameter. The function returns the length of the longest element of the parameter list. For example, the output produced by the following code is shown in the text box on the right:
print(“1.”, get_length_of_longest([‘234344415’, ‘3441423’, ‘2434523’])) print(“2.”, get_length_of_longest([‘2214545’, ‘142’]))
<--------------------------->
4. print_pattern_info(pattern_list, longest_length, size)
This function is passed three parameters: a list of strings and two integers. The function prints the parameter information in the following format:
• Line 1: the string “Pattern info:”,
• Line 2: the string “Size: n pixels” where n is the size parameter value,
• Line 3: the string “Number of rows: ” followed by the length of the pattern_list parameter, followed by the string “, longest row length: ” followed by the longest_length parameter,
• Line 4: a blank line,
• Line 5: the string “Pattern of digits in each row:
• The rest of the output is a numbered list (starting from the number 1) of all the elements of the parameter list. Each line of this numbered list is preceded by 4 spaces and there is a ” – ” between the line number and the element.
For example, the output produced by the following code is shown in the text box:
print_pattern_info([‘234344415’, ‘3441423’, ‘2434523’], 9, 10)
1. [‘234344415’, ‘3441423’, ‘2434523’] 2. [‘2214545’, ‘142’]
1. 9 2. 7
Pattern info:
Size: 10 pixels
Number of rows: 3, longest row length: 9
Pattern of digits in each row: 1 – 234344415
2 – 3441423
3 – 243452
<--------------------------->
CompSci 101, 2019 – 2 – Assignment Five

5. draw_pattern_version1(a_canvas, pattern_list, size, left, top)
This function is passed five parameters: the Canvas object, a list of string elements, followed by three integer parameters. The function draws a grid of coloured rectangles inside the canvas area. The left-top position of the grid of coloured rectangles is given by the last two parameter values and the size of each rectangle is given by the size parameter. Each element of the pattern_list parameter is a string of digits and each element corresponds to one row of coloured rectangles. Each digit of the string element dictates the colour of the rectangle, e.g., the digit 1 means that the rectangle is filled with red, the digit 2 means that the rectangle is filled with blue, the digit 3 means that the rectangle is filled with green, etc. Note that the first two lines of this function are (you may use any colours of your choice):
possible_digits = “12345”
colours = [“red”, “blue”, “green”, “yellow”, “black”]
Once you have completed this function you should see rows of coloured rectangles (of varying lengths) on the left hand side of the canvas area.
<--------------------------->
6. draw_pattern_version2(a_canvas, pattern_list, size, left, top, longest_width)
This function is passed six parameters: the Canvas object, a list of string elements, followed by four integer parameters. Inside the canvas area the function draws a mirror image of the grid of coloured rectangles drawn in Part 5. above, i.e., if you were to fold the canvas down the middle line each coloured square lies exactly over a square of the same colour. Note that the first two lines of this function are (use the same colours as you used in Part 6. above):
possible_digits = “12345”
colours = [“red”, “blue”, “green”, “yellow”, “black”]
Once you have completed this function and you test your program, you should see a mirror image of the Part 5. grid of coloured squares on the right hand side of the Canvas area.
<--------------------------->
The skeleton of this assignment program is shown below. You need to define five of the functions
and make one small change to the main() function.
Rename the file to ‘YourUsernameA5.py’, e.g., afer023A5.py.
from tkinter import *
import random
# 1) Make one change to the main() function def main():
size = 10
number_of_rows = 50
max_in_each_row = 40
min_in_each_row = 25
start_left1 = size * 2
start_down = size * 2
The program skeleton:
CompSci 101, 2019
– 3 –
Assignment Five

canvas_width = size * max_in_each_row * 2 +size * 5 canvas_height = number_of_rows * size + size * 4 window = Tk()
window.title(“A5 by “)
geometry_string = str(canvas_width)+”x”+str(canvas_height)+”+10+20″ window.geometry(geometry_string)
a_canvas = Canvas(window)
a_canvas.config(background=”blue”)
a_canvas.pack(fill=BOTH, expand = True) #Canvas fills the whole window
pattern_list = get_list_of_rows(number_of_rows, min_in_each_row, max_in_each_row) #2
longest_string_length = get_length_of_longest(pattern_list) #3 print_pattern_info(pattern_list, longest_string_length, size) #4
draw_pattern_version1(a_canvas, pattern_list, size, start_left1, start_down)#5
start_left2 = start_left1 + size * 3 + longest_string_length * size draw_pattern_version2(a_canvas, pattern_list, size, start_left2, start_down,
longest_string_length) #6
window.mainloop()
# 2. Complete the get_list_of_rows() function
def get_list_of_rows(number_of_rows, min_in_each_row, max_in_each_row):
possible_digits = “12345”
# 3. Complete the get_length_of_longest() function def get_length_of_longest(pattern_list):
return 0 #needs to be changed
# 4. Complete the print_pattern_info() function
def print_pattern_info(pattern_list, longest_length, size):
pass
# 5. Complete the draw_pattern_version1() function
def draw_pattern_version1(a_canvas, pattern_list, size, left, top):
possible_digits = “12345”
colours = [“red”, “blue”, “green”, “yellow”, “black”]
# 6. Complete the draw_pattern_version2() function
def draw_pattern_version2(a_canvas, pattern_list, size, left, down,
longest_width): possible_digits = “12345”
colours = [“red”, “blue”, “green”, “yellow”, “black”] main()
CompSci 101, 2019 – 4 – Assignment Five