aboutsummaryrefslogtreecommitdiff
path: root/challenge-102/roger-bell-west/python/ch-2.py
blob: 7812a33fe8f4df486843c332a51b0d15cf681565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/python3

def hcs(n):
    s=list()
    t=list()
    while 1:
        s=list()
        l=0
        if len(t)>0:
            s=t.pop()
            l=sum((0 if i==1 else len(str(i)))+1 for i in s)
        if l==n:
            break
        if l > n:
            continue
        c=l
        while 1:
            tt=(0 if c==1 else len(str(c)))+l+1
            if c==tt:
                k=s.copy()
                k.append(c)
                t.append(k)
            if c > tt:
                break
            c+=1
    return ''.join((("" if i==1 else str(i)) + "#") for i in s)


import unittest

class TestHcs(unittest.TestCase):

        def test_ex1(self):
            self.assertEqual(hcs(1),'#','example 1')

        def test_ex2(self):
            self.assertEqual(hcs(2),'2#','example 2')

        def test_ex3(self):
            self.assertEqual(hcs(3),'#3#','example 3')

        def test_ex4(self):
            self.assertEqual(hcs(10),'#3#5#7#10#','example 4')

        def test_ex5(self):
            self.assertEqual(hcs(14),'2#4#6#8#11#14#','example 5')

unittest.main()