Skip to content

Commit

Permalink
sort collection of type(int)
Browse files Browse the repository at this point in the history
sort collection of type(int) with tests coverage
  • Loading branch information
oleksandr-maksymikhin committed Jan 3, 2025
1 parent 70d45ad commit 6817f13
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
27 changes: 24 additions & 3 deletions solutions/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""


# def bubble_sort(input_collection: list[int]) -> list[int]:
def bubble_sort(input_collection: list[int]) -> list[int]:
"""Sort collection using bubble sort algorithm.
Expand All @@ -32,6 +33,26 @@ def bubble_sort(input_collection: list[int]) -> list[int]:
>>> bubble_sort([3, 2, 1000000, 1]])
[1, 2, 3, 1000000]
"""
# add return statement to avoid lint errors
output_collection = input_collection
return output_collection

# copy collection to avoid side effect
collection = input_collection.copy()
# define collection length
collection_length = len(collection)
# first loop to traverse the collection
for current_item_index in range(collection_length):
# flag to break if the last run didn't swap any item
already_sorted = True
# second loop to compare item with adjacent one
for swap_index in range(collection_length - current_item_index - 1):
# swap items if next adjacent item is bigger
if collection[swap_index] > collection[swap_index + 1]:
(collection[swap_index], collection[swap_index + 1]) = (
collection[swap_index + 1],
collection[swap_index],
)
already_sorted = False
# break loop if the last run didn't swap any item
if already_sorted:
break
# return sorted collection
return collection
51 changes: 51 additions & 0 deletions solutions/tests/test_bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test module for bubble_sort function.
Test categories:
- Standard cases: typical lists with different lengths
- Edge cases: empty lists, single element
- Defensive tests: wrong input types, different input types
Created on 2024-01-03
Author: Oleksandr Maksymikhin
"""

import unittest

from ..bubble_sort import bubble_sort


class TestBubbleSort(unittest.TestCase):
"""Test the bubble_sort function."""

def test_empty_list(self):
"""It should return [] for input []"""
actual = bubble_sort([])
expected = []
self.assertEqual(actual, expected)

def test_one_element_list(self):
"""It should return [1] for input [1]"""
actual = bubble_sort([1])
expected = [1]
self.assertEqual(actual, expected)

def test_two_element_list(self):
"""It should return [1, 2] for input [2, 1]"""
actual = bubble_sort([2, 1])
expected = [1, 2]
self.assertEqual(actual, expected)

def test_three_elements_list(self):
"""It should return [1, 2, 3] for input [3, 2, 1]"""
actual = bubble_sort([3, 2, 1])
expected = [1, 2, 3]
self.assertEqual(actual, expected)

def test_four_elements_big_number_list(self):
"""It should return [1, 2, 3, 100500] for input [2, 3, 100500, 1]"""
actual = bubble_sort([2, 3, 100500, 1])
expected = [1, 2, 3, 100500]
self.assertEqual(actual, expected)

0 comments on commit 6817f13

Please sign in to comment.