Commit 3bb9426f authored by Loic Huder's avatar Loic Huder
Browse files

Removed except:

parent 45f02d76
Pipeline #33210 passed with stage
in 1 minute and 1 second
......@@ -310,11 +310,11 @@
ns.pop()
```
%%%% Output: execute_result
12
3
%% Cell type:markdown id: tags:
## Complexity :
......@@ -523,16 +523,16 @@
def build_count_count(t):
d = {k:t.count(k) for k in set(t)}
return d
def build_count_excpt(t):
def build_count_except(t):
d = {}
for s in t:
try:
d[s] += 1
except:
except KeyError:
d[s] = 1
return d
import collections
......@@ -545,26 +545,25 @@
d[k] += 1
return d
s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tristique at velit in varius. Cras ut ultricies orci. Fusce vel consequat ante, vitae luctus tortor. Sed condimentum faucibus enim, sit amet pulvinar ligula feugiat ac. Sed interdum id risus id rhoncus. Nullam nisi justo, ultrices eu est nec, hendrerit maximus lorem. Nam urna eros, accumsan nec magna eu, elementum semper diam. Nulla tempus, nibh id elementum dapibus, ex diam lacinia est, sit amet suscipit nulla nibh eu sapien. Aliquam orci enim, malesuada in facilisis vitae, pharetra sit amet mi. Pellentesque mi tortor, sagittis quis odio quis, fermentum faucibus ex. Aenean sagittis nisl orci. Maecenas tristique velit sed leo facilisis porttitor. "
s = s*10000
len(s)
print(f"len(s) = {len(s)}, nbkeys {len(set(s))} base, count, count_count, except, colection.counter")
print(f"len(s) = {len(s)}, nbkeys {len(set(s))} base, count, count_count, except, collections.Counter")
%timeit build_count_base(s)
%timeit build_count_set(s)
%timeit build_count_count(s)
%timeit build_count_excpt(s)
%timeit build_count_except(s)
%timeit build_count_counter(s)
%timeit build_count_defaultdict(s)
print("with split")
s2 = s.split()
print(f"len(s) = {len(s2)}, nbkeys {len(set(s2))} base, count, count_count, except, colection.counter")
print(f"len(s) = {len(s2)}, nbkeys {len(set(s2))} base, count, count_count, except, collections.Counter")
%timeit build_count_base(s2)
%timeit build_count_set(s2)
%timeit build_count_count(s2)
%timeit build_count_excpt(s2)
%timeit build_count_except(s2)
%timeit build_count_counter(s2)
%timeit build_count_defaultdict(s2)
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment