Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 3rd, 2012, 12:22 PM
nishakothari1 nishakothari1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 nishakothari1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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, '')

Reply With Quote
  #2  
Old November 5th, 2012, 11:16 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 97 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

work?

Reply With Quote
  #3  
Old November 5th, 2012, 12:29 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,785 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 sec
Reputation Power: 1569
Rrashkin: if you look again at the problem statement, the goal is to interlace the two strings, not just concatenate them.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

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

Reply With Quote
  #4  
Old November 5th, 2012, 12:52 PM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 97 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 15 h 24 m 4 sec
Reputation Power: 2
Quote:
Originally Posted by Schol-R-LEA
Rrashkin: if you look again at the problem statement, the goal is to interlace the two strings, not just concatenate them.

My mistake. What about
Code:
s1="xyz"
s2="pqrstu"
out=""
def lace(s1,s2,out):
    len1=len(s1)
    len2=len(s2)
    if len1>len2:
       s2=s2+(len1-len2)*' '
    else: s1=s1+(len2-len1)*' '
    out=out+s1[0]+s2[0]
    s1=s1[1:]
    s2=s2[1:]
    if len(s1)>0: out=lace(s1,s2,out).replace(' ','')
    return out
out=lace(s1,s2,out)
print out

Reply With Quote
  #5  
Old December 21st, 2012, 02:48 AM
eGrove eGrove is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 eGrove User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
Comments on this post
b49P23TIvg agrees: Works. Preserve white space using [code] tags.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Adding of the two strings with help of Python in recursive manner

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap