Question: python programming Exercise 5 [Sets] A set in python is an unordered and un-indexed collection of unique immutable values. As a rough approximation, we can
![python programming Exercise 5 [Sets] A set in python is an](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4ee247d379_77966f4ee23eb461.jpg)
python programming


Exercise 5 [Sets] A set in python is an unordered and un-indexed collection of unique immutable values. As a rough approximation, we can think of a dictionary of keys without values.) The add() method can add something to a set and remove() can remove something from a set. >>> s = set() # create an empty set >>> s.add(10) # adds 10 to the set >>> 10 in S True >>> 1 in s False >>> len(s) 1 >>> s.remove (10) >>> len(s) 0 1 import random import time 2 3 4 5 n = 100 6 7 8 9 10 11 12 not-found') set list') set print( f found print( f list for n in [8,9,10,11,12,13,14,15,16]: N = (2**n) numberset = set() while len(numberSet) 07} : ', end=' ') 18 19 20 21 22 23 24 25 26 27 28 tic = time.perf_counter() for n in numberset: n in numberset toc = time.perf_counter() print(f'{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to True in set : {toc-tic:0.4f} seconds (for {N} searches)' ) 29 30 31 32 33 tic = time.perf_counter() for n in numberset: n in numberlist toc = time.perf_counter() print(f'{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to True in list : {toc-tic:0.4f} seconds (for {N} searches)' ) tic = time.perf_counter() for n in numberset: -n in numberset toc = time.perf_counter() print('{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to False in set : {toc-tic:0.4f} seconds (for {N} searches)' ) tic = time.perf_counter() for n in numberset: -n in numberlist toc = time.perf_counter() print(f'{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to False in list : {toc-tic:0.4f} seconds (for {N} searches)' ) print() Exercise 5 [Sets] A set in python is an unordered and un-indexed collection of unique immutable values. As a rough approximation, we can think of a dictionary of keys without values.) The add() method can add something to a set and remove() can remove something from a set. >>> s = set() # create an empty set >>> s.add(10) # adds 10 to the set >>> 10 in S True >>> 1 in s False >>> len(s) 1 >>> s.remove (10) >>> len(s) 0 1 import random import time 2 3 4 5 n = 100 6 7 8 9 10 11 12 not-found') set list') set print( f found print( f list for n in [8,9,10,11,12,13,14,15,16]: N = (2**n) numberset = set() while len(numberSet) 07} : ', end=' ') 18 19 20 21 22 23 24 25 26 27 28 tic = time.perf_counter() for n in numberset: n in numberset toc = time.perf_counter() print(f'{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to True in set : {toc-tic:0.4f} seconds (for {N} searches)' ) 29 30 31 32 33 tic = time.perf_counter() for n in numberset: n in numberlist toc = time.perf_counter() print(f'{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to True in list : {toc-tic:0.4f} seconds (for {N} searches)' ) tic = time.perf_counter() for n in numberset: -n in numberset toc = time.perf_counter() print('{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to False in set : {toc-tic:0.4f} seconds (for {N} searches)' ) tic = time.perf_counter() for n in numberset: -n in numberlist toc = time.perf_counter() print(f'{toc-tic:>8.5f}s', end='') #print(f'time for many searches evaluating to False in list : {toc-tic:0.4f} seconds (for {N} searches)' ) print()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
