+ All Categories
Home > Documents > 15-112 Fundamentals of Programming - Carnegie...

15-112 Fundamentals of Programming - Carnegie...

Date post: 22-May-2018
Category:
Upload: vuongdan
View: 221 times
Download: 3 times
Share this document with a friend
37
15-112 Fundamentals of Programming Lecture 8 September 14 , 2017
Transcript
Page 1: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

15-112 Fundamentals of Programming

Lecture 8September 14 , 2017

Page 2: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Announcements

Read pages 74 – 77

Page 3: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

What are we doing today?

ListsList functions

Page 4: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Lists – Lets start with the “why”

Given a sequence of numbers, how do you find the minimum value?How do you find: max, sum, average?How do you find: How many numbers are

above average?

Page 5: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Lists as sequence of values

Variables can hold one value at a timeLists can hold a number of different values

Page 6: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Create a list

A simple listmyList = [1, 4, 5, 8, 12]

An initialized listmyList = [0] * 5myList[0,0,0,0,0]

Adding element to a lista = [0] * 5b= 12a = a + [b]a [0,0,0,0,0,12]

Page 7: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Creating Lists

Combining two listsmyList = [1, 4, 5, 8, 12]yourList = [3,7,4,9]ourList = myList + yourList ourList would be [1, 4, 5, 8, 12, 3, 7, 4, 9]

Page 8: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Accessing Elements

a = [2, 3, 5, 7, 11, 13]print a[0]print a[2]print a[-1]print a[-3]print a[0:2]print a[1:4]print a[1:6:2]

2

13

5

7[2,3]

[3, 7, 13]

[3, 5, 7]

Page 9: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Modifying Elements

a = [2, 3, 5, 7, 11, 13]

print aa[2] = 0print a

[2, 3, 5, 7, 11, 13]

[2, 3, 0, 7, 11, 13]

Page 10: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

More List Operations

Slicing a ListmyList [1, 4, 5, 200, 12]print myList[1:3] [4, 5]

Assigning Value to a slicemyList[1:3] = [6,6,6,6]myList[1, 6, 6, 6, 6, 200, 12]

Page 11: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Finding Elements

Check for list membership: using “in”

a = [ 2, 3, 5, 2, 6, 2, 2, 7 ]print "a =", aprint (2 in a)print (4 in a)

Page 12: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Looping over Lists

a = [ 2, 3, 5, 7 ]

for item in a:print item

Page 13: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Exercise

Write a function called isSubset, that takes two lists as input parameters. The function should return True if all elements of first list are in second list.

Page 14: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Do you know the following about lists?

Can you declare an empty list?Can you declare a list with 5 1’s?Can you print the last element in a list?Can you access each element in a list using

a loop?Can you check if an element exists in a list?

Page 15: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

So what’s the outputa = [2,3,6,7,1,4,8,8,8]for i in range(0,3):

print a[i*3+0],a[i*3+1],a[i*3+2]

a = [3,2,1,6,4,8,7,7,5]for i in range(0,3):

print a[i],a[i+3],a[i+6]

Page 16: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Output?def fun2(x,y):

for i in range(1,len(y)):if x[i] == y[-i]:

print i

fun2([7,3,5,2,4,8],[7,3,4,2,7,3])

Page 17: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Representing Data using lists

We can use lists to represent 1-D data. Grades of all students in Quiz1

How about 2-D data using a list? Grades of all students in all quizzes.

Page 18: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Tic-Tac-Toe

Suppose we have a list that represents a tic-tac-toe board. How big should be the list? How should we initialize it?

1 2 3

4 5 6

987

[1,2,3,4,5,6,7,8,9]

Page 19: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Printing the board

How do we print this list so it looks like a board?

printBoard(mylist)?

1 2 3

4 5 6

987

[1,2,3,4,5,6,7,8,9]

Page 20: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Accessing an element

How do I get an element at row 0, column 2

getElement(board,row,column)?

1 2 3

4 5 6

987

