From f5b6330c59731df8c814d093ff679cc058febfde Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Mon, 8 Jun 2020 08:41:14 +0200 Subject: [PATCH 1/7] Added doctests to bucket sort --- sorts/bucket_sort.py | 71 +++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index 217ee5893c4b..d9427b72bc38 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -1,33 +1,51 @@ #!/usr/bin/env python -"""Illustrate how to implement bucket sort algorithm.""" - -# Author: OMKAR PATHAK -# This program will illustrate how to implement bucket sort algorithm - -# Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works -# by distributing the elements of an array into a number of buckets. -# Each bucket is then sorted individually, either using a different sorting -# algorithm, or by recursively applying the bucket sorting algorithm. It is a -# distribution sort, and is a cousin of radix sort in the most to least -# significant digit flavour. -# Bucket sort is a generalization of pigeonhole sort. Bucket sort can be -# implemented with comparisons and therefore can also be considered a -# comparison sort algorithm. The computational complexity estimates involve the -# number of buckets. - -# Time Complexity of Solution: -# Worst case scenario occurs when all the elements are placed in a single bucket. The overall performance -# would then be dominated by the algorithm used to sort each bucket. In this case, O(n log n), because of TimSort -# -# Average Case O(n + (n^2)/k + k), where k is the number of buckets -# -# If k = O(n), time complexity is O(n) +""" +Illustrate how to implement bucket sort algorithm. +Author: OMKAR PATHAK +This program will illustrate how to implement bucket sort algorithm + +Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works +by distributing the elements of an array into a number of buckets. +Each bucket is then sorted individually, either using a different sorting +algorithm, or by recursively applying the bucket sorting algorithm. It is a +distribution sort, and is a cousin of radix sort in the most to least +significant digit flavour. +Bucket sort is a generalization of pigeonhole sort. Bucket sort can be +implemented with comparisons and therefore can also be considered a +comparison sort algorithm. The computational complexity estimates involve the +number of buckets. + +Time Complexity of Solution: +Worst case scenario occurs when all the elements are placed in a single bucket. The overall performance +would then be dominated by the algorithm used to sort each bucket. In this case, O(n log n), because of TimSort + +Average Case O(n + (n^2)/k + k), where k is the number of buckets + +If k = O(n), time complexity is O(n) + +Source: https://en.wikipedia.org/wiki/Bucket_sort +""" DEFAULT_BUCKET_SIZE = 5 -def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE): +def bucket_sort(my_list: list, bucket_size=DEFAULT_BUCKET_SIZE) -> list: + """ + >>> bucket_sort([-1, 2, -5, 0]) + [-5, -1, 0, 2] + + >>> bucket_sort([9, 8, 7, 6, -12]) + [-12, 6, 7, 8, 9] + + >>> bucket_sort([.4, 1.2, .1, .2, -.9]) + [-0.9, 0.1, 0.2, 0.4, 1.2] + + >>> bucket_sort([]) + Traceback (most recent call last): + ... + Exception: Please add some elements in the array. + """ if len(my_list) == 0: raise Exception("Please add some elements in the array.") @@ -44,6 +62,5 @@ def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE): if __name__ == "__main__": - user_input = input("Enter numbers separated by a comma:").strip() - unsorted = [float(n) for n in user_input.split(",") if len(user_input) > 0] - print(bucket_sort(unsorted)) + assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] + assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15] From fbec3570ea67b6da3ba362648dda7580de019e5f Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Mon, 8 Jun 2020 08:43:58 +0200 Subject: [PATCH 2/7] Missing typehint --- sorts/bucket_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index d9427b72bc38..3003f2efc86a 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -30,7 +30,7 @@ DEFAULT_BUCKET_SIZE = 5 -def bucket_sort(my_list: list, bucket_size=DEFAULT_BUCKET_SIZE) -> list: +def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list: """ >>> bucket_sort([-1, 2, -5, 0]) [-5, -1, 0, 2] From ed891841cb3a64ecd9555931349fb19b424753df Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Jun 2020 08:18:24 +0200 Subject: [PATCH 3/7] Wrap long lines --- sorts/bucket_sort.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index 3003f2efc86a..8e4e766d5f14 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Illustrate how to implement bucket sort algorithm. @@ -18,8 +18,9 @@ number of buckets. Time Complexity of Solution: -Worst case scenario occurs when all the elements are placed in a single bucket. The overall performance -would then be dominated by the algorithm used to sort each bucket. In this case, O(n log n), because of TimSort +Worst case scenario occurs when all the elements are placed in a single bucket. +The overall performance would then be dominated by the algorithm used to sort each +bucket. In this case, O(n log n), because of TimSort Average Case O(n + (n^2)/k + k), where k is the number of buckets @@ -32,14 +33,17 @@ def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list: """ - >>> bucket_sort([-1, 2, -5, 0]) - [-5, -1, 0, 2] + >>> data = [-1, 2, -5, 0] + >>> bucket_sort(data) == sorted(data) + True - >>> bucket_sort([9, 8, 7, 6, -12]) - [-12, 6, 7, 8, 9] + >>> data = [9, 8, 7, 6, -12]) + >>> bucket_sort(data) == sorted(data) + True - >>> bucket_sort([.4, 1.2, .1, .2, -.9]) - [-0.9, 0.1, 0.2, 0.4, 1.2] + >>> data = [.4, 1.2, .1, .2, -.9]) + >>> bucket_sort(data) == sorted(data) + True >>> bucket_sort([]) Traceback (most recent call last): @@ -57,7 +61,7 @@ def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list: buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i]) return sorted( - [buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))] + buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i])) ) From f21395db27af358ddae0dcaf1c32bddbd19f1266 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 16 Jun 2020 06:18:51 +0000 Subject: [PATCH 4/7] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 78afe07ec21f..cc73b18db50a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -72,6 +72,9 @@ * [Huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py) * [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py) +## Computer Vision + * [Harriscorner](https://github.com/TheAlgorithms/Python/blob/master/computer_vision/harriscorner.py) + ## Conversions * [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py) * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py) @@ -350,6 +353,7 @@ * [Monte Carlo](https://github.com/TheAlgorithms/Python/blob/master/maths/monte_carlo.py) * [Monte Carlo Dice](https://github.com/TheAlgorithms/Python/blob/master/maths/monte_carlo_dice.py) * [Newton Raphson](https://github.com/TheAlgorithms/Python/blob/master/maths/newton_raphson.py) + * [Number Of Digits](https://github.com/TheAlgorithms/Python/blob/master/maths/number_of_digits.py) * [Numerical Integration](https://github.com/TheAlgorithms/Python/blob/master/maths/numerical_integration.py) * [Perfect Square](https://github.com/TheAlgorithms/Python/blob/master/maths/perfect_square.py) * [Pi Monte Carlo Estimation](https://github.com/TheAlgorithms/Python/blob/master/maths/pi_monte_carlo_estimation.py) @@ -375,6 +379,7 @@ * [Softmax](https://github.com/TheAlgorithms/Python/blob/master/maths/softmax.py) * [Square Root](https://github.com/TheAlgorithms/Python/blob/master/maths/square_root.py) * [Sum Of Arithmetic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_arithmetic_series.py) + * [Sum Of Digits](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_digits.py) * [Test Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/test_prime_check.py) * [Trapezoidal Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/trapezoidal_rule.py) * [Volume](https://github.com/TheAlgorithms/Python/blob/master/maths/volume.py) From d65465aef26e8d83c3ddafb12b63b6acf2664d87 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Jun 2020 08:29:43 +0200 Subject: [PATCH 5/7] Update bucket_sort.py --- sorts/bucket_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index 8e4e766d5f14..bd788ecdb9b6 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -37,11 +37,11 @@ def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list: >>> bucket_sort(data) == sorted(data) True - >>> data = [9, 8, 7, 6, -12]) + >>> data = [9, 8, 7, 6, -12] >>> bucket_sort(data) == sorted(data) True - >>> data = [.4, 1.2, .1, .2, -.9]) + >>> data = [.4, 1.2, .1, .2, -.9] >>> bucket_sort(data) == sorted(data) True From 62d38851391e382cc786dbc287f27da35a70b8db Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 17 Jun 2020 06:07:01 +0000 Subject: [PATCH 6/7] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 54de20c0e13f..940b039f99d1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -147,6 +147,7 @@ * [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py) ## Digital Image Processing + * [Change Brightness](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_brightness.py) * [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py) * [Convert To Negative](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/convert_to_negative.py) * Dithering @@ -268,6 +269,7 @@ ## Hashes * [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py) * [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py) + * [Djb2](https://github.com/TheAlgorithms/Python/blob/master/hashes/djb2.py) * [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py) * [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py) * [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py) From c2d04bdd858b69ac513c8c2a3553015d16eb46e7 Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Wed, 17 Jun 2020 08:25:26 +0200 Subject: [PATCH 7/7] Update bucket_sort.py --- sorts/bucket_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index 9a462ce3323e..178b4f664480 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 """ Illustrate how to implement bucket sort algorithm.