Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

format data output inline

I need help

they need the results to be in a column

def part(n, k):
    def _part(n, k, pre):
        if n <= 0:
            return []
        if k == 1:
            if n <= pre:
                return [[n]]
            return []
        ret = []
        for i in range(min(pre, n), 0, -1):
            ret += [[i] + sub for sub in _part(n-i, k-1, i)]
        return ret
    return _part(n, k, n)

print(part(21, 2))

the script gives results in this format

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[20, 1], [19, 2], [18, 3], [17, 4], [16, 5], [15, 6], [14, 7], [13, 8], [12, 9], [11, 10]]

I need each result to be on the same line

example

20:1
19:2
18:3
17:4
16:5
15:6
14:7
13:8
12:9

11:10

>Solution :

You can simply do :

x = part(21,2)
for i in x:
   print(str(i[0])+':'+str(i[1]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading