def find(lst, item, low, high, indent):
"""
Finds index of string in list of strings, else -1.
Searches only the index range low to high
Note: Upper/Lower case characters matter
"""
print(indent, 'find() range', low, high)
range_size = (high - low) + 1
mid = (high + low) // 2
if item == lst[mid]: # Base case 1: Found at mid
print(indent, 'Found person.')
pos = mid
elif range_size == 1: # Base case 2: Not found
print(indent, 'Person not found.')
pos = 0
else: # Recursive search: Search lower or upper half
if item < lst[mid]: # Search lower half
print(indent, 'Searching lower half.')
pos = find(lst, item, low, mid, indent + ' ')
else: # Search upper half
print(indent, 'Searching upper half.')
pos = find(lst, item, mid+1, high, indent + ' ')
print(indent, 'Returning pos = %d.' % pos)
return pos
,attendees = []
attendees.append('Adams, Mary')
attendees.append('Carver, Michael')
attendees.append('Domer, Hugo')
attendees.ap - Answers True
def find(lst, item, low, high, indent):
"""
Finds index of string in list of strings, else -1.
Searches only the index range low to high
Note: Upper/Lower case characters matter
"""
print(indent, 'find() range', low, high)
range_size = (high - low) + 1
mid = (high + low) // 2
if item == lst[mid]: # Base case 1: Found at mid
print(indent, 'Found person.')
pos = mid
elif range_size == 1: # Base case 2: Not found
print(indent, 'Person not found.')
pos = 0
else: # Recursive search: Search lower or upper half
if item < lst[mid]: # Search lower half
print(indent, 'Searching lower half.')
pos = find(lst, item, low, mid, indent + ' ')
else: # Search upper half
,print(indent, 'Searching upper half.')
pos = find(lst, item, mid+1, high, indent + ' ')
print(indent, 'Returning pos = %d.' % pos)
return pos
attendees = []
attendees.append('Adams, Mary')
attendees.append('Carver, Michael')
attendees.append('Domer, Hugo')
attendees.ap - Answers True
def find(lst, item, low, high, indent):
"""
Finds index of string in list of strings, else -1.
Searches only the index range low to high
Note: Upper/Lower case characters matter
"""
print(indent, 'find() range', low, high)
range_size = (high - low) + 1
mid = (high + low) // 2
if item == lst[mid]: # Base case 1: Found at mid
print(indent, 'Found person.')
pos = mid
elif range_size == 1: # Base case 2: Not found
print(indent, 'Person not found.')
pos = 0
else: # Recursive search: Search lower or upper half
, if item < lst[mid]: # Search lower half
print(indent, 'Searching lower half.')
pos = find(lst, item, low, mid, indent + ' ')
else: # Search upper half
print(indent, 'Searching upper half.')
pos = find(lst, item, mid+1, high, indent + ' ')
print(indent, 'Returning pos = %d.' % pos)
return pos
attendees = []
attendees.append('Adams, Mary')
attendees.append('Carver, Michael')
attendees.append('Domer, Hugo')
attendees.ap - Answers False
A recursive function with parameter n counts up from any negative number to 0. An appropriate
base case would be n == 0. - Answers True
A recursive function can have two base cases, such as n == 0 returning 0, and n == 1, returning 1.
- Answers True
n factorial (n!) is commonly implemented as a recursive function due to being easier to
understand and executing faster than a loop implementation. - Answers False
In the code below, suppose str1 is a pointer or reference to a string. The code only executes in
constant time if the assignment copies the pointer/reference, and not all the characters in the
string.
srt2 = str1 - Answers True
Certain hardware may execute division more slowly than multiplication, but both may still be
constant time operations. - Answers True
The hardware running the code is the only thing that affects what is and what is not a constant
time operation. - Answers False
Computational complexity analysis allows the efficiency of algorithms to be compared. -
Answers True