Lect07 Data AbstractionII

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Data Abstractions contd

Data types

1
Data Abstraction
Every programming language must provide a
means of representing collections of data
Allows user to think at a higher level than just bits or
numbers
Builtin compound data provide basic means of
combination for primitive data
Users should be able to define their own
compound data types

2
Python Compound Data
Strings
Lists
Tuples (basically immutable lists)
Dictionaries

3
Python Tuples
Tuples are just like
>>> x = (10, 20, 30, 40) lists
>>> len(x)
4 Most list operations
>>> x[0] supported with same
10
>>> x[1:2] syntax
(20, 30)
Cannot modify their
contents though

4
Python Tuples Examples

def sum_mult(x,y): >>>sum_mult(3,5)


return x+y, x*y (8,15)

>>> even_odd([4,3,1,6,8,12,11,13,15])
def even_odd(lst): ([4, 6, 8, 12], [3, 1, 11, 13, 15])
elst=[]
olst=[]
for i in lst:
if i %2==0:
elst=elst + [i]
else:
olst=olst+[i]
return elst, olst 5
Data Structure Example
Suppose that we would like to maintain a list of
students as part of an information processing system.
Each student record contains the following
(1) a student id,
(2) students name given as a list of strings with surname
and first name,
(3) a list of courses with their corresponding coursework
grades, also given as a list.
For example, for the course COMP1126, 7, 8 are the quiz
results for Jane, 10 is her lab grade and 50 is her project grade.
In COMP1127 her grades are 0,6,9,20.

6
Example Cont.
Draw the memory structure to represent the following
expression

student=[62000001, ["Jane","Doe"],
[ ["COMP1126",[7, 8, 10,50] ], ["COMP1127",[0,6,9,20] ] ] ]

7
Example Cont.

student

62000001

Jane Doe

COMP1127
0 6 9 20
COMP1126
7 8 10 50
Write functions to do following...
Return all names of a given student as a list
e.g. >>>get_sname(student)
["Jane","Doe"]

List all courses done by a given student e.g. std.


e.g. >>>get_courses(student)
[COMP1126",COMP1127"]

9
Write functions to do following...
Return all names of a given student as a list
def get_names(s):
return s[1]
List all courses done by a given student e.g. student.
def get_courses(s):
clist=[]
glist=s[2]
for i in glist:
clist+=[i[0]]
return clist
10
Write functions to do following...
Given a student and a course code return the list
of grades.
e.g. >>>get_grades(student,COMP1127)
[0, 6, 9, 20]
Given a student and a course code return total
marks and return True if the total marks for the
course is greater than 60 and False otherwise.
e.g. >>>is_A(student,COMP1126)
True
11
Write functions to do following...
Given a student and a course code return the list
of grades.
def get_grades(s,ccode):
glist=s[2]
for i in glist:
if ccode==i[0]:
return i[1]
return []

12
Write functions to do following...
Given a student and a course code return total
marks and return True if the total marks for the
course is greater than 60 and False otherwise.
def is_A(s,ccode):
glist=get_grades(s,ccode)
total = 0
for i in glist:
total+= i
if total > 60:
return True
else:
return False 13

You might also like