Adding of the two strings with help of Python in recursive manner
Discuss Adding of the two strings with help of Python in recursive manner in the Python Programming forum on Dev Shed. Adding of the two strings with help of Python in recursive manner Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Posts: 3
Time spent in forums: 46 m 8 sec
Reputation Power: 0
Adding of the two strings with help of Python in recursive manner
Hello Friends,
Here I write a piece of code for adding two strings. For e.g. if first string is "xyz" and second is "pqrstu" then the final out should be "xpyqzrstu"... But the condition is to use only if and else condition.. We cant use any loop mechanism or any thing else..
def helpLaceStrings(s1, s2, out):
if s1 == '':
a single line code here
if s2 == '':
a single line code here
else:
a single line code here
return helpLaceStrings(s1, s2, '')
Posts: 97
Time spent in forums: 1 Day 15 h 24 m 4 sec
Reputation Power: 2
Quote:
Originally Posted by nishakothari1
Hello Friends,
Here I write a piece of code for adding two strings. For e.g. if first string is "xyz" and second is "pqrstu" then the final out should be "xpyqzrstu"... But the condition is to use only if and else condition.. We cant use any loop mechanism or any thing else..
def helpLaceStrings(s1, s2, out):
if s1 == '':
a single line code here
if s2 == '':
a single line code here
else:
a single line code here
return helpLaceStrings(s1, s2, '')
Why do you need recursion? What are you doing over and over and what condition are you doing it until? That is, why won't:
Code:
def helpLaceStrings(s1, s2, out):
if s1 == '':
out = s2
if s2 == '':
out = s1
else:
out = s1+s2
FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
Posts: 7
Time spent in forums: 4 h 58 m 56 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Re: Add two string with python in recrusive
def str_cat(str1, str2, index = 0, out = ''):
if index < len(str1):
try: #if len(str1) than str2
out = out + str1[index] + str2[index]
index = index + 1
return str_cat(str1, str2, index, out)
except:
out = out + str1[index:]
return out
else: #if len(str2) than str1
if index <= len(str2):
return out+str2[index:]
s1= 'abc'
s2 = 'xyz456'
res = str_cat(s1,s2)
print res