Home > Mobile >  format data output inline
format data output inline

Time:02-14

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

[[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

CodePudding user response:

You can simply do :

x = part(21,2)
for i in x:
   print(str(i[0]) ':' str(i[1]))
  • Related