December 2023
Intermediate to advanced
504 pages
11h 43m
English
The next example is an algorithm that collects every combination of two-character strings built from an array of single characters. For example, given the array ["a", "b", "c", "d"], we’d return a new array containing the following string combinations:
| | [ |
| | 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', |
| | 'ca', 'cb', 'cd', 'da', 'db', 'dc' |
| | ] |
Following is an implementation of this algorithm. Let’s see if we can figure out its Big O efficiency:
| | def word_builder(array): |
| | collection = [] |
| | |
| | for index_i, i in enumerate(array): |
| | for index_j, j in enumerate(array): |
| | if index_i != index_j: |
| | collection.append(i + j) |
| | |
| | return collection |
Here we’re running one loop nested inside another. The outer loop ...
Read now
Unlock full access