Skip to content

Commit

Permalink
Sums of Parts
Browse files Browse the repository at this point in the history
  • Loading branch information
ikostan committed Nov 24, 2024
1 parent b69f922 commit 0525e8a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
44 changes: 44 additions & 0 deletions kyu_6/sums_of_parts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Sums of Parts

Let us consider this example (array written in general format):

```bash
ls = [0, 1, 3, 6, 10]
```

It's following parts:

```bash
ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]
ls = [10]
ls = []
```

The corresponding sums are (put together in a list):

```bash
[20, 20, 19, 16, 10, 0]
```

The function parts_sums (or its variants in other languages) will take as
parameter a list ls and return a list of the sums of its parts as defined above.

Other Examples:

```bash
ls = [1, 2, 3, 4, 5, 6]
parts_sums(ls) -> [21, 20, 18, 15, 11, 6, 0]

ls = [744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]
parts_sums(ls) -> [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]
```

## Notes

- Take a look at performance: some lists have thousands of elements.
- Please ask before translating.

[Source](https://www.codewars.com/kata/5ce399e0047a45001c853c2b/python)
Empty file added kyu_6/sums_of_parts/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions kyu_6/sums_of_parts/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Test for -> Sums of Parts
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""


def parts_sums(ls: list) -> list:

Check notice on line 8 in kyu_6/sums_of_parts/solution.py

View check run for this annotation

codefactor.io / CodeFactor

kyu_6/sums_of_parts/solution.py#L8

Unused argument 'ls' (unused-argument)
"""
The function parts_sums will take as parameter a list ls
and return a list of the sums of its parts.
:param ls:
:return:
"""
pass

Check notice on line 15 in kyu_6/sums_of_parts/solution.py

View check run for this annotation

codefactor.io / CodeFactor

kyu_6/sums_of_parts/solution.py#L15

Unnecessary pass statement (unnecessary-pass)
73 changes: 73 additions & 0 deletions kyu_6/sums_of_parts/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Test for -> Sums of Parts
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""

# Fundamentals Performance Algorithms

import unittest
import allure
from utils.log_func import print_log
from kyu_6.sums_of_parts.solution import parts_sums


# pylint: disable-msg=R0801
@allure.epic('6 kyu')
@allure.parent_suite('Novice')
@allure.suite("Fundamentals")
@allure.sub_suite("Unit Tests")
@allure.feature("Algorithms")
@allure.story('Sums of Parts')
@allure.tag('FUNDAMENTALS',
'PERFORMANCE',
'ALGORITHMS')
@allure.link(
url='https://www.codewars.com/kata/5ce399e0047a45001c853c2b',
name='Source/Kata')
# pylint: enable-msg=R0801
class SJFTestCase(unittest.TestCase):
"""
Testing 'parts_sums' function
"""

# pylint: disable-msg=R0801
def test_sjf(self):
"""
Testing 'parts_sums' function with various test data
:return:
"""
allure.dynamic.title("Testing 'parts_sums' function")
allure.dynamic.severity(allure.severity_level.NORMAL)
allure.dynamic.description_html(
'<h3>Codewars badge:</h3>'
'<img src="https://www.codewars.com/users/myFirstCode'
'/badges/large">'
'<h3>Test Description:</h3>'
"<p>Test a function that take as parameter a list ls and "
"return a list of the sums of its parts as defined below:<br>"
"ls = [1, 2, 3, 4, 5, 6]<br>"
"parts_sums(ls) -> [21, 20, 18, 15, 11, 6, 0]"
"</p>")
# pylint: enable-msg=R0801
test_data: tuple = (
([1, 2, 3, 4, 5, 6], [21, 20, 18, 15, 11, 6, 0]),
([0, 1, 3, 6, 10], [20, 20, 19, 16, 10, 0]),
([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358],
[10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414,
9291270, 2581057, 2580168, 2579358, 0]))

for ls, expected in test_data:
actual_result = parts_sums(ls)
# pylint: disable-msg=R0801
with allure.step(f"Enter a list ls ({ls}) and verify the "
f"expected output ({expected}) vs "
f"actual result ({actual_result})"):

print_log(ls=ls,
expected=expected,
result=actual_result)

self.assertEqual(expected,
actual_result)
# pylint: enable-msg=R0801

0 comments on commit 0525e8a

Please sign in to comment.