Skip to content
This repository
Browse code

Cleanup, pythonic code

Strip trailing space on line ends, pep8 cleanup

Improve bubblesort
- Inline swap
- hoist constant out of loop
- avoid unused variable 'x'

Conflicts:
	algorithms/shuffling/knuth.py
	algorithms/sorting/bogo_sort.py
  • Loading branch information...
commit fa705db0f510054ff7b50cff4910bb0254259c3b 1 parent fc4820d
authored
11  algorithms/searching/bmh_search.py
@@ -25,16 +25,13 @@
25 25
 
26 26
 
27 27
 def search(text, pattern):
28  
-    m = len(pattern)
29  
-    n = len(text)
  28
+    m, n = len(pattern), len(text)
30 29
     offsets = []
31 30
     if m > n:
32 31
         return offsets
33  
-    bmbc = []
34  
-    for k in range(256):
35  
-        bmbc.append(m)
36  
-    for k in range(m - 1):
37  
-        bmbc[ord(pattern[k])] = m - k - 1
  32
+    bmbc = [m] * 256
  33
+    for k, p in enumerate(pattern[:-1]):
  34
+        bmbc[ord(p)] = m - k - 1
38 35
     bmbc = tuple(bmbc)
39 36
     k = m - 1
40 37
     while k < n:
14  algorithms/shuffling/knuth.py
... ...
@@ -1,22 +1,22 @@
1 1
 """
2 2
     knuth.py
3 3
     Implementation of the Fisher-Yates/Knuth shuffle
4  
-    
  4
+
5 5
     Pre: Takes any list, unshuffled
6 6
     Post: Returns list shuffled randomly
7  
-    
  7
+
8 8
     Time Complexity: n
9 9
     Space Complexity: n
10  
-    
  10
+
11 11
     Pseudocode: http://en.wikipedia.org/wiki/Fisher%E1%80%93Yates_shuffle
12 12
 
13 13
 """
14  
-import random
  14
+from random import seed, randint
15 15
 
16 16
 
17 17
 def shuffle(li):
18  
-    random.seed()
19  
-    for i in xrange(len(li) - 1, 0, -1):
20  
-        j = random.randint(0, i)
  18
+    seed()
  19
+    for i in reversed(range(len(li))):
  20
+        j = randint(0, i)
21 21
         li[i], li[j] = li[j], li[i]
22 22
     return li
9  algorithms/sorting/bogo_sort.py
... ...
@@ -1,9 +1,9 @@
1 1
 """
2 2
     bogo_sort.py
3  
-    
  3
+
4 4
     This module implements bogo sort on an unsorted list and
5 5
     returns the list in sorted order.
6  
-    
  6
+
7 7
     Bogo Sort Overview:
8 8
     -------------------
9 9
     If list is not in order, picks two elements at random and swaps them.
@@ -41,7 +41,4 @@ def sort(seq):
41 41
 
42 42
 
43 43
 def is_sorted(seq):
44  
-    for i in xrange(1, len(seq)):
45  
-        if seq[i - 1] > seq[i]:
46  
-            return False
47  
-    return True
  44
+    return all(seq[i - 1] <= seq[i] for i in xrange(1, len(seq)))
9  algorithms/sorting/bubble_sort.py
@@ -25,10 +25,9 @@
25 25
 
26 26
 
27 27
 def sort(seq):
28  
-    for x in seq:
29  
-        for n in range(1, len(seq)):
  28
+    L = len(seq)
  29
+    for _ in range(L):
  30
+        for n in range(1, L):
30 31
             if seq[n] < seq[n - 1]:
31  
-                temp = seq[n]
32  
-                seq[n] = seq[n - 1]
33  
-                seq[n - 1] = temp
  32
+                seq[n - 1], seq[n] = seq[n], seq[n - 1]
34 33
     return seq

0 notes on commit fa705db

Please sign in to comment.
Something went wrong with that request. Please try again.