Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Week 7 Practice Questions-1

The document outlines practice exercises for a programming course, focusing on strings, dictionaries, and sets in Python. It includes tasks for manipulating strings, creating account details, validating passwords, managing student lists, and generating unique email addresses. Each task provides specific requirements and examples to guide the coding process.

Uploaded by

umapathyashwin23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 7 Practice Questions-1

The document outlines practice exercises for a programming course, focusing on strings, dictionaries, and sets in Python. It includes tasks for manipulating strings, creating account details, validating passwords, managing student lists, and generating unique email addresses. Each task provides specific requirements and examples to guide the coding process.

Uploaded by

umapathyashwin23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSP1150/CSP5110: Programming Principles

Week 7: Practice Exercises


Strings, Dictionaries and Sets

Task 1 – Concept Revision in the Python Shell


Let’s revise the concepts from this module by typing some simple statements into the Python Shell.
Feel free to deviate from the exercise and experiment!

1. pun = 'Eating a clock is very time consuming!!!'


2. pun[23:]
3. pun = pun.rstrip('!')
4. pun.count('i')
5. punWords = pun.lower().split()
These statements manipulate a string in various ways.

6. scores = {'Mary': [22, 23, 36], 'Fred': [20, 21, 29]}


This creates a dictionary with two items. The items have keys of “Mary” and “Fred”, and the
values are both lists containing three integers (test scores out of 25, 25 and 50).

7. scores['Mary']
8. scores['Fred'][1]
These statements demonstrate how to refer to an item in a dictionary via its key.
Since the value is a list, you can then refer to an item in the list via its index.

9. scoreKeys = scores.keys()
This statement creates a live list of the keys in the “scores” dictionary. Check what it
contains now by typing “scoreKeys”, and then check it again after statement 10.

10. scores['Jane'] = [21, 24, 30]


11. scores['Fred'][2] = 39
These statements demonstrate adding a new item to the dictionary, and changing the value
of an item in one of the lists inside the dictionary.

12. HDs = [name for name in scores if sum(scores[name]) >= 80]


This statement uses list comprehension to loop through the “scores” dictionary, summing
the list of test scores for each person and adding their name to the “HDs” list if is at least 80.

13. set1 = {4, 2, 6, 3, 6, 2, 7}


14. set2 = set(9, 1, 0, 2, 4, 1, 4, 6)
15. set2 = set([9, 1, 0, 2, 4, 1, 4, 6])
Notice how sets automatically elminate duplicate values, and how the “set()” function
only accepts one parameter (hence the need to pass data as a list, or use “{” and “}” instead).

16. set1 | set2


17. set1 & set2
These statements show the union and intersection of the sets – try some other operations!

CSP1150 Page 1
Task 2 – Online account details
You are supposed to create a program which generates account details of an e-library.
Write a program that gets strings containing a person’s first and last name as separate values, and
then displays their “initials”, “name in address book”, and “username”. For example, if the user enters
a first name of “John” and a last name of “Smith”, the program should display
“Initials : J.S.”
“Name in address book: John SMITH”
“Username: jsmith”

Task 3 – Online account details - continuation


The above person who created the account is now supposed to create a password for the account. The
password is valid only if the following conditions are met.
• The length of the password should be between 6 and 12
• Should contain at least 1 capital letter
• Should contain at least 1 simple letter
• Should contain at least 1 number
• Should contain at least 1 special character out of: '~!#$%^*()_+-={}|[]\:<>?,./

Write a program which checks whether the password entered is valid or not. The program should take
a string (password) as an input and give the output as “Your password is valid” or “Your password is
invalid!”
Write a pseudocode before you start coding using python.
Suggested approach: You can create separate variables to check each condition and assign Boolean
values for them, and check if all these variables are True after you examine all the characters in the
password.

Checking for a special character is best achieved by creating a string containing all of the special
characters (add a backslash before the quote mark and the backslash characters in the string so that
they don’t get misinterpreted) and checking if the character is “in” it:
specialChars = '\'~!#$%^*()_+-={}|[]\\:<>?,./' Python

CSP1150 Page 2
Task 4 – Student List for a Subject
A teacher wants to enter the details of the students for his class into an online database and get three
lists of students who follow Mathematics, Science, and Computing.
Write a program that asks the user to enter the student number, name (first name last name), and the
subjects selected out of Mathematics, Science, and Computing. A student can be enrolled in one or
more of the three subjects, and all students are enrolled in one of the three subjects mentioned. The
program should ask the teacher for the number of students in the class and prompt for the student
number, name, and subjects (given in sample input) followed by the students accordingly. When all
the details of the students are entered, the program should display the list of students who are
following each subject in the following format (given in sample output).
Use dictionaries in python to achieve the given task.

Sample Input
Student Number: 4005632
Name: John Smith
Subjects followed: Mathematics, Computing

Sample Output
Mathematics (5)
1. John Smith
2. Jane Doe
3. Harry Potter
4. Peter Parker
5. Jack Reacher

Science (2)
1. Chin Li
2. Jane Doe

Computing (6)
1. Chin Li
2. Jane Doe
3. Harry Potter
4. Peter Parker
5. Jack Reacher
6. John Smith

Now modify the program to handle any input errors.

CSP1150 Page 3
Task 5 – Email Generator
Create a new file in the Python Editor and copy-paste the following code into it:
students = {400123: 'John Smith', 400159: 'Du Pen-Gu', 400527: 'Chin Li', Python
400321: 'Jane Doe', 400357: 'Harry Potter', 400624: 'Peter Parker'}

This simply creates a dictionary named “students” which contains 6 items. The keys are student
numbers, and the values are their names. Your task is to write a program that will generate email
addresses from the student names / numbers provided, implementing the following rules:
a. All characters in an email address should be in lowercase.

b. Dashes (“-”) or any other symbols in a student’s name should not appear their email
address.

c. An email address consist of the first letter of the student’s first name, followed by the
first 4 letters of their surname, joined with “@example.com” (e.g. “John Smith” would
become “jsmit@example.com”). We will assume that a student’s name only consists
of a first name and surname.

d. Email addresses must have a minimum length of 5 characters before the domain name
(before the “@” symbol). If a student’s surname is less than 4 characters long, include
add “0”s (zeroes) to the end (e.g. “Chin Li” becomes “cli00@example.com”).

Write a pseudocode before you start coding using python and make sure there is clear logic to solve
your problem.

Task 6 – Enhancing the Email Address Generator


Enhance the email address generator to incorporate one additional requirement:

e. Email address must be unique – if a email address would be identical to a previous


one, resolve it by adding a number to the end. Start with 1, and increment the
number until a unique email address is generated (e.g. if “jchen@example.com” exists,
and if another “jchen@example.com” is to be added, it should be
“jchen1@example.com”)

Before coding using python, write a pseudocode to get a picture of what exactly needs to be done.

Use the following dictionary to test your code


Python
students = {400123: 'John Smith', 400159: 'Du Pen-Gu', 400527: 'Chin Li',400321: 'Jane
Doe', 400357: 'Harry Potter', 400624: 'Peter Parker', 400555: ‘Jane Smith’, 400111:
‘Hung Li’, 400233: ‘Chen Li’}

CSP1150 Page 4

You might also like