exam 1 review cs 1803

13
CS 1803 Recitation 9/20

Upload: will-barr

Post on 04-Jul-2015

215 views

Category:

Education


2 download

DESCRIPTION

From section IE3

TRANSCRIPT

Page 1: Exam 1 Review CS 1803

CS 1803 Recitation 9/20

Page 2: Exam 1 Review CS 1803

Announcements OH NOES! AN EXAM IS WEDNESDAY!

What should you study?

No homework is due this week because of the exam.

It’ll be released tomorrow.

HW3 should be almost completely graded.

If you don’t have your grade, you should very soon.

Review session is tomorrow at 6 PM in Boggs B6

Yours TAs truly will be there!

Read the grading rubric at the bottom of every assignment.

We grade your stuff according to that rubric.

Page 3: Exam 1 Review CS 1803

HW2 comments Duplication of code It’s a problem.

Don’t do it.

Srsly.

Things that don’t work: if int(inputString) == int: Why?

myList.append([stuff]) Why?

The use of global

Checking for bad input!!!!!! OMGOSH! CHECK IT!

This was the biggest problem on HW3.

Page 4: Exam 1 Review CS 1803

Exam ReviewWe have not seen the exam…

Your guess is as good as ours.

We won’t see the exam until you do.

Q/A time

Written Practice Questions

Page 5: Exam 1 Review CS 1803

Practice Question 1 Trace the following code and write down the output:

def myFunc(x):

if(len(x) <=0):

print(“Done!”)

else:

print(x)

myFunc(x[:len(x)-1])

myFunc(“This is a string.”)

Page 6: Exam 1 Review CS 1803

Practice Question 1 Solution This is a string.

This is a string

This is a strin

This is a stri

This is a str

This is a st

This is a s

This is a

This is a

This is

This is

This i

This

This

Thi

Th

T

Done!

Page 7: Exam 1 Review CS 1803

Practice Question 2Write a function reverseList() that will take in a list as a

parameter and return the original list in reverse order.

Page 8: Exam 1 Review CS 1803

Practice Question 2 Solution def reverseList(x):

rList = []

for i in range(-1, (len(x)*-1)-1, -1):

print("i:", i, "list:", x[i])

rList.append(x[i])

return rList

Page 9: Exam 1 Review CS 1803

Practice Question 2 Solution def reverseList(x):

l=len(x) -1

new=[]

while(i>0)

new.append(x[i])

i = i-1

return new

Page 10: Exam 1 Review CS 1803

Practice Question 3 Briefly explain the use of the global keyword in Python. Why

it is needed?

Page 11: Exam 1 Review CS 1803

Practice Question 3 Solution The global keyword is used when we wish for a function to

modify a variable that is declared above the function’s scope.

All calls to the function will use the same instance of the

global variable. It is needed when we wish to use the same

variable among multiple functions.

Page 12: Exam 1 Review CS 1803

Practice Question 4Write a function named averageList(), which takes in no

parameters, which will prompt the user to enter a set of

numbers, separated by commas (You may assume the user

will follow your directions; you do not need to include error

checking). Then, you must print the average of the list to 3

decimal places.

Page 13: Exam 1 Review CS 1803

Practice Question 4 Solution def averageList():

st = input("Enter the list of numbers: ")

stList = st.split(',')

total = 0

for eachNumber in stList:

total += float(eachNumber)

total /= len(stList)

print("The average is: %.3f"%total)