recursion doesnt return expected result in python [duplicate]












0
















This question already has an answer here:




  • Recursion function not working properly

    4 answers



  • Python recursive function doesn't return

    3 answers




I try to implement the Longest Common Subsequence problem that it can be found in various online programming challenges. the piece of code is



sub=""

def lcs(x, y):
sub2=sub1=''
global sub
if len(x) == 0 or len(y) == 0:
return sub
if len(x) != 0 and len(y) != 0:
sub1 = [i for i in x][-1]
sub2 = [i for i in y][-1]

if sub1!=sub2:
if len(x) > len(y):
x=x[:-1]
elif len(x) < len(y):
y=y[:-1]
else:
x=x[:-1]
y=y[:-1]
else:
sub=sub+sub1
x=x[:-1]
y=y[:-1]

lcs(x, y)

print lcs('a','a')


i expect the print statement to return 'a' but instead returns None. I think the problem is that the steps of the recursion saves the previous output and when the print is called it reaches these previous results. what am i doing wrong and how should i handle such cases?










share|improve this question













marked as duplicate by dani herrera, g.d.d.c, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    You have to return the result of the recursive call. return lcs(x, y).

    – g.d.d.c
    Nov 22 '18 at 17:03











  • Note, the second duplicate target was literally the first result I got when I copy-and-pasted your title into a google search. Please try to at least try to find if someone has asked a similar question before you post a new one. I understand sometimes its difficult to articulate if you don't know what is wrong, but again, just googling your title brought me to one...

    – juanpa.arrivillaga
    Nov 22 '18 at 17:07


















0
















This question already has an answer here:




  • Recursion function not working properly

    4 answers



  • Python recursive function doesn't return

    3 answers




I try to implement the Longest Common Subsequence problem that it can be found in various online programming challenges. the piece of code is



sub=""

def lcs(x, y):
sub2=sub1=''
global sub
if len(x) == 0 or len(y) == 0:
return sub
if len(x) != 0 and len(y) != 0:
sub1 = [i for i in x][-1]
sub2 = [i for i in y][-1]

if sub1!=sub2:
if len(x) > len(y):
x=x[:-1]
elif len(x) < len(y):
y=y[:-1]
else:
x=x[:-1]
y=y[:-1]
else:
sub=sub+sub1
x=x[:-1]
y=y[:-1]

lcs(x, y)

print lcs('a','a')


i expect the print statement to return 'a' but instead returns None. I think the problem is that the steps of the recursion saves the previous output and when the print is called it reaches these previous results. what am i doing wrong and how should i handle such cases?










share|improve this question













marked as duplicate by dani herrera, g.d.d.c, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    You have to return the result of the recursive call. return lcs(x, y).

    – g.d.d.c
    Nov 22 '18 at 17:03











  • Note, the second duplicate target was literally the first result I got when I copy-and-pasted your title into a google search. Please try to at least try to find if someone has asked a similar question before you post a new one. I understand sometimes its difficult to articulate if you don't know what is wrong, but again, just googling your title brought me to one...

    – juanpa.arrivillaga
    Nov 22 '18 at 17:07
















0












0








0









This question already has an answer here:




  • Recursion function not working properly

    4 answers



  • Python recursive function doesn't return

    3 answers




I try to implement the Longest Common Subsequence problem that it can be found in various online programming challenges. the piece of code is



sub=""

def lcs(x, y):
sub2=sub1=''
global sub
if len(x) == 0 or len(y) == 0:
return sub
if len(x) != 0 and len(y) != 0:
sub1 = [i for i in x][-1]
sub2 = [i for i in y][-1]

if sub1!=sub2:
if len(x) > len(y):
x=x[:-1]
elif len(x) < len(y):
y=y[:-1]
else:
x=x[:-1]
y=y[:-1]
else:
sub=sub+sub1
x=x[:-1]
y=y[:-1]

lcs(x, y)

print lcs('a','a')


i expect the print statement to return 'a' but instead returns None. I think the problem is that the steps of the recursion saves the previous output and when the print is called it reaches these previous results. what am i doing wrong and how should i handle such cases?










share|improve this question















This question already has an answer here:




  • Recursion function not working properly

    4 answers



  • Python recursive function doesn't return

    3 answers




I try to implement the Longest Common Subsequence problem that it can be found in various online programming challenges. the piece of code is



sub=""

def lcs(x, y):
sub2=sub1=''
global sub
if len(x) == 0 or len(y) == 0:
return sub
if len(x) != 0 and len(y) != 0:
sub1 = [i for i in x][-1]
sub2 = [i for i in y][-1]

if sub1!=sub2:
if len(x) > len(y):
x=x[:-1]
elif len(x) < len(y):
y=y[:-1]
else:
x=x[:-1]
y=y[:-1]
else:
sub=sub+sub1
x=x[:-1]
y=y[:-1]

lcs(x, y)

print lcs('a','a')


i expect the print statement to return 'a' but instead returns None. I think the problem is that the steps of the recursion saves the previous output and when the print is called it reaches these previous results. what am i doing wrong and how should i handle such cases?





This question already has an answer here:




  • Recursion function not working properly

    4 answers



  • Python recursive function doesn't return

    3 answers








python recursion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 16:59









b10n1kb10n1k

316314




316314




marked as duplicate by dani herrera, g.d.d.c, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by dani herrera, g.d.d.c, juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4





    You have to return the result of the recursive call. return lcs(x, y).

    – g.d.d.c
    Nov 22 '18 at 17:03











  • Note, the second duplicate target was literally the first result I got when I copy-and-pasted your title into a google search. Please try to at least try to find if someone has asked a similar question before you post a new one. I understand sometimes its difficult to articulate if you don't know what is wrong, but again, just googling your title brought me to one...

    – juanpa.arrivillaga
    Nov 22 '18 at 17:07
















  • 4





    You have to return the result of the recursive call. return lcs(x, y).

    – g.d.d.c
    Nov 22 '18 at 17:03











  • Note, the second duplicate target was literally the first result I got when I copy-and-pasted your title into a google search. Please try to at least try to find if someone has asked a similar question before you post a new one. I understand sometimes its difficult to articulate if you don't know what is wrong, but again, just googling your title brought me to one...

    – juanpa.arrivillaga
    Nov 22 '18 at 17:07










4




4





You have to return the result of the recursive call. return lcs(x, y).

– g.d.d.c
Nov 22 '18 at 17:03





You have to return the result of the recursive call. return lcs(x, y).

– g.d.d.c
Nov 22 '18 at 17:03













Note, the second duplicate target was literally the first result I got when I copy-and-pasted your title into a google search. Please try to at least try to find if someone has asked a similar question before you post a new one. I understand sometimes its difficult to articulate if you don't know what is wrong, but again, just googling your title brought me to one...

– juanpa.arrivillaga
Nov 22 '18 at 17:07







Note, the second duplicate target was literally the first result I got when I copy-and-pasted your title into a google search. Please try to at least try to find if someone has asked a similar question before you post a new one. I understand sometimes its difficult to articulate if you don't know what is wrong, but again, just googling your title brought me to one...

– juanpa.arrivillaga
Nov 22 '18 at 17:07














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen