Skip to content
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

Create count_of_Smaller_Numbers_After_Self.py #221

Open
wants to merge 4 commits into
base: main
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/DATA-STRUCTURE-AND-ALGORITHM-IN-PY.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Arrays/Count of Smaller Numbers After Self/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from bisect import bisect_left
from typing import List


class Solution:
@staticmethod
def countSmaller(nums: List[int]) -> List[int]:
# get the length of the input array
n = len(nums)

# create an array of zeros with the same length as the input array
counts = [0] * n

# create an empty list to hold the sorted numbers
sorted_nums = []

# loop through the input array in reverse order
for i in range(n - 1, -1, -1):
# use the bisect_left function to find the index where the current number
# should be inserted in the sorted list
idx = bisect_left(sorted_nums, nums[i])

# the number of elements to the right of the current index is equal to the
# number of elements that are smaller than the current number
counts[i] = idx

# insert the current number into the sorted list at the correct index
sorted_nums.insert(idx, nums[i])

# return the array of counts
return counts
36 changes: 36 additions & 0 deletions Arrays/Count of Smaller Numbers After Self/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest # importing the unittest module for unit testing
from solution import Solution


class TestSolution(unittest.TestCase):

def setUp(self):
self.solution = Solution() # creating an instance of the Solution class for testing

def test_empty_input(self):
# testing with empty input
nums = []
expected_counts = []
self.assertEqual(self.solution.countSmaller(nums), expected_counts)

def test_single_input(self):
# testing with a single input
nums = [5]
expected_counts = [0]
self.assertEqual(self.solution.countSmaller(nums), expected_counts)

def test_multiple_input(self):
# testing with multiple inputs
nums = [5, 2, 6, 1]
expected_counts = [1, 0, 1, 0]
self.assertEqual(self.solution.countSmaller(nums), expected_counts)

def test_duplicate_input(self):
# testing with duplicate inputs
nums = [1, 2, 2, 3]
expected_counts = [0, 0, 0, 2]
self.assertEqual(self.solution.countSmaller(nums), expected_counts)


if __name__ == '__main__':
unittest.main() # running the test suite using unittest module