# Solutions for the second quiz COSC 1306 Fall 2104

Post on 18-Dec-2015

231 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Slide 1
• Solutions for the second quiz COSC 1306 Fall 2104
• Slide 2
• First Question What is the difference between a compiled language and an interpreted language? (25 points)
• Slide 3
• Answer What is the difference between a compiled language and an interpreted language? (25 points) A compiled language is translated before execution into binary code that can be directly executed. An interpreted language is interpreted just before and during execution into something executable.
• Slide 4
• Second question The University of Houston stores your names in a last name first followed by a comma and no space format as in Edison,Thomas Alva. Write a Python 3 function adding a space after the comma to and return something like Edison, Thomas Alva, which is more pleasing to the eye. (25 points). (Hint: I would split the string at the comma and rebuild it with the comma and the space.)
• Slide 5
• Answer def student_name(uh_name): lst = uh_name.split(',') return lst + ', ' + lst Space
• Slide 6
• Second question (variant) The University of Houston stores your names in a last name first followed by a comma and no space format as in Edison,Thomas Alva. Write a Python 3 function adding a space after the comma to and return something like Thomas Alva Edison,, which is more pleasing to the eye. (25 points). (Hint: I would split the string at the comma and rebuild it with the space.)
• Slide 7
• Answer def student_name(uh_name) : lst = uh_name.split(',') return lst + ' ' + lst
• Slide 8
• Third question Write a Python function computing restaurant tips. Your function should have as inputs the amount on the check the purchase and the tip rate in percent. In addition, it should assume a default tip rate of 15 percent. For instance, tip(50) should return 7.50 and tip(100, 18) should return 18. (25 points)
• Slide 9
• Answer def tip(bill, rate = 15) : return bill*rate/100
• Slide 10
• Fourth question Consider the following list containing two events, which themselves are lists: sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']] What would be the outcomes of the following Python statements, taken individually? (35 points) sched  = '1100' sched .append('Must attend') sched.pop(0)
• Slide 11
• Answer sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']] sched  = '1100' [['1100', 'Faculty meeting'] ['1430', 'COSC 1306']] sched .append('Must attend') [['1030', 'Faculty meeting', 'Must attend'] ['1430', 'COSC 1306']] sched.pop(0) [['1430', 'COSC 1306']]
• Slide 12
• Fifth question If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (45 points) lst.pop() lst[0:1] lst lst[:]
• Slide 13
• Answer If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (45 points) lst.pop() ['Ann', 'Barbara'] lst[0:1] ['Ann'] lst 'Ann' lst[:] ['Ann', 'Barbara', 'Charles']
• Slide 14
• Sixth question Complete the following program to have it compute the sum of all numbers entered. Your program should end once the user has entered a single minus sign and then print the total. (45 points)
• Slide 15
• Sixth question astring =input('Enter a number or a minus to terminate: ') sum = ___ while ___________: sum =___________ ________________ print('Total is ' + str(sum))
• Slide 16
• Answer astring =input('Enter a number or a minus to terminate: ') sum = 0 while astring != '-' : sum =sum + float(astring) astring =input('Enter a number or a minus to terminate: ') print('Total is ' + str(sum))
• Slide 17
• Seventh question Professor Jenkins has to compute her semester averages. Each of her student records consists of a student name followed by the grades she gave for the essays she assigned to her students as in: ['Avarez, Alonzo', 85, 70, 90, 85] Given that all essays have the same weight, what code will she write? (35 points) (Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)
• Slide 18
• Seventh question def semester_average(record): ___________________________ if ________ : return ______________ else: return ______________
• Slide 19
• Answer def semester_average(record): essays= record[1:] # eliminate first entry if len(essays) == 0 : return 0 else: return sum(essays)/len(essays)
• Slide 20
• Seventh question (variant) Professor Patel has to compute her semester averages. Each of her student records consists of a student name followed by the grades she gave for the essays she assigned to her students as in: ['Alvarez', 'Alonzo', 85, 70, 90, 85] Given that all essays have the same weight, what code will she write? (35 points) (Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)
• Slide 21
• Answer def semester_average(record): essays= record[2:] # eliminate two entries if len(essays) == 0 : return 0 else: return sum(essays)/len(essays)