辅导案例-CMT309
Cardiff School of Computer Science and Informatics Coursework Assessment Pro-forma Module Code: CMT309 Module Title: Computational Data Science Lecturer: Dr. Matthias Treder, Dr. Luis Espinosa-Anke Assessment Title: CMT309 Programming Exercises Assessment Number: 1 Date set: 25-10-2019 Submission date and time: 06-12-2019 at 9:30 am Return date: This assignment is worth 30% of the total marks available for this module. If coursework is submitted late (and where there are no extenuating circumstances): 1 If the assessment is submitted no later than 24 hours after the deadline, the mark for the assessment will be capped at the minimum pass mark; 2 If the assessment is submitted more than 24 hours after the deadline, a mark of 0 will be given for the assessment. Your submission must include the official Coursework Submission Cover sheet, which can be found here: https://docs.cs.cf.ac.uk/downloads/coursework/Coversheet.pdf Submission Instructions Your coursework should be submitted via Learning Central by the above deadline. You have to upload the following files: Description Type Name Cover sheet Compulsory One PDF (.pdf) file Student_number.pdf Your solution to question 1 Compulsory One Python (.py) file Q1.py Your solution to question 2 Compulsory One Python (.py) file Q2.py Your solution to question 3 Compulsory One Python (.py) file Q3.py For the filename of the Cover Sheet, replace ‘Student_number’ by your student number, e.g. “C1234567890.pdf”. Make sure to include your student number as a comment in all of the Python files! Any deviation from the submission instructions above (including the number and types of files submitted) may result in a reduction of marks for the assessment or question part. For submission of the questions follow below instructions: Download the following files from Learning Central: • Q1.py • Q2.py • Q2_example_text.txt • Q3.py Test your implementation: For Q1, you can execute the function from the command line python3 Q1.py You can change the parameters of the function call in the main part of Q1.py. For Q2 and Q3, you can use python3 Q2.py my_example.txt python3 Q3.py my_example.py where my_example is a file with example text or code. You are encouraged to produce your own additional files to test your code. Any code must be submitted as stipulated in the instructions above. Any deviation from the submission instructions above (including the number and types of files submitted) will result in a mark of zero for the assessment or question. Staff reserve the right to invite students to a meeting to discuss coursework submissions Assignment Answer all of the following questions: Question 1 – Cycle convert (Total 30 Marks) Write a function cycle_convert that takes a variable x as an input and a cycle step size n. It converts the type of x into another type and returns the converted x. The conversion is in the following a fixed order of Python types: int → float → bool → string → complex Further instructions: • Cycle step size n controls how many steps you go. For instance, for n=1, an int is converted to float, a string is converted to complex, and so on. For n=2, an int is converted to bool (skipping the conversion to float), or a float is converted to string (skipping the conversion to bool). • Sometimes conversion is not possible. For instance, you cannot convert random words to numbers, but you needn’t worry about this. Assume all the conversions are possible. • Wrap around: When the step size brings you beyond the end of the conversion order, you start from the beginning. For instance, for n=2, a complex type is converted to float. Or for n=4, a string is converted to bool. • Negative step size: if n negative, the cycle goes to the left. For instance, for n=-1, a complex type is converted to string, or a bool type is converted to float. • The default value for n is 1. Examples: • If x=2 is an integer, then cycle_convert(x, 1) returns a float, and cycle_convert(x, 2) returns a Boolean • If x=”10” is a string, then cycle_convert(x, 1) returns the complex number 10+0j, cycle_convert(x, 2) returns the interger 10, and cycle_convert(x, 3) returns the float 10.0. As a starting point, use Q1.py from Learning Central. Do not rename the file or the function. Question 2 – Unit translator (Total 40 Marks) Write a function unit_translator(filename, D) that converts all length, filesize, and time units written in a text file to target units. The text file with converted units is then written out to a new text file; the filename of the new file is the name of the original file appended with ‘_translated’ (e.g. “example_text.txt” becomes “example_text_translated.txt”). Filename is a string that specifies the name of the input text file. D is a dictionary that specifies to which units lengths, filesizes, and times are to be converted. Further instructions: • The following types of units must be covered o length units: km, m, cm, mm o filesize units: B (for bytes), KB, MB, GB, TB, PB o time units: sec (for seconds), min, h • They are specified in dictionary D, e.g. D = {‘length’ : ‘km’, ‘filesize’ : ‘GB’ , ‘time’ : ‘h’}. • If the dictionary does not contain a particular key, the respective unit is not converted. o Example: if D = {‘length’ : ‘km’}, then only lengths get converted. Filesizes and times would not get converted. • Filename can be an absolute or relative path. • It is up to you whether integers are given in float or integer notation (e.g. converting “1024 MB” to either “1 GB” or “1.0 GB” are both correct solutions). To give an example, consider the example text provided in the file Q2_example_text.txt: At 2 h after starting the programme, the GPU was consuming about 12 GB at the time. Each chip had a length of 5.3 cm and the width was about 10 mm. Over time, VRAM memory consumption rose beyond the 16 GB limit and the programme crashed, though it ran really just short of 1024 MB. With better coding, some GB might have been saved. It just took 120 sec to restart the programme. Applying the unit translator with the function call unit_translator(filename, {'length' : 'cm', 'filesize':'GB'}) we obtain the following translated text (saved by the function in the file Q2_example_text_translated.txt): At 2 h after starting the programme, the GPU was consuming about 12.0 GB at the time. Each chip had a length of 5.3 cm and the width was about 1.0 cm. Over time, VRAM memory consumption rose beyond the 16.0 GB limit and the programme crashed, though it ran really just short of 1.0 GB. With better coding, some GB might have been saved. It just took 120 sec to restart the programme. As a starting point, use Q2.py from Learning Central. Do not rename the file or the function. Question 3 – Lambda machine (Total 30 Marks) A lambda expression represents an anonymous Python function. It can be used as a shorthand notation for simple Python functions. Write a function called lambda_machine that takes a filename for a Python file (e.g. “mytest.py”) as an input. The function parses the contents of the file and outputs a new file with “lambda_” prepended (e.g. “lambda_mytest.py”). • If the code contains a def statement, it is being translated into a lambda function. • Code that already represents a lambda function remains unchanged. Code that just contains a nested lambda function is removed, see these following example lines of code: o add = lambda x,y : return x+y à remains unchanged o d = sorted(d.items(), key = lambda x:x[1], reverse=True) à gets removed • Any code that is not part of a function or a lambda function should be ignored/removed from the output. • If there is multiple def statements, the same number of lambda functions needs to be produced. • Your solution needs to be able to deal with if-elif-else statements in the function body. • Your solution does not need to handle other situations such as functions defined within functions, or for/while loops. Simple example: Consider the file mytest.py which contains the following function: def add(a,b,c): return a + b + c Then lambda_machine should output a file that contains the function in lambda notation as follows: add = lambda a,b,c: a + b + c As a starting point, use Q3.py from Learning Central. Do not rename the file or the function. Learning Outcomes Assessed • Using the Python programming language to complete programming tasks • Familiarity with basic programming concepts and data structures • Reading and writing files Criteria for assessment Credit will be awarded against the following criteria. The functions you have implemented will be tested against different data sets. The score each implemented function receives is judged by its functionality. Additionally, in Q2, the efficiency and quality of the code will be part of the mark. The below table explains the criteria. Criteria Distinction (70-100%) Merit (60-69%) Pass (50-59%) Fail (0-50%) Q1 Excellent working condition with no errors Mostly correct. Minor errors in output Major problem. Errors in output Mostly wrong or hardly implemented Feedback and suggestion for future learning Feedback on your coursework will address the above criteria. Feedback and marks will be returned within 4 weeks of your submission date via Learning Central. In case you require further details you are welcome to schedule a one-to-one meeting. Feedback from this assignment will be useful for next year’s version of this module as well as the Python for Data Analysis module. Criteria Distinction (70-100%) Merit (60-69%) Pass (50-59%) Fail (0-50%) Q2 Functionality (80%) fully working application that demonstrates an excellent understanding of the assignment problem using relevant python approach. All required functionality is met, and the application are working probably with some minors’ errors Some of the functionality developed with and incorrect output major errors. Faulty application with wrong implementation and wrong output Quality (20%) Excellent documentation with usage of __docstring__ and comments Good documentation with minor missing of comments. Fair documentation. No comments or documentation at all Criteria Distinction (70-100%) Merit (60-69%) Pass (50-59%) Fail (0-50%) Q3 Excellent working condition with no errors Mostly correct. Minor errors in output Major problem. Errors in output Mostly wrong or hardly implemented