Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Given a positive integer 'n' and another positive integer k where k<n, I need to 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, I need to keep decrementing the given value of 'n' by 'k' until you encounter 0 or a negative number, in which case, I need to start incrementing by 'k' until you reach 'n' again. I need to implement this using recursion function. Can anyone help me with solution please? I'm not getting the logic and also I'm unable to remove the "," after the last number<pre>

What I have tried:

<pre># Reading inputs
n = int(input())
k = int(input())

# Function

def pattern(n):
    
    if n<0:
        return n
    else:
        print(n, end=", ")
        pattern(n-k)
        print(n,end=", ")
    
pattern(n)  
Posted
Updated 30-Aug-20 6:11am

My approach:
Python
def pattern(n):
    
    if n<0: # test must include 0
        return n # Where have you seen that pattern return something ?
        print(n)
    else:
        print(n)
        print(", ")
        pattern(n-k)
        print(", ")
        print(n)
 
Share this answer
 
Comments
[no name] 23-Apr-20 14:15pm    
I assume you tested it and it works. So a +5
Does this really works?

Wow yes, just tested online :thumbsup:

Only one small correction: if n<=0: like you mentioned it :-)
Patrice T 23-Apr-20 14:59pm    
Thank you.
In fact, I didn't tested it :)
Done it just by reading code .
[no name] 23-Apr-20 15:15pm    
Thank you! And I'm still not able to recognize how it goes with that example :blush:. Recursive stuff was never my thing, but because of your solution I will dive into it and try to understand this example :-)
Patrice T 23-Apr-20 15:40pm    
Recursion need a little thinking twist.
The easiest way to go is probably to attack the problem backward.
Say you have pattern(n,k)
pattern(0,2) is 0
pattern(2,2) is 2, 0, 2 => n+", "+pattern(n-k,k)+", "+n
pattern(4,2) is 4, 2, 0, 2, 4 => n+", "+pattern(n-k,k)+", "+n
pattern(6,2) is 6, 4, 2, 0, 2, 4, 6 => n+", "+pattern(n-k,k)+", "+n
Mary Louse 24-Apr-20 2:34am    
Hi thank you for the code but it need some changes.. it not giving correct result for the following:
input : n=10, k=2
actual output: 10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10
Solution output is : 10
,
8
,
6
,
4
,
2
,
0
,
-2
,
0
,
2
,
4
,
6
,
8
,
10

can you please help me with answer.
Read the instructions again, and think about what you have to do.
I would write two functions: one to go "down", and one to go "up". Then call them both, one after the other.
Pass it three values: a current value, a decrement, and a "separator". In this case, a return value isn't needed (nor is recursion, this is a load simpler as a loop).
The first time you call down, pass it n and an empty string.
in the function, print the separator, then the current value.
Now decrement the current value and check it. If it's less than zero, return.
Otherwise, call the function again with the new current value, the same decrement, and teh separator of ", ".

The "Up" function is the same, but going the other way.

Try it on paper manually first, and you'll see what I mean - then start coding.
 
Share this answer
 
# 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)
 
Share this answer
 
Hi All,

After the above logics it's perfectly works but I see None is appended with it. How to handle with that.

def pattern(n):

if n<=0: # test must include 0
print(n, end= '')
else:
print(n, end= '')
print(", ", end= '')
pattern(n-k)
print(", ", end= '')
print(n, end= '')

Output:

Solution output
10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10None
Expected output
10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10
 
Share this answer
 
Comments
[no name] 21-Jun-20 12:47pm    
A neutral 3, others will give easy a 1 for this. You did not read through all the comments?
CHill60 22-Jun-20 4:28am    
Or some of us will report it as a "Not an answer" :-)
[no name] 22-Jun-20 9:05am    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900