Skip to content

updated few codes in classic algorithms and added unittests #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions classical_algorithms/python/Bubble Sort.py

This file was deleted.

22 changes: 22 additions & 0 deletions classical_algorithms/python/BubbleSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def bubblesort(array):
n = len(array)

for i in range(0, n - 1):
for j in range(0, n - i - 1):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]

return array


if __name__ == '__main__':
m = int(input('Enter the number of elements in the array '))

mylist = []

for i in range(0, m):
num = int(input('Enter element: '))
mylist.append(num)

output = bubblesort(mylist)
print(output)
27 changes: 0 additions & 27 deletions classical_algorithms/python/Insertion Sort.py

This file was deleted.

21 changes: 21 additions & 0 deletions classical_algorithms/python/InsertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def insertion_sort(array):
for i in range(1, len(array)):
j = i - 1
key = array[i]

while j >= 0 and array[j] > key:
array[j + 1] = array[j]
j = j - 1

array[j + 1] = key

return array


if "__name__" == "__main__":
m = int(input('Enter the number of elements in the array '))
mylist = []
for i in range(0, m):
num = int(input('Enter element: '))
mylist.append(num)
print(insertion_sort(mylist))
28 changes: 0 additions & 28 deletions classical_algorithms/python/Sieve of Eratosthenes.py

This file was deleted.

23 changes: 23 additions & 0 deletions classical_algorithms/python/sieveoferatosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import math


def sieve_of_eratosthenes(n):
if n < 2:
return []
primes = [1] * (n + 1)
primes[0] = primes[1] = 0

for i in range(2, int(math.sqrt(n) + 1)):
if primes[i] == 1:
for j in range(i * i, n + 1, i):
primes[j] = 0

return [i for i in range(n + 1) if primes[i] == 1]


if "__name__" == "__main__":
n = int(input())
primes = sieve_of_eratosthenes(n)
print(f"Prime numbers up to {n}:")
for prime in primes:
print(prime, end=" ")
23 changes: 23 additions & 0 deletions classical_algorithms/python/tests/test_bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from classical_algorithms.python.BubbleSort import bubblesort


class TestBubbleSort(unittest.TestCase):
def test_bubble_sort(self):
# normal test 1
self.assertEqual(bubblesort([64, 34, 25, 12, 22, 11, 90]), [11, 12, 22, 25, 34, 64, 90])
# normal test 2
self.assertEqual(bubblesort([5, 2, 9, 1, 5, 6]), [1, 2, 5, 5, 6, 9])
# empty list
self.assertEqual(bubblesort([]), [])
# single-element list
self.assertEqual(bubblesort([1]), [1])
# reverse-ordered list
self.assertEqual(bubblesort([9, 8, 7, 6, 5, 4, 3, 2, 1]),
[1, 2, 3, 4, 5, 6, 7, 8, 9])
# negative list
self.assertEqual(bubblesort([-1, -2, -3, -4]), [-4, -3, -2, -1])

if __name__ == '__main__':
unittest.main()
25 changes: 25 additions & 0 deletions classical_algorithms/python/tests/test_insertionsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from classical_algorithms.python.InsertionSort import insertion_sort


class TestInsertionSort(unittest.TestCase):

def test_insertion_sort(self):
# test case 1
self.assertEqual(insertion_sort([64, 34, 25, 12, 22, 11, 90]), [11, 12, 22, 25, 34, 64, 90])
# test case 2
self.assertEqual(insertion_sort([5, 2, 9, 1, 5, 6]), [1, 2, 5, 5, 6, 9])
# empty list
self.assertEqual(insertion_sort([]), [])
# single-element list
self.assertEqual(insertion_sort([1]), [1])
# reverse-ordered list
self.assertEqual(insertion_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]),
[1, 2, 3, 4, 5, 6, 7, 8, 9])
# negative list
self.assertEqual(insertion_sort([-1, -2, -3, -4]), [-4, -3, -2, -1])


if __name__ == '__main__':
unittest.main()
21 changes: 21 additions & 0 deletions classical_algorithms/python/tests/test_sieveoferatosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest

from classical_algorithms.python.sieveoferatosthenes import sieve_of_eratosthenes


class TestSieveOfEratosthenes(unittest.TestCase):
def test_sieve_of_eratosthenes(self):
#test case1
self.assertEqual(sieve_of_eratosthenes(10), [2, 3, 5, 7])
# test case2
self.assertEqual(sieve_of_eratosthenes(20), [2, 3, 5, 7, 11, 13, 17, 19])
# for 1
self.assertEqual(sieve_of_eratosthenes(1), [])
# for 2
self.assertEqual(sieve_of_eratosthenes(2), [2])
# for negative number
self.assertEqual(sieve_of_eratosthenes(-1), [])


if __name__ == '__main__':
unittest.main()