Professional Documents
Culture Documents
Lecture Practice Questions 1
Lecture Practice Questions 1
PRACTICE
QUESTIONS
COMP1127
Practice Questions
>>> my_str="cat"
>>> my_str[0]="b"
a. "bat"
b. "b"
c. "bcat"
d. Syntax Error
Practice Question – MCQ 3
Assume you have a function get_pos() which returns the position at
which a number should be inserted into a sorted list. If the number
already exists in the list, the function returns the position at which the
number is found.
def get_pos(el, lst):
if lst == []:
return 0
elif el == lst[0]:
return 0
elif el < lst[0]:
return get_pos(el,[])
else:
return ……
>>>lst=[3,5,2,6]
>>>lst=lst.sort()
>>>lst
a. [3,5,2,6]
b. [2,3,5,6]
c. Nothing
d. [6,5,3,2]
Practice Question – MCQ 5
A Tree Position ADT creates a tree position pstn as a tuple
that specified x and y co-ordinates (in that order). Which of
the following pairs of functions would be appropriate
selectors for each of the x and y co-ordinate values?
marks=dict(zip(["CBE","GM","EKM"],[95,32,63]))
a. list(filter((lambda x:x>50),marks))
b. list(filter((lambda x:x>50),marks[1]))
c. list(filter((lambda x:x>50),marks.values()))
d. list(filter((lambda x:x>50),marks.keys()))
Practice Question – MCQ 7
What does the following function accomplish given a
dictionary D, and value V?
def loopy(D,V):
for K in D:
if D[K]==V:
return K
a. f
b. 6
c. 2
d. 24
Practice Question – MCQ 10
What would the output of the following statement.
>>>[x for x in [10,25,30,45] if x % 10 == 0 ]
a. [0, 5, 0, 5]
b. [10, 30]
c. [True False, True, False]
d. [10, 25, 30, 45]
COMP1127
Practice Questions
('pcd',[sn,[make,model,form],[storage,memory,processor]])
Practice Question – Short Answer 1
As an example, pcd_list has been created with
three devices as shown,
pcd1=make_pcd(1001,'Motorola','Xoom','Tablet',32,2,1.2)
pcd2=make_pcd(1002,make_desc'HP','3115M','Netbook',320,
8,1.3)
pcd3=make_pcd(1003,'Sony','PSP','Console',.32,.32,.33)
>>> pcd2
('pcd',[1002,['HP','3115M','Netbook'],[320,8,1.3]])
>>> pcd_list=[pcd1,pcd2,pcd3]
Practice Question – Short Answer 1
The functions of the PCD ADT are as follows:
Name Type Description
make_desc(make,model,form) Const returns a list of the 3 string arguments
make_specs(strge,mem,proc) Const returns a list of the 3 floating point arguments
make_pcd(sn,make,model,for Const returns the representation of the PCD as the tagged tuple
m,strge,mem,proc) shown above given the PCD's information
is_pcd(pcd) Pred checks whether the given argument is a valid PCD. A PCD
is a tagged tuple where the tag is “pcd”.
get_sn(pcd) Sel returns the stock number of a PCD
get_desc(pcd) Sel returns the list of make, model and form of a PCD
get_specs(pcd) Sel returns the list of storage, memory and processor of a PCD
(1) make_pcd()
(2) is_pcd()
(3) get_model()
(4) get_proc()
[8 marks]
Practice Question – Short Answer 1
def make_pcd(sn,make,model,form,strge,mem,proc):
def is_pcd(pcd):
def get_model(pcd):
def get_proc(pcd):
Practice Question – Short Answer 1
Student Attempt
def make_pcd(sn,make,model,form,strge,mem,proc):
def is_pcd(pcd):
def get_model(pcd):
def get_proc(pcd):
Practice Question – Short Answer 1
def make_pcd(sn,make,model,form,strge,mem,proc):
return ('pcd', [sn,[make,model,form], \
[storage,memory,processor]])
def is_pcd(pcd):
return type(pcd) == tuple and pcd[0] == 'pcd’ and
type(pcd[1]) == list and len(pcd) == 2
def get_model(pcd):
return pcd[1][1][1]
def get_proc(pcd):
return pcd[1][2][2]
Practice Question – Short Answer 1
b.
Using the higher order function map, write a function
called list_all_makes() that accepts a list of
PCDs and returns all the makes of PCD that are
available.
e.g. >>> list_all_makes(pcd_list)
['Motorola','HP','Sony']
[3 marks]
Practice Question – Short Answer 1
def list_all_makes(plist):
[6 marks]
Practice Question – Short Answer 1
Student Attempt
def list_all_makes(plist):
[6 marks]
Practice Question – Short Answer 1
def list_all_makes(plist):
return list(map([1][2][1], plist))
def list_all_makes(plist):
return list(map(lambda pcd: pcd[1][2][1], plist))
def list_all_makes(plist):
return list(map(get_make, plist))
Practice Question – Short Answer 1
c.
Using the inbuilt append function, write a function called
insert_pcd that takes a list of PCDs and the information
for a new PCD and inserts the PCD into the list.
e.g.
>>> insert_pcd(pcd_list,1004,'Apple','iPad','Tablet',16,1.5,1.2)
>>> pcd_list
[('pcd', [1001, ['Motorola', 'Xoom', 'Tablet'], [32, 2, 1.2]]),
def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
[6 marks]
Practice Question – Short Answer 1
Student Attempt
def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
[6 marks]
Practice Question – Short Answer 1
def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
pcd = ('pcd', [sn,[make,model,form], \
[strge,mem,processor]])
pcd = make_pcd(sn,make,model,form,strge,mem,proc)
pcd_list.insert(-1, pcd)
pcd_list.append(pcd)
Practice Question – Short Answer 1
def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
pcd = make_pcd(sn,make,model,form,strge,mem,proc)
pcd_list.append(pcd)
[6 marks]
Practice Question – Short Answer 1
d.
Using the higher order functions filter and map, write a
function called min_mem that accepts a list of PCDs and
returns the type of form factor for those devices in the list
with an amount of memory that is greater than or equal to
the amount specified. You do not need to consider the
possibility of multiple devices with the same form factor that
satisfy the requirements. (Hint: you will also need to write a
helper function that selects the PCDs that satisfy the
memory requirement).
e.g. >>> min_mem(pcd_list, min)
['Tablet','Netbook'] [6 marks]
Practice Question – Short Answer 1
[6 marks]
Practice Question – Short Answer 1
Student Attempt
def min_mem(pcd_list, min)
[6 marks]
Practice Question – Short Answer 1
PRACTICE
QUESTIONS