CMPUT 101 Assignment 2
Due Date: The due date is Friday, November 22 by 6pm. The assignment is to be
submitted on-line on eclass. Late submissions will not be accepted.
Collaboration Policy: This assignment is individual work. Do not share Python code
with other people from this course or outside of this course. Do not search for or copy
code from the Web. You can talk to others that are in this course about solution ideas
but do not copy code from anyone in this course or not taking this course. You must
cite who you talked to in the comments of your programs.
Submission: You need to submit two files: A2q1.py and A2q2.py on eclass. Use the
program “stubs” of these names provided on eclass. Your submitted solutions must be
in the correct .py format so that the TAs can run the programs. Keep the file names the
same as listed above. Do not submit pdf or doc or other incorrectly formatted files.
Remember to leave your programs on your lab account before the due date in case
something goes wrong with your submission.
Marking: The assignment is worth 8% of your final grade.
Marks are allocated for each question as follows:
• 10% Describe your program in a comment at the top of the solution
• 20% Program syntax is correct (Python reports no errors), program file can be
run by the TAs without error
• 50% Program works
• 20% Coding style (meaningful variable names, proper indentations, comments in
Question 1 [10 marks]:
Write a Python program that draws different shapes. First, define a function
drawLine(length) that takes an input parameter number, length, and draws one line of
stars (*) where the number of stars is equal to the input number length. This function
will be used throughout your program to draw a line and also other shapes.
Write a program that draws one of five shapes depending on the user’s choice: a line, a
square, a rectangle, a triangle, or a diamond. Your program will prompt the user to enter
their choice of shape: ‘l’ for line, ‘s’ for square, ‘r’ for rectangle, ‘t’ for triangle, and ‘d’ for
diamond. If a user enters a character that is not ‘l’, ‘s’, ‘r’, ‘t’, or ‘d’, the program should
display a message indicating that the user’s choice was an incorrect type of shape. If
the user enters a valid choice, your program will prompt the user to enter the size of the
shape. This will be the length of the line, or the length of the side of the square, both the
length of the “across” side and length of the “down” side for the rectangle, the length of
the side of a right triangle, or the length of the centre line of a diamond. Implement a
function to draw each shape; your program will call one of the five functions depending
on the shape the user chooses. Your program must be general enough to draw shapes
of different sizes. You may assume sizes given by the user will be greater than 0.
def drawRectangle(across_size, down_size):
Enter type of shape (l,s,r,t,d) : l
Enter size of line: 6
Enter type of shape (l,s,r,t,d) : s
Enter size of side of square : 2
Enter type of shape (l,s,r,t,d) : r
Enter the down side of rectangle : 3
Enter the across side of rectangle : 5
Enter type of shape (l,s,r,t,d) : t
Enter size of triangle : 3
Enter type of shape (l,s,r,t,d) : d
Enter size of diamond : 3
Enter type of shape (l,s,r,t,d) : p
Entered incorrect type of shape p
Question 2 [10 marks]:
Use Python to create a function cleanstring(S) to “clean up” the spaces in a sentence
S. The sentence may have extra spaces at the front and/or at the end and/or between
words. The subroutine returns a new version of the sentence without the extra spaces.
That is, in the new string, the words should be the same but there should be no spaces
at the start, only one space between each word and no spaces at the end. In addition,
there should be no spaces before a comma, a semi-colon, a period or an exclamation
mark following a word, but there should be one space after a comma or semi-colon.
You may assume that the comma, semi-colon, period, and exclamation mark are the
only punctuation characters that will be entered by the user.
This program is about you writing code to search through a string to find words and so
you are not allowed to use the split function or other built-in functions from Python. You
can instead solve this problem using the basic capabilities of the if and while statements
and string operations of len and concatentation.
For example: if the input is: ” Hello to the world !”
then the output should be: “Hello to the world!”
Your function should also print out the number of characters that have been removed
from the original string in order to obtain the final string. So, your function should return
a list of one string and one integer. The string in the list returned is the final string after
removing the extra characters and the integer in the list returned is the number of
Enter a string: Hello to the world !
A total of 19 characters have been removed from your string.
The new string is: Hello to the world!
Enter a string: Hello , this is cmput 101 .
A total of 15 characters have been removed from your string.
The new string is: Hello, this is cmput 101.
Make sure your program is general enough to work for other possible sentences. Use
the file A2q2.py that has been given to you on eclass. Note, keep the same name
when you submit it.
本网站支持 Alipay WeChatPay PayPal等支付方式
E-mail: email@example.com 微信号:vipnxx