jeudi 23 avril 2020

trying recursion to print a pattern based on arithematic progression in Python

I am learning recursive function and have been trying to print a pattern based on arithmetic progression. like:
Input 1: n = 12, k = 5
Output 2: 12, 7, 2, -3, 2, 7, 12
Input 2: n = 10, k =2
Output 2: 10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10.
I am getting a , and None at the end. like :- 12, 7, 2, -3, 2, 7, 12, None
Here is my code.

# Reading the inputs
n = int(input())
k = int(input())

# Function
def pattern(n,k):

    if (n == 0 or n < 0): 
        print(n, end = ", ")


    else:
        # First print decreasing order 
        print(n, end = ", ") 
        pattern(n - k, k) 

        # Then print increasing order 
        print(n, end = ", ")  


print (pattern(n,k))

Aucun commentaire:

Enregistrer un commentaire