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
7,401 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,770 points
702 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,720 points
4,951 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,720 points
13,592 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,720 points
5,075 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
1,175 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 4,467 views
0 votes
1 answer
+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,790 points
4,128 views
0 votes
1 answer

please explain me what does i-1 in python user_define function

i - 1 is the index of ...READ MORE

answered Jun 28, 2020 in Python by Shaurya Thapliyal
• 340 points
3,185 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP