The following exercises provide you with the opportunity to practice analyzing algorithms. The solutions to these exercises are found in the section, Chapter 5 .
-
Use Big O Notation to describe the time complexity of an algorithm that takes 4N + 16 steps.
-
Use Big O Notation to describe the time complexity of an algorithm that takes 2N 2 .
-
Use Big O Notation to describe the time complexity of the following function, which returns the sum of all numbers of an array after the numbers have been doubled:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>double_then_sum</strong> (array)</td></tr><tr><td> <span> </span></td><td>doubled_array = []</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td>array. <strong>each</strong> <strong>do</strong> |number|</td></tr><tr><td> <span> </span></td><td>doubled_array << number *= 2</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td>sum = 0</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td>doubled_array. <strong>each</strong> <strong>do</strong> |number|</td></tr><tr><td> <span> </span></td><td>sum += number</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>return</strong> sum</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
-
Use Big O Notation to describe the time complexity of the following function, which accepts an array of strings and prints each string in multiple cases:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>multiple_cases</strong> (array)</td></tr><tr><td> <span> </span></td><td>array. <strong>each</strong> <strong>do</strong> |string|</td></tr><tr><td> <span> </span></td><td>puts string. <strong>upcase</strong> </td></tr><tr><td> <span> </span></td><td>puts string. <strong>downcase</strong> </td></tr><tr><td> <span> </span></td><td>puts string. <strong>capitalize</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
-
The next function iterates over an array of numbers, and for each number whose index is even, it prints the sum of that number plus every number in the array. What is this function’s efficiency in terms of Big O Notation?
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>every_other</strong> (array)</td></tr><tr><td> <span> </span></td><td>array. <strong>each_with_index</strong> <strong>do</strong> |number, index|</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> index. <strong>even?</strong> </td></tr><tr><td> <span> </span></td><td>array. <strong>each</strong> <strong>do</strong> |other_number|</td></tr><tr><td> <span> </span></td><td>puts number + other_number</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
Copyright © 2020, The Pragmatic Bookshelf.