Asn 2
Asn 2
Assignment 2
Modules, Slicing, Indexing, Branching and Loops
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.
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
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