A4 Python for engineers · Practice
How running time grows
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write count_pairs(n) that loops over all pairs i < j of n items and counts them. Set c = count_pairs(n) and formula = n * (n - 1) // 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Rewrite the running total so it carries the sum forward in one pass. Set running to the list of cumulative sums of x.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set linear to the number of comparisons a linear search needs in the worst case on N = 1000000 items, and binary to the worst case for binary search, .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set dupes to the sorted list of values that appear more than once in x, using a set so the work is linear.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write has_pair(xs, target) that returns True if two different entries of xs add up to target, in one pass with a set of the values seen so far (no nested loops). Then set found for the starter's readings and target = 7.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A sort takes time proportional to . It takes 2.0 s on items. How long should it take on items?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
A quadratic algorithm takes 1 s at n = 1000. Set t to the seconds it takes at m = 2 times that size.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set ratio to how many times fewer multiplications an FFT () needs than a naive DFT () for N = 65536.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4