[1,2,3,4,5,6,7,8,9]

Page 21: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Setting an element

How do I set an element at row 0, column 2

setElement(board,row,column,player)?

1 2 3

4 5 6

987

[1,2,3,4,5,6,7,8,9]

Page 22: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Winning

How do we know if a player won?

gameWon(board,player)?

1 2 3

4 5 6

987

[1,2,3,4,5,6,7,8,9]

Page 23: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

2-D lists

We have seen lists that hold integers, booleans, strings, etc.Can lists also hold other lists? [[1,2,3],[4,5,6],[7,8,9]]

+ How many elements in the above list? [1,2,[7,’a’,True],5,8]

+ How many elements in this list?

Page 24: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Accessing Elements in 2-D listsa = [[1,2,3],[4,5,6],[7,8,9]]

Reading valuesprint a[0][1]

Writing valuesa[1][0] = 12print a

[[1,2,3],[12,5,6],[7,8,9]]

Page 25: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Tic-Tac-Toe with 2-D lists

Suppose we use a 2D-list to represent a tic-tac-toe board. How big should be the list? How should we initialize it?

1 2 3

4 5 6

987

[[1,2,3],[4,5,6],[7,8,9]]

Page 26: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Printing the board

How do we print this list so it looks like a board?

printBoard(mylist)?

1 2 3

4 5 6

987

[[1,2,3],[4,5,6],[7,8,9]]

Page 27: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Accessing an element

How do I get an element at row 0, column 2

getElement(board,row,column)?

1 2 3

4 5 6

987

[[1,2,3],[4,5,6],[7,8,9]]

Page 28: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Setting an element

How do I get an element at row 0, column 2

setElement(board,row,column,player)?

1 2 3

4 5 6

987

[[1,2,3],[4,5,6],[7,8,9]]

Page 29: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Winning

How do we know if a player won?

gameWon(board,player)?

1 2 3

4 5 6

987

[[1,2,3],[4,5,6],[7,8,9]]

Page 30: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Strings

Strings are immutable Once created, a string itself cannot be

changed You can assign a different string to an existing

variablesname = “Daun Chung"name.lower()print namename = name.lower()print name

Page 31: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

String Functions

s.count(sub) – Count the number of occurrences of sub in s

name = “Daun Chuung"print name.count(“u")print name.count(“uu")

Page 32: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

String Functions

s.find(sub) Find the first index of sub in s, or -1 if not found

name = “Yasser El-Sayed"print name.find(“El")print name.find(“ “)print name.find(“Yasser")print name.find(“yasser")

Page 33: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

String Functions

s.index(sub) – find the first index of sub in s or error if not found

name = “Yasser El-Sayed"print name.find(“El")print name.find(“ “)print name.find(“Yasser")print name.find(“yasser")

Page 34: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

String Functions

s.rfind(sub) – Find the last index of sub in s or -1 if not founds.rindex(sub) – Find the last index of sub

in s or Error if not found

Page 35: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

String Functions

s.split() – Returns a list of words in s

name = “Muhammad Ali Najeeb Qazi"eachName = name.split()print eachName

OUTPUT [‘Muhammad', ‘Ali', ‘Najeeb‘, ‘Qazi’]

Page 36: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

String Functions

s.join(lst) – Join the list of words into a single string using s as separator

a = ["Harry", "Potter","and","the","goblet","of","fire"]t = " "print t.join(a) t = “..”print t.join(a)

Harry Potter and the goblet of fire

Harry..Potter..and..the..goblet..of..fire

Page 37: 15-112 Fundamentals of Programming - Carnegie …web2.qatar.cmu.edu/~srazak/courses/15112-f17/lectures...Do you know the following about lists? Can you declare an empty list? Can you

Working with Strings

Count the number of vowels in a stringa = raw_input(“Enter something> ”)s = a.lower()print s.count(‘a’) + s.count(‘e’) +

s.count(‘i’) + s.count(‘o’) +s.count(‘u’)


Recommended