Big O Flashcards
Best, worst, expected cases
Describe the big O (or big theta) time for particular inputs or scenarios
Big O, big omega, big theta
Describe the upper, lower, and tight bounds for the runtime
Add runtimes
If the algorithm is “do this, then do that”
Multiply runtimes
If the algorithm is “do this for each time you do that”
Amortized Time
Concept that takes into account the worst case happens every once in a while, but once it does it won’t happen again for so long that the cost is “amortized”
The motivation for amortized analysis is that looking at the worst-case run time per operation, rather than per algorithm, can be too pessimistic.
Example: If the insertion time takes O(2x), the amortized time for each insertion is O(1)
.sort( )
Modifies the list it is called on in-place, runtime of O(n log n)
sorted( )
Creates a new copy of the modified list in memory, does not modify the original list, primary difference is additional space complexity vs .sort( )
Try/Except
A try statement may have more than one except clause, to specify handlers for different exceptions
Example:
while True:
… try:
… x = int(input(“Please enter a number: “))
… break
… except ValueError:
… print(“Oops! That was no valid number. Try again…”)
… except:
… print(“Don’t try this at home”)
Be wary of a last except: with nothing specified like above - the last except clause may omit the exception name(s), to serve as a wildcard. Use this with extreme caution, since it is easy to mask a real programming error in this way
Big O
Measures how time scales with respect to some input variables
O(1)
Constant time
O(n)
Linear time
O(n^2)
Quadratic time