Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP0015

Mid-Term Take Home Paper

Sample

1)      Turtles

In this question you will add code to program circles.py, do not change the name of this file.  Your      

code should be formatted according to the style guidelines at the end of this paper. The program        

output should look like this:

 

Notes:

circles in the picture produced will not be measured.

the turtle draws no visible lines at all, the turtle’s pen colour is white so that its lines are invisible.

The centre of the orange circle is at position (0, 0). The turtle draws a circle by walking around the outside of the circle. To draw a circle you should move the turtle to the edge of the circle and turn it to face either north or south before drawing the circle.

is used for setting all the characteristics of the turtle before anything else is done. setup() is complete, do not add anything more to it.

function draw_inside_circle()

and

 

turtle.end_fill(). Take a look at the Python documentation

You  must  also  complete  function  draw_ring_of_circles(num_circles, outer_circle_radius). This function draws the ring of grey circles.

 

 

 

 

Description

num_circles

The number of circles in the ring.

outer_circle_radius

The radius of the circles in the outer ring.

 

Submission:

Type your student number into the comment at the top of the file circles.py. Do not change the name of the file. Submit the file circles.py at the submission link on moodle.

[5 marks]


 

2)      Conditions

In this question you will add code to the program midpoint.py, do not change the name of this file. Your code should be formatted according to the style guidelines at the end of this paper. You must

not use a python list to solve this problem.

 

Complete the function called  has_midpoint() that accepts three integers as  parameters and returns True if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your function should return False if no such midpoint relationship exists. The integers could be passed in any order; the midpoint could be the 1st, 2nd, or 3rd  parameter. You must check all cases. The midpoint can be calculated by adding the three integers and dividing by 3.

Submission:

Type your student number into the comment at the top of the file midpoint.py. Do not change the name of the file. Submit the file midpoint.py at the submission link on moodle.

[7

 

Loops

In this question you will add code to the program print_grid.py, do not change the name of this file. Your code should be formatted according to the style guidelines at the end of this paper.  You must not use a python list to solve this problem.

Complete the function named print_grid() that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The  numbers count  up from  1 to rows x cols. The output is displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the   next   column   to   the   right   once   the   bottom   of   the   current   column   is   reached.

Assume that rows and cols are always greater than 0.  Here are some example calls to your function and their expected results:

 

Call

print_grid(3, 6)

print_grid(5, 3)

print_grid(4, 1)

print_grid(1, 3)

Output

1, 4, 7, 10, 13, 16

2, 5, 8, 11, 14, 17

3, 6, 9, 12, 15, 18

1, 6, 11

2, 7, 12

3, 8, 13

4, 9, 14

5, 10, 15

1

2

3

4

1, 2, 3

 

Submission:

Type your student number into the comment at the top of the file print_grid.py. Do not change the name of the file. Submit the file print_grid.py at the submission link on moodle.

[7 marks]


4)       Functions

In this question you will add code to the program quiz.py, do not change the name of this file. Your code should be formatted according to the style guidelines at the end of this paper.

Complete the function named quiz(). Function quiz() has one parameter representing the number of questions in the quiz. The function will present the user with multiplication questions that they will answer. The function will inform the user whether the answer is correct or not. If the answer is wrong, the user should be prompted to retry the question. Here is some sample dialogue:

1.   What is 5 x 8? 48 No, try again

1.   What is 5 x 8? 40 Correct!

2.   What is 7 x 2? 14 Correct!

3.   What is 5 x 7? 35 Correct!

 

To solve this problem, you are also required to complete the additional function get_two_numbers(). Function get_two_numbers() will return a tuple containing two 1-digit numbers that will be used as operands for the calculation. You must use the random library to generate the 1-digit numbers.

Submission:

Type your student number into the comment at the top of the file quiz.py. Do not change the name of the file. Submit the file quiz.py at the submission link on moodle.

[7 marks]

 

5)       Strings

In this question you will add code to the program alpha_add.py, do not change the name of this file. Your code should be formatted according to the style guidelines at the end of this paper.

Complete the function  named  alpha_add().  Given  a  string  parameter  s,  sum  all the  digits  in  s between the parentheses ‘{‘ and ‘}’ and return the sum.

For example: alpha_add(‘ab{*23#o8}x’) returns 13.

 

If a  pair of parentheses does not exist or if there are no numbers between the parentheses, the function must return 0.

Submission:

Type your student number into the comment at the top of the file alpha_add.py. Do not change the name of the file. Submit the file alpha_add.py at the submission link on moodle.

[7 marks]


6)       Lists

In this question you will add code to the program is_unique.py, do not change the name of this file. Your code should be formatted according to the style guidelines at the end of this paper.   You must not use python sets to solve this problem.

Complete the function is_unique()that takes a list of integers as a parameter and returns a boolean value indicating whether or not the values in the list are unique (Truefor yes, False for no).  The values in the list are considered unique if there is no pair of values that are equal.

For example, if a variable called test_list contains the following values:                             test_list = [3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97, 10, 4]

Then the call of  is_unique(list) should  return True because there are  no duplicated values in this list.

If instead the list stored these values:                                                                     test_list = [4, 7, 2, 3, 9, 12, -47, -19, 308, 3, 74]

Then the call should return False because the value 3 appears twice in this list.  Notice that given this definition, a list of 0 or 1 elements would be considered unique.

 

Submission:

Type your student number into the comment at the top of the file   is_unique.py. Do not change the name of the file. Submit the file is_unique.py at the submission link on moodle.

[7 marks]

[PAPER TOTAL: 40 marks]

 

Style Guide

 

You must adhere to the style guidelines in this section.

Formatting Style

1.   Use Python style conventions for your variable names (snake case: lowercase letters with words separated by underscores (_) to improve readability).

2.   Choose good names for your variables. For example, num_bright_spots is more helpful and readable than nbs.

3.   Name constants properly using capital letters and put them at the top of your program.

4.   Indentation: use a tab width of 4 or 8. Your IDE should do this automatically for you. The best way to make sure your program will be formatted correctly is never to mix spaces and tabs -- use only tabs, or only spaces.

5.   Put a blank space before and after every operator. For example, the first line below is good but the second line is not:

b = 3 > x and 4 - 5 < 32

b= 3>x and 4-5<32

 

6.   Each line must be less than 80 characters long including tabs and spaces. You should break up long lines using \. You don’t need a continuation character if you are breaking up the parameters of a function.

7.   Function names should also be in snake_case: encrypt_message(), print_introduction().

8.   Functions should be no longer than about 12 lines in length. Longer functions should be decomposed into 2 or more smaller functions.