Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* [Test Intersection Two](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/intersection/test_intersection_two.py)
* Majority Element
* [Test Majority Element](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/majority_element/test_majority_element.py)
* Non Constructible Change
* [Test Non Constructible Change](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/non_constructible_change/test_non_constructible_change.py)
Comment thread
BrianLusina marked this conversation as resolved.
* Optimal Task Assignment
* [Test Optimal Task Assignment](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/optimal_task_assignment/test_optimal_task_assignment.py)
* Remove Duplicates
Expand Down
20 changes: 20 additions & 0 deletions algorithms/arrays/non_constructible_change/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Non-Constructible Change

Given an array of positive integers representing the values of coins in your
possession, write a function that returns the minimum amount of change (the
minimum sum of money) that you cannot create. The given coins can have
any positive integer value and aren't necessarily unique (i.e., you can have
multiple coins of the same value).


Sample Input:

```plain
coins = [5,7,1,1,2,3,22]
```

Sample output:

```plain
20
```
17 changes: 17 additions & 0 deletions algorithms/arrays/non_constructible_change/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List


def non_constructible_change(coins: List[int]) -> int:
"""

"""
coins.sort()

current_change_created = 0

for coin in coins:
if coin > current_change_created + 1:
return current_change_created + 1
current_change_created += coin
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

return current_change_created + 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import unittest
from . import non_constructible_change
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use absolute import so tests work via discovery and when run as a script.

The relative import breaks if this file is executed directly (your main block implies that use). Prefer an absolute import.

-from . import non_constructible_change
+from algorithms.arrays.non_constructible_change import non_constructible_change
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from . import non_constructible_change
from algorithms.arrays.non_constructible_change import non_constructible_change
🤖 Prompt for AI Agents
In algorithms/arrays/non_constructible_change/test_non_constructible_change.py
around line 2, the test uses a relative import which fails when the file is run
directly; change the import to an absolute import using the full package path
(algorithms.arrays.non_constructible_change) so test discovery and direct
execution both work; update the import statement accordingly and run the test
file directly to verify it imports correctly.



class NonConstructibleChangeTestCase(unittest.TestCase):
def test_1(self):
"""should return 20 for input of [5, 7, 1, 1, 2, 3, 22]"""
coins = [5, 7, 1, 1, 2, 3, 22]
expected = 20
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_2(self):
"""should return 6 for input of [1, 1, 1, 1, 1]"""
coins = [1, 1, 1, 1, 1]
expected = 6
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_3(self):
"""should return 55 for input of [1, 5, 1, 1, 1, 10, 15, 20, 100]"""
coins = [1, 5, 1, 1, 1, 10, 15, 20, 100]
expected = 55
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_4(self):
"""should return 3 for input of [6, 4, 5, 1, 1, 8, 9]"""
coins = [6, 4, 5, 1, 1, 8, 9]
expected = 3
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_5(self):
"""should return 1 for input of []"""
coins = []
expected = 1
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_6(self):
"""should return 1 for input of [87]"""
coins = [87]
expected = 1
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_7(self):
"""should return 32 for input of [5, 6, 1, 1, 2, 3, 4, 9]"""
coins = [5, 6, 1, 1, 2, 3, 4, 9]
expected = 32
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_8(self):
"""should return 19 for input of [5, 6, 1, 1, 2, 3, 43]"""
coins = [5, 6, 1, 1, 2, 3, 43]
expected = 19
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_9(self):
"""should return 3 for input of [1, 1]"""
coins = [1, 1]
expected = 3
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_10(self):
"""should return 1 for input of [2]"""
coins = [2]
expected = 1
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_11(self):
"""should return 2 for input of [1]"""
coins = [1]
expected = 2
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_12(self):
"""should return 87 for input of [109, 2000, 8765, 19, 18, 17, 16, 8, 1, 1, 2, 4]"""
coins = [109, 2000, 8765, 19, 18, 17, 16, 8, 1, 1, 2, 4]
expected = 87
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)

def test_13(self):
"""should return 29 for input of [1, 2, 3, 4, 5, 6, 7]"""
coins = [1, 2, 3, 4, 5, 6, 7]
expected = 29
actual = non_constructible_change(coins)
self.assertEqual(actual, expected)


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