1

For instance, calling the function on
n = 1 would return: [[1]]
n = 12 would return: [[1, 2]]
n = 123 would return: [[1, 2, 3], [12, 3], [1, 23]]
n = 1234 would return: [[1, 2, 3, 4], [1, 234], [1, 2, 34], [1, 23, 4], [12, 3, 4], [123, 4], [12, 34]]

I have tried a recursive approach, but I don't seem to be getting any closer to a solution. My attempted solution used modulo, floor division, and treated n as an integer, but I understand this can be done with strings as well.

def subs(n):
    if n < 10:
        return n
    else:
        lst = []
        lst.append([n])
        lst.extend([[subs(n//10), subs(n % 10)]])
    return lst
  • 3
    Hi, I think you should include your version, otherwise people could think you are searching someone who does your homework or your hackerrank task for you :-) – jottbe Nov 08 '20 at 21:08
  • 1
    It's a little inconsistent that for `n=1` you consider `[1]` a substring, but for `n=12` you don't consider `[12]` a substring. – Mark Nov 08 '20 at 21:18

0 Answers0