solutions for the second quiz cosc 1306 fall 2104

21
Solutions for the second quiz COSC 1306 Fall 2104

Upload: louise-watts

Post on 18-Dec-2015

239 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Solutions for the second quiz COSC 1306 Fall 2104

Solutions for the second quiz

COSC 1306

Fall 2104

Page 2: Solutions for the second quiz COSC 1306 Fall 2104

First Question

What is the difference between a compiled language and an interpreted language? (2×5 points)

Page 3: Solutions for the second quiz COSC 1306 Fall 2104

Answer

What is the difference between a compiled language and an interpreted language?(2×5 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.

Page 4: Solutions for the second quiz COSC 1306 Fall 2104

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. (2×5 points).(Hint: I would split the string at the comma and rebuild it with the comma and the space.)

Page 5: Solutions for the second quiz COSC 1306 Fall 2104

Answer

def student_name(uh_name):

lst = uh_name.split(',')

return lst[0] + ', ' + lst[1]

Space

Page 6: Solutions for the second quiz COSC 1306 Fall 2104

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. (2×5 points).(Hint: I would split the string at the comma and rebuild it with the space.)

Page 7: Solutions for the second quiz COSC 1306 Fall 2104

Answer

def student_name(uh_name) :

lst = uh_name.split(',')

return lst[1] + ' ' + lst[0]

Page 8: Solutions for the second quiz COSC 1306 Fall 2104

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. (2×5 points)

Page 9: Solutions for the second quiz COSC 1306 Fall 2104

Answer

def tip(bill, rate = 15) :

return bill*rate/100

Page 10: Solutions for the second quiz COSC 1306 Fall 2104

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?(3×5 points) sched [0][0] = '1100' sched [0].append('Must attend') sched.pop(0)

Page 11: Solutions for the second quiz COSC 1306 Fall 2104

Answer

sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']] sched [0][0] = '1100'

[['1100', 'Faculty meeting'] ['1430', 'COSC 1306']]

sched [0].append('Must attend') [['1030', 'Faculty meeting',

'Must attend'] ['1430', 'COSC 1306']] sched.pop(0)

[['1430', 'COSC 1306']]

Page 12: Solutions for the second quiz COSC 1306 Fall 2104

Fifth question

If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (4×5 points)

lst.pop() lst[0:1] lst[0]   lst[:]

Page 13: Solutions for the second quiz COSC 1306 Fall 2104

Answer

If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (4×5 points)

lst.pop() ['Ann', 'Barbara'] lst[0:1] ['Ann'] lst[0]   'Ann' lst[:] ['Ann', 'Barbara', 'Charles']

Page 14: Solutions for the second quiz COSC 1306 Fall 2104

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. (4×5 points)

Page 15: Solutions for the second quiz COSC 1306 Fall 2104

Sixth question

astring =input('Enter a number or a minus to

terminate: ')

sum = ___

while ___________:

sum =___________

________________

print('Total is ' + str(sum))

Page 16: Solutions for the second quiz COSC 1306 Fall 2104

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))

Page 17: Solutions for the second quiz COSC 1306 Fall 2104

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? (3×5 points)(Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)

Page 18: Solutions for the second quiz COSC 1306 Fall 2104

Seventh question

def semester_average(record):

___________________________

if ________ :

return ______________

else:

return ______________

Page 19: Solutions for the second quiz COSC 1306 Fall 2104

Answer

def semester_average(record):

essays= record[1:] # eliminate first

entry

if len(essays) == 0 :

return 0

else:

return sum(essays)/len(essays)

Page 20: Solutions for the second quiz COSC 1306 Fall 2104

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? (3×5 points)(Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)

Page 21: Solutions for the second quiz COSC 1306 Fall 2104

Answer

def semester_average(record):

essays= record[2:] # eliminate two entries

if len(essays) == 0 :

return 0

else:

return sum(essays)/len(essays)