(License: CC BY-SA 4.0)
Complexity makes a difference in everyday use:
Fast and high quality services vs
those that lag, crash, and are unreliable
Does this algorithm scale up when it is given a large input?
Options:
Can you solve word capitalizations using binary numbers?
Take advantage of binary representation of computers!
&
AND|
OR>>
, <<
shifting bits to right or left, resp~
complement (negate)^
exclusive-OR (XOR)Examples:
x & 4 # will be non-zero only if x has 3rd bit on
x | 15 # will turn on lower 4 bits
x >> 4 # shift x by 4 bits to the right (faster than dividing by 2)
x & ~x # will always be 0
x ^ x # will always be 0
Why does indexing an array take constant time, $O(1)$?
Hint: In Python,
list
can contain heterogeneous items, but each item is an object reference that take up equal space.
hash("hello") -> 46487
Pros:
Cons:
Compare speed of:
intersection
)"hello" in my_list
)OR use defbench that GGC graduate Shaun Mitchell made!
Use local computer because cloud run times vary (individual submission only)
Instructions:
Examples on how to create a long list:
[ "test" + str(x) for x in range (10) ]
.split(" ")
to find words as a listHints:
"text" in list
) operator to search in list and setmax_repeat
to millions to see a differenceimport time
start = time.time()
repeat = 0
max_repeat = 1000
while repeat < max_repeat:
#################
# do something you want to measure here
a = 200**2
#################
repeat += 1
print("It took", "{:.10}".format((time.time() - start)), "seconds total")
print("It took", "{:.10}".format((time.time() - start) / max_repeat), "seconds per operation")