0% found this document useful (0 votes)
6 views6 pages

Asn 2

This document outlines Assignment 2 for CMPT 141, which focuses on modules, slicing, indexing, branching, and loops in Python. It consists of four questions, each requiring individual programming tasks with specific submission formats and evaluation criteria. The assignment is due on May 25, 2023, and must be submitted electronically via Canvas.

Uploaded by

thinknow402
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Asn 2

This document outlines Assignment 2 for CMPT 141, which focuses on modules, slicing, indexing, branching, and loops in Python. It consists of four questions, each requiring individual programming tasks with specific submission formats and evaluation criteria. The assignment is due on May 25, 2023, and must be submitted electronically via Canvas.

Uploaded by

thinknow402
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Computer Science CMPT 141

176 Thorvaldson Building Spring 2023


110 Science Place, Saskatoon, SK, S7N 5C9, Canada
Telephine: (306) 966-4886, Facimile: (306) 966-4884 Introduction to Computer Science

Assignment 2
Modules, Slicing, Indexing, Branching and Loops

Date Due: Thursday, May 25, 2023, 11:59pm Total Marks: 18

General Instructions

• This assignment is individual work. You may discuss questions and problems with anyone, but
the work you hand in for this assignment must be your own work.
• Each question indicates what to hand in. You must give your document the name we prescribe for
each question, usually in the form aNqM, meaning Assignment N, Question M. Put your name and
student number at the top of every document you hand in. These conventions assist the markers
in their work. Failure to follow these conventions will result in needless effort by the markers, and
a deduction of grades for you. Do not submit folders, zip documents, even if you think it will help.
• Programs must be written in Python, and the file format must be text-only, with the file extension
.py.
• Documents submitted for discussion questions should make use of common file formats, such as
plain text (.txt), Rich Text (.rtf), and PDF (.pdf). We permit only these formats to ensure that our
markers can open your files conveniently.
• Assignments must be submitted electronically to Canvas.
• Canvas will not let you submit work after the assignment deadline. It is advisable to hand in
each answer that you are happy with as you go. You can always revise and resubmit as many
times as you like before the deadline; only your most recent submission will be graded.
Department of Computer Science CMPT 141
176 Thorvaldson Building Spring 2023
110 Science Place, Saskatoon, SK, S7N 5C9, Canada
Telephine: (306) 966-4886, Facimile: (306) 966-4884 Introduction to Computer Science

Question 1 (4 points):
Purpose: To practice string methods and conditional branching

For this question, you’ll write a simple function that accepts alphanumeric string. Then your program will
display number of characters, number of special symbols, number of digits in that string.
First, your program should let users input an arbitrary string.
Second, extract a single character from string and check it whether it is a alphabet, digit or special symbol.
Based on that check, increment the counter.
Finally, the program should print this result out to the console.

Sample Run
Sample input and output (input typed by the user is shown in green text, output value of a variable is shown
in blue text):
 
Please input a string :abcdefg_123
Number of characters : 7
Number of digits : 3
Number of special symbols : 1
 

What to Hand In
• A file called a3q1.py containing your finished program, as described above.

Evaluation
• 1 mark: For correct input
• 3 marks: For the correct calculation for digit counter, alphabet counter and special symbol counter
• -1 mark if the student did not include their name, NSID, student number and instructor’s name at the
top of the submitted file.

Page 2
Department of Computer Science CMPT 141
176 Thorvaldson Building Spring 2023
110 Science Place, Saskatoon, SK, S7N 5C9, Canada
Telephine: (306) 966-4886, Facimile: (306) 966-4884 Introduction to Computer Science

Question 2 (2 points):
Purpose: To practice modules and conditional branching

For this question, you’ll write a program that checks whether a randomly generated integer is odd number.
First, your program should import both the random modules. Make sure to use the import syntax as shown
in the course readings.
Then, your program should generate a random number between 1 and 10000 (inclusive).
Finally, your program should display that number to the console. If the number is a odd number, an addi-
tional message should be displayed to say so. Use a conditional statement along with any functions from
the math module that you think you need to do this.

Sample Run
Here is a possible sample run (output value of a variable is shown in blue text):
 
Your random number is : 5062
 
Here is another one.
 
Your random number is : 961
Amazing ! 961 is an odd number !
 

What to Hand In
• A file called a3q2.py containing your finished program, as described above.

Evaluation
• 1 mark: For correct input
• 1 mark: For correct output
• -1 mark if the student did not include their name, NSID, student number and instructor’s name at the
top of the submitted file.

Page 3
Department of Computer Science CMPT 141
176 Thorvaldson Building Spring 2023
110 Science Place, Saskatoon, SK, S7N 5C9, Canada
Telephine: (306) 966-4886, Facimile: (306) 966-4884 Introduction to Computer Science

Question 3 (8 points):
Purpose: To practice chained if-elif-else constructs.
Degree of Difficulty: mod The code is not difficult, but there’s a lot to read about it.

