Recursive Pattern Using Python

0 votes

Recursive Pattern

Description Given a positive integer 'n' and another positive integer 'k' (<n),  print the following pattern using recursion.

Example:
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

Basically, you need to keep decrementing the given value of 'n' by 'k' until you encounter 0 or a negative number, in which case, you need to start incrementing by 'k' until you reach 'n' again.

Format:
Input: The first line contains the positive integer 'n'. The second line contains the positive integer 'k'.
Output: The required pattern as comma-separated-values as shown in the examples above.

Jun 22, 2020 in Python by rocking
• 160 points
2,416 views

2 answers to this question.

+1 vote
n = int(input("please enter your no:"))
k = int(input("please enter ypur no:"))
m = []
for i in range(0,n*2+k,k):
    if n-i >= 0:
        a = n-i
        m.append(a)
    elif n-i < 0:
        a = n-i
        a = a + a*-2
        m.append(a)
for i in m:
    print(i,end=",")
answered Jun 28, 2020 by Shaurya Thapliyal
• 340 points
0 votes
# Reading the input
n = int(input())
k = int(input())

# Define the function
def pattern(n, m, k):
    
    # Base case. If m <= 0, print m and return from the function
    if m <= 0:
        print(m, end = ", ")
        return
    
    # Print m, call the recursive function with (n, m-k, k), and then print m again
    print(m, end = ", ")
    pattern(n, m-k, k)
    # This conditional statement has been used so that you do not print a comma
    # after the last element.
    if(n != m):
        print(m, end = ", ")
    else:
        print(m)

# Declaring another variable m equal to n
m = n

# Calling the function with 3 arguments - n, m, and k. The extra m will be used
# for recursive calls. The n will be preserved because if you check the output
# format, you will see that after the last element, you shouldn't print a comma.
# The value of 'n' in the function will be used primarily for that.
pattern(n, m, k)
answered Aug 31, 2020 by Shweta Patil

Related Questions In Python

0 votes
1 answer

How to create the following pattern using Python?

Hi, @Roshni, You can use two for-loops for ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,950 points
145 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,780 points
3,527 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,780 points
9,635 views
+2 votes
2 answers

How can I plot a k-dsitance graph using python?

Hi there, instead of sklearn you could ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,780 points
3,428 views
0 votes
2 answers

how to print the current time using python?

print(datetime.datetime.today()) READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
236 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,422 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,823 views
0 votes
1 answer