For a given list : first( list ) returns the first (left-most) element of list .
ID: 3730650 • Letter: F
Question
For a given list:
first(list) returns the first (left-most) element of list.
last(list) returns the last (right-most) element of list.
secondlast(list) reutrns the second right-most element of list.
appended(x,list) is a new list obtained by copying the contents of list and then adding x as a new last (right-most) element. The original list is not changed.
Consider the function:
function fiblist(n)
if n=0 then fiblist [0] endif
if n=1 then fiblist [0,1] endif
if n2 then
listfiblist(n1)
xlast(list)
xx+secondlast(list)
listappended(list;x)
fiblist list
endif
endfunc
1. fiblist(0) = [ ]. type the list brackets.
2. fiblist(2) = [ ]. type the list brackets.
3. fiblist(7) = [ ]. type the list brackets.
4. When computing fiblist(7), how many additional times is the fiblist() function called?
(Assume you are computing the function exactly as written and have not stored any previous results).
Explanation / Answer
1)
fiblist(0) = [0]
2)
fiblist(2) = [0,1,1]
3)
fiblist(7) = [0,1,1,2,3,5,8,13]
4)
fiblist(7) => fiblist(6) => fiblist(5) => fiblist(4) => fiblist(3) => fiblist(2) => fiblist(1)
6 additional calls and total 7 calls to fiblist() function made while calculating fiblist(7).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.