CSCI 2040: Introduction to Python 2021-2022 Term 1 Lab Assignment 1 Instructor: Siu Hang, Or Due: 23:59 on Wednesday, Oct. 06, 2021 Notes 1. You are allowed to form a group of two to do this lab assignment. 2. You are strongly recommended to bring your own laptop to the lab with Anaconda1 and Pycharm2 installed. You don’t even have to attend the lab session if you know what you are required to do by reading this assignment. 3. As python 2.x will not officially update anymore, Python 3.x is the only acceptable version for this assignment. It is necessary for us to use python 3.x as it has a lot more up-to-date libraries than python 2.x and much more stable. 4. For those of you using the Windows PC in SHB 924A (NOT recommended) with your CSDOMAIN account3, please login and open “Computer” on the desktop to check if an “S:” drive is there. If not, then you need to click “Map network drive”, use “S:” for the drive letter, fill in the path ntsvr6userapps and click “Finish”. Then open the “S:” drive, open the Python3 folder, and click the “IDLE” shortcut to start doing the lab exercises. You will also receive a paper document and if anything has changed, please be subject to the paper. 5. The programming assignments are graded by Python scripts on Linux! Just a reminder, the test scripts we provide can be used only on Linux for your own test. However, for those who use Windows, this assignment should be fine for you. Please DO NOT test your scripts on CSE Linux. The CSE Linux machine seems to have some problems with our test script, so if you happen to have any other Linux machines, you can test your scripts on them. If your script does not pass the tests of our grading script, which support both Linux and Windows, you cannot get full grades. You are able to test the input and output format for a exercise by the corresponding testing script. For example, by running test_p1.py, you can test p1.py for exercise 1. Note that the grading script will be DIFFERENT from the provided testing script. To use our testing scripts, you should install package pexpect, and run the testing script in the same folder of the script. 6. Passing the test scripts we have provided doesn’t guarantee full marks for your question as our grade scripts will test for more cases. 7. You may assume that all the corner cases we have not mentioned in this document will not appear in the hidden test cases, so you do not have to worry too much about wrong inputs unless you are required to do so. 1An open data science platform powered by Python. https://www.continuum.io/downloads 2A powerful Python IDE. https://www.jetbrains.com/pycharm/download/ 3A non-CSE student should ask the TA for a CSDOMAIN account. 1 of 6 CSCI 2040 Lab Assignment 1 Page 2 Exercise 1: The Five Numbers (20 marks) input() is a function that reads a string from a user’s input. The function can be used as input([‘prompt’]). For example, after executing x = input(‘type a number:’) and reading the user’s input “one”, the string ”one” will be stored to variable x. Please write a python script and save it as p1.py that reads five integers from the user’s input. You can use int() to convert a string to the corresponding integer data type. If anyone of the input is not an integer, the program should output “Your input is not an integer!” and skip this round. After that, calculate two values: the largest possible integer such that it is not greater than the average of all input numbers the 3rd largest of all valid inputs, if there are no 3 valid inputs, this value will become the smallest of all inputs Print the larger one of the two values above and Program ends. if there is at least one valid numeric input, else it will print nothing. A sample run should be as follows: Please input the first number: 1 Please input the second number: joke Your input is not an integer! Please input the third number: 10.5 Your input is not an integer! Please input the fourth number: 3 Please input the fifth number: 5 The value is 3. Program ends. If you use IDLE, you can test your script p1.py in the Python shell with >>> execfile(’path/to/p1.py’) Exercise 2: Palindrome Wonderland (20 marks) In a secret place near the back hill of CUHK, there is a palindrome wonderland. People there uses palindrome as their language. A palindrome is a string that reads the same backward and forward. For example, the string eye and able was I ere I saw elba are both palindromes. The palindrome wonderland has an entrance guard, who only understands palindrome and only allows people speaking palindrome to pass. You would implement such an entrance guard in p2.py. The entrance guard starts with the prompt Please input a palindrome: that first reads a string from the user’s input and check whether the input string is a palindrome or not and the script will not exit until the user has input a valid palindrome. If the input is not a palindrome, the guard would say I do not understand what you mean! Say again:, 2 of 6 CSCI 2040 Lab Assignment 1 Page 3 and if the user inputs palindrome pure number(which ensures that the guard can understand so that he would not say I do not understand what you mean! Say again:), the guard would think you are trying to bribe him, and he would say No BRIBING as I am loyal to the King! and listen to the next input. The script will print Welcome to the wonderland! and exit if the input string is a palindrome and not a valid pure number. For this exercise, uppercase and lowercase letters are treated as same characters. A sample run should be like as follows, Please input a palindrome: 210 I do not understand what you mean! Say again: 012 I do not understand what you mean! Say again: 010 No BRIBING as I am loyal to the King!: NB aA bn I do not understand what you mean! Say again: NB aA bn Welcome to the wonderland! Exercise 3: String Mountain (20 marks) Using while loop or for loop to write a script p3.py that reads an integer value n and a string str from the user’s input and then produces a 2n-1-column block, repeatedly. For the 2n-1-column output, the first column contains 1 str at the bottom, the second column contains 2 strs at the bottom, and so on until the n-th column, which contains exactly n strs. And the remaining n-1 columns will be the reverse of the first n-1 columns. The script will keep asking the user to input an integer value n and a string s and then print the corresponding 2n-1-column block. The script will print the line Program ends. and exit if the user inputs any integer less than 1. For this exercise, you don’t need to check whether the user’s input is an integer or not also you don’t need to consider that a very long input would screw the format. A sample run should be as follows: Enter an integer:4 Enter a string:1 1 111 11111 1111111 Enter an integer:2 Enter a string:^_^ ^_^ ^_^^_^^_^ Enter an integer:0 Program ends. 3 of 6 CSCI 2040 Lab Assignment 1 Page 4 Exercise 4: GPA Calculator (20 marks) In order to improve Grade Point Average (GPA4), the first thing at hand is to write a script p4.py for GPA calculation. You should refer to the footnote website to convert the letter grade into numeric grade. With the script, a user could input the letter grade followed by the credit of a course. Immediately after reading the letter grade for a course, the GPA calculator prints out the current GPA. The user could keep adding the grades for more and more courses until the user inputs exit. When the user inputs exit, the script will print the user’s GPA and line Program ends. and exit. The letter grade and credit are separated by comma followed by a space. If the credit is not positive or the letter grade is not the legal CUHK letter grade(Only capitals are allowed), your script should print Wrong input! and continue to wait for a new input. The GPA MUST be in the format of 2 decimal points. Use “0:.2f”.format(a) if you want to output 2 decimal points of a. A sample run should be as follows: A-, 3 Current GPA: 3.70 B+, 2 Current GPA: 3.54 B-, -2 Wrong input! G, 4 Wrong input! exit Your GPA: 3.54. Program ends. Exercise 5: Penalty Kick (20 marks) To hide the input in Python, you could simply replace input() with getpass.getpass(). import getpass secret_input = getpass.getpass() In a football match, whenever there is any foul inside the box area, penalty kick will be resulted. In this exercise, we will implement the penalty kick process as a number guessing. First, player 1 is required to input two integers x1 and x2, separated by a single space, such that x1, x2 ∈ [0, 10] and x2 x1 > 1. These two values will be secretly input and act as the goal posts of the net. Then another user, player 2 will then be required to input an integer x resembling the kick. If the integer fall within the goal posts region(x ∈ (x1, x2)), then it is a goal, you should print Goal! and 1 score will be given for this goal. If the input number x 4http://www.res.cuhk.edu.hk/en-gb/general-information/guide-to-grading-system 4 of 6 CSCI 2040 Lab Assignment 1 Page 5 is the same as x1 or x2(x = x1 or x = x2), you should print Top Bin!!! and 2 scores will be given to player2 as a reward for the top bin! If player 2 has missed this shot, just print You missed! and 0 score will be given. Player 2 has 5 chances to kick and you should print out the statistics for this player and the final score followed by a Program ends.. You may assume that we will only test integer inputs. Below is a sample run to illustrate the working. Please input goal posts: