Browse Source

Make distinct.py more efficient: one loop over words.

Charles Reid 1 year ago
parent
commit
4dd57760fa
1 changed files with 4 additions and 5 deletions
  1. 4
    5
      distinct.py

+ 4
- 5
distinct.py View File

@@ -13,11 +13,10 @@ if __name__=="__main__":
13 13
 
14 14
     lengths = [[] for i in range(5+1)]
15 15
 
16
-    for i in range(1,5+1):
17
-        for word in words:
18
-            if(len(set(word))==i):
19
-                lengths[i].append(word)
20
-    
16
+    for word in words:
17
+        k = len(set(word))
18
+        lengths[k].append(word)
19
+
21 20
     for i in range(1,5+1):
22 21
         print("-"*40)
23 22
         print("Number of words with {0:d} letters: {1:d}".format(i, len(lengths[i])))