Exercises

The following exercises provide you with the opportunity to practice with algorithms in practical situations. The solutions to these exercises are found in the section Chapter 7.

  1. Use Big O notation to describe the time complexity of the following function. The function returns True if the array is a 100-sum array, and False if it is not.

    A 100-sum array meets the following criteria:

    • Its first and last numbers add up to 100.
    • Its second and second-to-last numbers add up to 100.
    • Its third and third-to-last numbers add up to 100, and so on.

      Here’s the function:

     def​ ​one_hundred_sum​(array):
     if​ (len(array) % 2 != 0) ​or​ ​not​ array:
     return​ False
     
      left_index = 0
      right_index = len(array) - 1
     
     while​ left_index < (len(array) ...

Get A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.