Trace the call to confuse 4 7 19 24 35 47 64 85 97 19 this i

Trace the call to confuse ([4, 7, 19, 24, 35, 47, 64, 85, 97], 19), this is, show the output of the function. Write your answer in the space to the right. def confuse(lst, target); first = 0 last = len(lst) - 1 while first

Solution

first the list [4,7,19,24,35,47,64,85,97] and the target=19 are passed to the function confuse.

Now,
first=0
last=len(lst)-1
   =9-1
   =8
while first<=last ie 0<=8(condition is true so enters the loop)
now,
  middle=(0+(8-0))/2
= 4
the value at middle in the list is 35. the target is not equal to 35 but less than 35 therefore,
last =middle-1
   =3
once again repeat the loop
while first<=last:(0<3)condtion true therefore enters the loop

middle=(0+(3-0))/2
=1
target is not equal to the value at middle
but greater than middle, therefore first=middle+1
   =2

now repeat the loop

while first<=last(true)
  middle=2+(3-2)/2
   = 1
target not equal to middle value but greater than the middle value, therefore
first=middle+1
   =2
repeat the loop

2<=3 condition satisfies
middle=(2+1)/2
middle=1
target greater than middle value
first=middle+1
   =2
first is less than last

This gets repeated infinitely.To overcome this, middle=(first+last)/2

 Trace the call to confuse ([4, 7, 19, 24, 35, 47, 64, 85, 97], 19), this is, show the output of the function. Write your answer in the space to the right. def

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site