From e40c3946e8453660283b613ad734e8f508e73103 Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 19 Mar 2015 13:34:04 -0700 Subject: [PATCH 01/10] wrote radix sort, but doesn't work yet --- r_sort.py | 13 +++++++++++++ test_r_sort.py | 0 2 files changed, 13 insertions(+) create mode 100644 r_sort.py create mode 100644 test_r_sort.py diff --git a/r_sort.py b/r_sort.py new file mode 100644 index 0000000..cf806d0 --- /dev/null +++ b/r_sort.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +from math import pow +from merge_sort import timed_func + +def r_sort(sequence): + digit = 1 + while True: + bins = [[] for i in range(10)] + for x in sequence: + bins[x % 10 ** digit // 10 ** digit -1].append(x) + sequence = [number for x in bins for number in bins[x]] + digit += 1 + return sequence diff --git a/test_r_sort.py b/test_r_sort.py new file mode 100644 index 0000000..e69de29 From 7c2d97a5e4cfef5b229f44a57d9aed09fda7374b Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Thu, 19 Mar 2015 13:35:45 -0700 Subject: [PATCH 02/10] Added basic tests for r_sort. --- test_r_sort.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test_r_sort.py diff --git a/test_r_sort.py b/test_r_sort.py new file mode 100644 index 0000000..184b738 --- /dev/null +++ b/test_r_sort.py @@ -0,0 +1,19 @@ +from r_sort import r_sort + + +def test_r_one(): + a = [4, 3, 2, 5, 7, 1, 6] + a = r_sort(a) + assert a == [1, 2, 3, 4, 5, 6, 7] + + +def test_r_two(): + a = [5, 5, 5, 5, 5, 5, 5, 5, 5] + a = r_sort(a) + assert a == [5, 5, 5, 5, 5, 5, 5, 5, 5] + + +def test_r_mixed(): + a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] + a = r_sort(a) + assert a == [0, 0, 1, 2, 4, 4, 5, 6, 7, 8, 9] From 6e19d006856c5a198ce43eebf6675fde744c06ce Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 19 Mar 2015 13:37:34 -0700 Subject: [PATCH 03/10] added main block and timinf func --- r_sort.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/r_sort.py b/r_sort.py index cf806d0..cbc15ac 100644 --- a/r_sort.py +++ b/r_sort.py @@ -1,7 +1,7 @@ #!/usr/bin/env python -from math import pow from merge_sort import timed_func +@timed_func def r_sort(sequence): digit = 1 while True: @@ -10,4 +10,13 @@ def r_sort(sequence): bins[x % 10 ** digit // 10 ** digit -1].append(x) sequence = [number for x in bins for number in bins[x]] digit += 1 - return sequence + if len(sequence) == len(bins[0]): + return sequence + + +if __name__ == '__main__': + inputs = [range(500), range(1000)] + + + for inp in inputs: + r_sort(inp) \ No newline at end of file From 0abc47c7aeb4d4c59e2ebe6e70d6aca7fdad2362 Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 19 Mar 2015 14:04:40 -0700 Subject: [PATCH 04/10] fixed list comprehension that builds sequence and fixed error in index of bins --- q_sort.py | 2 +- r_sort.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) mode change 100644 => 100755 r_sort.py diff --git a/q_sort.py b/q_sort.py index b69ce31..94ebd9d 100644 --- a/q_sort.py +++ b/q_sort.py @@ -57,4 +57,4 @@ def same_maker(how_many): inputs.append(same_maker(997)) for inp in inputs: - timed_q_sort(inp) + timed_q_sort(inp) \ No newline at end of file diff --git a/r_sort.py b/r_sort.py old mode 100644 new mode 100755 index cbc15ac..ea042f9 --- a/r_sort.py +++ b/r_sort.py @@ -7,8 +7,9 @@ def r_sort(sequence): while True: bins = [[] for i in range(10)] for x in sequence: - bins[x % 10 ** digit // 10 ** digit -1].append(x) - sequence = [number for x in bins for number in bins[x]] + # the expression in bins returns the number in digit place + bins[x % 10 ** digit // 10 ** (digit -1)].append(x) + sequence = [number for x in bins for number in x] digit += 1 if len(sequence) == len(bins[0]): return sequence @@ -16,7 +17,10 @@ def r_sort(sequence): if __name__ == '__main__': inputs = [range(500), range(1000)] - + inputs.append([10 ** 1000, 1, 2, 3, 4, 5]) + inputs.append(range(6)) + inputs.append(range(1000, 0, -1)) + inputs.append([10 ** 1000, 1, 2, 3, 4, 5] + range(1000, 0 -1)) for inp in inputs: r_sort(inp) \ No newline at end of file From d57873a76d070be78a2d7d3774aac67c022f46fe Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Thu, 19 Mar 2015 14:59:21 -0700 Subject: [PATCH 05/10] Added tests for negative numbers in lists. --- test_r_sort.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test_r_sort.py b/test_r_sort.py index 184b738..3c823c8 100644 --- a/test_r_sort.py +++ b/test_r_sort.py @@ -17,3 +17,15 @@ def test_r_mixed(): a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] a = r_sort(a) assert a == [0, 0, 1, 2, 4, 4, 5, 6, 7, 8, 9] + + +def test_q_neg(): + a = [-1, -3, -6, -7, -8, -10, -50] + a = r_sort(a) + assert a == [-50, -10, -8, -7, -6, -3, -1] + + +def test_q_posneg(): + a = [1, -3, -6, 7, -8, 10, -50] + a = r_sort(a) + assert a == [-50, -8, -6, -3, 1, 7, 10] \ No newline at end of file From a3cf2b69ae37efdefc5f675e93bf01296ae37927 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Thu, 19 Mar 2015 14:59:36 -0700 Subject: [PATCH 06/10] Added handling for lists including negative numbers. --- r_sort.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/r_sort.py b/r_sort.py index ea042f9..bc478b3 100755 --- a/r_sort.py +++ b/r_sort.py @@ -8,12 +8,20 @@ def r_sort(sequence): bins = [[] for i in range(10)] for x in sequence: # the expression in bins returns the number in digit place - bins[x % 10 ** digit // 10 ** (digit -1)].append(x) + bins[abs(x) % 10 ** digit // 10 ** (digit -1)].append(x) sequence = [number for x in bins for number in x] digit += 1 if len(sequence) == len(bins[0]): - return sequence + # Handling for negative numbers + final = [] + for num in sequence: + if num < 0: + final.insert(0, num) + else: + final.append(num) + + return final if __name__ == '__main__': inputs = [range(500), range(1000)] From 1f934cc16d3210f26e5355554fc88cde8dd10d96 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Thu, 19 Mar 2015 15:07:29 -0700 Subject: [PATCH 07/10] Changed way of handling negative numbers to use more bins. --- r_sort.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/r_sort.py b/r_sort.py index bc478b3..c4c6fa8 100755 --- a/r_sort.py +++ b/r_sort.py @@ -5,23 +5,19 @@ def r_sort(sequence): digit = 1 while True: - bins = [[] for i in range(10)] + pos_bins = [[] for i in range(10)] + neg_bins = [[] for i in range(10)] for x in sequence: # the expression in bins returns the number in digit place - bins[abs(x) % 10 ** digit // 10 ** (digit -1)].append(x) - sequence = [number for x in bins for number in x] + if x < 0: + neg_bins[abs(x) % 10 ** digit // 10 ** (digit -1)].append(x) + else: + pos_bins[x % 10 ** digit // 10 ** (digit -1)].append(x) + sequence = [number for x in neg_bins[::-1] for number in x] + sequence += [number for x in pos_bins for number in x] digit += 1 - if len(sequence) == len(bins[0]): - - # Handling for negative numbers - final = [] - for num in sequence: - if num < 0: - final.insert(0, num) - else: - final.append(num) - - return final + if len(sequence) == len(pos_bins[0]) + len(neg_bins[0]): + return sequence if __name__ == '__main__': inputs = [range(500), range(1000)] From c2014e2e8f48021dd751483c92e9677a44b3d284 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Thu, 19 Mar 2015 15:10:41 -0700 Subject: [PATCH 08/10] Changed name for the r_sort handling both positive and negative numbers. --- r_sort.py | 2 +- test_r_sort.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/r_sort.py b/r_sort.py index c4c6fa8..c71bab1 100755 --- a/r_sort.py +++ b/r_sort.py @@ -2,7 +2,7 @@ from merge_sort import timed_func @timed_func -def r_sort(sequence): +def r_sort_delta(sequence): digit = 1 while True: pos_bins = [[] for i in range(10)] diff --git a/test_r_sort.py b/test_r_sort.py index 3c823c8..0a709a1 100644 --- a/test_r_sort.py +++ b/test_r_sort.py @@ -1,3 +1,4 @@ +from r_sort import r_sort_delta from r_sort import r_sort @@ -19,13 +20,19 @@ def test_r_mixed(): assert a == [0, 0, 1, 2, 4, 4, 5, 6, 7, 8, 9] -def test_q_neg(): +def test_r_delta(): + a = [4, 3, 2, 5, 7, 1, 6] + a = r_sort_d(a) + assert a == [1, 2, 3, 4, 5, 6, 7] + + +def test_r_delta_neg(): a = [-1, -3, -6, -7, -8, -10, -50] - a = r_sort(a) + a = r_sort_delta(a) assert a == [-50, -10, -8, -7, -6, -3, -1] -def test_q_posneg(): +def test_r_delta_posneg(): a = [1, -3, -6, 7, -8, 10, -50] - a = r_sort(a) + a = r_sort_delta(a) assert a == [-50, -8, -6, -3, 1, 7, 10] \ No newline at end of file From 3ac6e29b27a124eda97cc256063b7e8a591e711e Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 19 Mar 2015 15:21:58 -0700 Subject: [PATCH 09/10] added tests for alpha sort --- r_sort.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/r_sort.py b/r_sort.py index ea042f9..b507da0 100755 --- a/r_sort.py +++ b/r_sort.py @@ -15,12 +15,58 @@ def r_sort(sequence): return sequence +@timed_func +def r_sort2(sequence): + digit = 1 + while True: + bins = [[] for i in range(10)] + for x in sequence: + # the expression in bins returns the number in digit place + bins[x % 10 ** digit // 10 ** (digit -1)].append(x) + sequence = [number for x in bins for number in x] + digit += 1 + if len(sequence) == len(bins[0]): + return sequence + + +def str_index(_str, digit): + """Returns the ord of the text at the index of digit""" + try: + index = ord(_str[digit]) + return index + 1 + except IndexError: + return 0 + + +@timed_func +def r_sort_alpha(sequence): + max_digit = 0 + # find the length of longest string + for _str in sequence: + if len(_str) > max_digit: + max_digit = len(_str) + for digit in range(max_digit + 1, -1, -1): + bins = [[] for i in range(257)] + for x in sequence: + index = str_index(x, digit) + bins[index].append(x) + sequence = [number for x in bins for number in x] + return sequence + + if __name__ == '__main__': inputs = [range(500), range(1000)] inputs.append([10 ** 1000, 1, 2, 3, 4, 5]) inputs.append(range(6)) inputs.append(range(1000, 0, -1)) inputs.append([10 ** 1000, 1, 2, 3, 4, 5] + range(1000, 0 -1)) + print "cases for positive integers:" + for inp in inputs: + r_sort(inp) + inputs = [['abcdefghijkabcdefghijk' for x in range(1000)]] + inputs.append(['abcdefghijkabcdefghijkabcdefghijkabcdefghijk' for x in range(1000)]) + inputs.append([chr(x) for x in range(256)]) + print "cases for strings:" for inp in inputs: - r_sort(inp) \ No newline at end of file + r_sort_alpha(inp) From f288e96d9fcf3384e48a121c14c1926fa7787577 Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 19 Mar 2015 15:23:21 -0700 Subject: [PATCH 10/10] added alpha sort tests --- test_r_sort.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test_r_sort.py b/test_r_sort.py index 184b738..9b2f9cf 100644 --- a/test_r_sort.py +++ b/test_r_sort.py @@ -1,4 +1,4 @@ -from r_sort import r_sort +from r_sort import r_sort, r_sort_alpha def test_r_one(): @@ -17,3 +17,22 @@ def test_r_mixed(): a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] a = r_sort(a) assert a == [0, 0, 1, 2, 4, 4, 5, 6, 7, 8, 9] + + +def test_r_alpha_empty(): + """Test empty strings""" + a = ['a', 'b', 'aaa', ''] + a = r_sort_alpha(a) + assert a == ['', 'a', 'aaa', 'b'] + +def test_r_alpha_shortvlong(): + """Test that 6 character string comes before 5 char string""" + a = ['bbbaa', 'b', 'aaabbb', ''] + a = r_sort_alpha(a) + assert a == ['', 'aaabbb', 'b', 'bbbaa'] + +def test_r_alpha_zero_char(): + """Test that 6 character string comes before 5 char string""" + a = ['\x00', 'b', 'c', ''] + a = r_sort_alpha(a) + assert a == ['', '\x00', 'b', 'c'] \ No newline at end of file