sk rao
sk rao

Reputation: 33

I am unable to decode the logic to print the following python pattern. What changes should I do in my code?

Printing the following pattern-

Example Input: 5

Output:

15
14.10
13.09.06
12.08.05.03
11.07.04.02.01

This is the code that I wrote:

def solve(n):
    # write your code here
    a = (n * (2 + (n - 1))) // 2
    for i in range(n,0,-1):
        for j in range(n,i-1,-1):
            print(str(a).zfill(2),end = ' ')
        a -= 1
        print()

Output:

15 
14 14 
13 13 13 
12 12 12 12 
11 11 11 11 11 

I don't know where I am going wrong. It would be great if someone could help me with this!

Upvotes: 3

Views: 146

Answers (3)

revliscano
revliscano

Reputation: 2272

Another way of solving this: (Explanation in the code's comments)

def solve(n):
    # Generator that gives us the values to subtract in each row
    # (First row: [0]; Second: [0, 4]; Third: [0, 4, 7]; and so on...
    def diff_gen(length):
        diff = n - 1
        val = 0
        while length > 0:
                yield  val
                val += diff
                diff -= 1
                length -= 1

    # Initialize required variables
    row_len = 1
    result = ''
    # Find out what's the number we are starting from
    start = sum(range(n + 1))

    # We print rows until there's one with '01' in it (the final row)
    while result.find('01') == -1:
        # We're basically creating each row by subtracting the values generated by 'diff_gen' to the 'start' value
        result = ".".join(str(start - x).zfill(2) for x in diff_gen(row_len))
        # Increment the number of values for each row
        row_len += 1
        # Each new row starts at a smaller number than the previous one
        start -= 1
        print(result)

Output

>>> solve(5)
15
14.10
13.09.06
12.08.05.03
11.07.04.02.01

Upvotes: 1

Parvathirajan Natarajan
Parvathirajan Natarajan

Reputation: 1305

How about this solution? Just an optimized code with your snippet

def solve(n):
    # write your code here
    a = (n * (2 + (n - 1))) // 2
    for i in range(n,0,-1):
        for j in range(n,i-1,-1):
            if j == n:
                print(a, end = '')
            else:
                a = a - j
                print('.' + str(a).zfill(2), end = '')
        a = ((n * (2 + (n - 1))) // 2) - ((n-i)+1)
        print()

solve(5)

Output:

15
14.10
13.09.06
12.08.05.03
11.07.04.02.01

This is really a cool pattern - Thanks!

Upvotes: 2

Abhishek J
Abhishek J

Reputation: 2584

Here's a fun one liner with the same output!! Consider posting your question in Codegolf as well.

def solve2(a):
    [print('.'.join(list(map(lambda x: str(x).zfill(len(str(int((a * (a + 1)) / 2)))),
                             [int((a * (a + 1)) / 2 - (i - j * (j + 1) / 2) - a * j) for j in range(i + 1)])))) for i in
     range(a)]

I've added comments below explaining each step of a longer version of the code below.

def solve(a):
    fvar = str(int((a * (a + 1)) / 2))  # Initial Variable. Note only with this formula will the last value in the
    # array always end in 01

    arr = [fvar]
    zlen = len(fvar)  # For Zfilling
    while True:
        ps = arr[-1].split('.')  # We only need the previous value to find the next one
        ps = [int(p) - 1 for p in ps]  # First convert it into integers
        ps.append(ps[-1] - (a - len(ps)))  # Computation for the next value
        ps = [str(p).zfill(zlen) for p in ps]  # Reconvert ints to strings and zfill them
        arr.append('.'.join(ps))  # Convert array to "." separated strings

        if int(ps[-1]) - (a - len(ps)) == 1:  # Finally if we reach one. Then break
            break

    # print(arr)  # Return or print
    for a in arr:
        print(a)
#Output
15
14.10
13.09.06
12.08.05.03
11.07.04.02.01

Upvotes: 1

Related Questions