The game "rock-paper-scissors-lizard-spock" is an extension of the commonly known game "rock-paper-


scissors". If you’re not familiar with rock-paper-scissors, click here. Rock-paper-scissors-lizard-spock adds
two additional moves to the basic game. The rules are summarized both in this educational video (click to
view), and the image below. The arrows indicate which move beats which. For example, the arrow from
paper to Spock indicates that paper disproves Spock (if one player plays paper, and the other plays Spock,
the one that played paper wins).

There are 25 possible pairs of moves, so sometimes it’s hard to remember them. You will write a computer
program that will act as a referee. The program will ask for the moves made by the two players via console
input and report which player won.
To solve this problem, you’ll write a function that accepts the two moves made by the players as arguments,
and returns the outcome (for more detail, see below). Separately, you’ll write a main program to perform
the console input to request player 1’s move, and player 2’s move, and then call the function to determine
the outcome.
Note: The computer is not one of the players, the computer only determines who won given the moves
that the two human players made.

Sample Run
Sample input and output (input typed by the user is shown in green text) for two different runs, the first
showing a winner, the second showing a tie.
 
Enter move for player 1: spock
Enter move for player 2: rock
Player 1 wins !
 
 
Enter move for player 1: lizard
Enter move for player 2: lizard
It was a tie !
 

Page 4
Department of Computer Science CMPT 141
176 Thorvaldson Building Spring 2023
110 Science Place, Saskatoon, SK, S7N 5C9, Canada
Telephine: (306) 966-4886, Facimile: (306) 966-4884 Introduction to Computer Science

Advice on how to get started


(a) Write a function called rock_paper_scissors_lizard_spock() that has two parameters: the first pa-
rameter is the move made by player 1, the second parameter is the move made by player 2. The
function should return:
• 1 when player 1 wins
• 2 when player 2 wins
• 0 if the game is tied
Hint: There’s a bad way and a better way to do this question. You’ll know you’re doing it the bad way
if your program is very long, and if you are tempted to copy/paste/edit to write your code.
The bad way looks at the problem from the point of view of analyzing the 25 combinations of moves by
the players.
The better way consists of looking at the problem from the point of view of the three possible outcomes:
a win for player 1, a win for player 2, or a tie. Try to aim for this approach.
(b) Write an appropriate docstring for your function in part (a).
(c) In the main program (after the function definition), write code to prompt for, and read, the players’
moves from the console. You may assume that the user always enters one of the five strings: 'rock',
'paper', 'scissors', 'lizard', 'spock'.
(d) Call the rock_paper_scissors_lizard_spock() function giving the two moves as arguments, and save
its return value using a variable.
(e) Use the return value to print a message to the console indicating which player won, or whether it was
a tie.
(f) Your program only has to referee one game. To referee another game, run the program again!

What to Hand In
• A file called a3q3.py containing your finished program, as described above.

Evaluation
• 1 mark: Your function takes two string arguments, one move chosen by each player, and returns the
integer values 0, 1, or 2.
• 3 marks: Your function correctly uses a chained-if statement to determine the outcome of the game.
• 1 mark for good organization of your function logic (i.e. using the better way, and not the bad way)
• 1 mark for an acceptable docstring for rock_paper_scissors_lizard_spock();
• 1 mark: Console input is present and external to rock_paper_scissors_lizard_spock()
• 1 mark: Game result is correctly displayed and is external to rock_paper_scissors_lizard_spock()
• -1 mark if identifying information is missing (name, NSID, student number and instructor’s name).

Page 5
Department of Computer Science CMPT 141
176 Thorvaldson Building Spring 2023
110 Science Place, Saskatoon, SK, S7N 5C9, Canada
Telephine: (306) 966-4886, Facimile: (306) 966-4884 Introduction to Computer Science

Question 4 (4 points):
Purpose: To practice conditional branching, strings and loops.
Degree of Difficulty: easy

Given two strings, a and b. Write a program to create a new string c made of the first char of a, then the
last char of b, Next, the second char of a and second last char of b, and so on. Any leftover chars go at the
end of the result. (Hint: This program requires length calculation of each string. One loop is required for
implementation.)

Sample Run
3 use cases are provided based on the length of input strings.Sample input and output (input typed by the
user is shown in green text, output value of a variable is shown in blue text):
 
Case 1
Please input a string1 :abc
Please input a string2 :xyz
The resultant string is : azbycx
 
 
Please input a string1 :abcdef
Please input a string2 :xyz
The resultant string is : azbycxdef
 
 
Please input a string1 :abc
Please input a string2 :xyz123
The resultant string is : a3b2c1zyx
 

What to Hand In
• A file called a3q4.py containing your finished program, as described above.

Evaluation
• 1 mark: Correct sequencing of output string.
• 1 mark: Length calculation and comparision logic.
• 1 mark: The resultant string is correct (by concatenation).
• 1 mark: The input and output of the console are correct.
• -1 mark if the student did not include their name, NSID, student number and instructor’s name at the
top of the submitted file.

Page 6

You might also like