Question

In: Computer Science

Smaller index Complete the following function according to its docstring using a while loop. The lists...

Smaller index

Complete the following function according to its docstring using a while loop. The lists we test will not all be shown; instead, some will be described in English. If you fail any test cases, you will need to read the description and make your own test.

def smaller_index(items):
""" (list of int) -> int
  
Return the index of the first integer in items that is less than its index,
or -1 if no such integer exists in items.
  
>>> smaller_index([2, 5, 7, 99, 6])
-1
>>> smaller_index([-5, 8, 9, 16])
0
>>> smaller_index([5, 8, 9, 0, 1, 3])
3
"""

Solutions

Expert Solution

def smaller_index(items):

    """ (list of int) -> int    

    Return the index of the first integer in items that is less than its index,

    or -1 if no such integer exists in items.

    >>> smaller_index([2, 5, 7, 99, 6])

    -1

    >>> smaller_index([-5, 8, 9, 16])

    0

    >>> smaller_index([5, 8, 9, 0, 1, 3])

    3

    """

    # iterate over items

    for i in range(len(items)):

        # found

        if items[i] < i:

            return i

    # not found

    return -1


# testing

print(smaller_index([2, 5, 7, 99, 6]))

print(smaller_index([-5, 8, 9, 16]))

print(smaller_index([5, 8, 9, 0, 1, 3]))

.

Screenshot:

Output:

.


Related Solutions

Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
write a program bus management system? using: if else, for loop, do while loop, function, arrays,...
write a program bus management system? using: if else, for loop, do while loop, function, arrays, string, structure
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Trying to complete this in Java. Application called 'Coffee Shop' that uses a while loop to...
Trying to complete this in Java. Application called 'Coffee Shop' that uses a while loop to build a customer order. The Coffee Shops sells: Coffee ($3.25), Espresso ($4.25), and Tea ($2.75). The coffee selection presents the customer with the choices of iced (no charge), cream (50 cents), and sugar (50 cents). The espresso selection presents the customer with choice of caramel (no charge) and chocolate (no charge) with one shot (no charge) or two shots ($1.25) of espresso. Once the...
The following table lists the activities needed to complete a project. The first column lists the...
The following table lists the activities needed to complete a project. The first column lists the activities and the “follows” column shows which other activity or activities, (if any), must be completed before these activities can start. The remaining columns give three estimates of the activity duration; the mean duration calculated from these estimates and standard deviation assuming a beta distribution of activity duration. Activity Follows Estimates of durations (days) Optimistic Most likely Pessimistic A -- 3 6 15 B...
The following table lists the activities needed to complete a project. The first column lists the...
The following table lists the activities needed to complete a project. The first column lists the activities and the “follows” column shows which other activity or activities, (if any), must be completed before these activities can start. The remaining columns give three estimates of the activity duration; the mean duration calculated from these estimates and standard deviation assuming a beta distribution of activity duration. Activity Follows Estimates of durations (days) Optimistic Most likely Pessimistic A -- 3 6 15 B...
Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock...
Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock type and a weight from the user. Keep a running total of the price for all requested rocks. Repeat until the user wants to quit. Quartz crystals cost $23 per pound. Garnets cost $160 per pound. Meteorite costs $15.50 per gram. Assume the user enters weights in the units as above. Return the total price of all of the material. Using Python For this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT