Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the out string, e.g. "<<word>>".

Python
make_out_word('<<>>', 'Yay') → '<<Yay>>'
make_out_word('<<>>', 'WooHoo') → '<<WooHoo>>'
make_out_word('[[]]', 'word') → '[[word]]'

def make_out_word(out, word):
	    x=out[0:2]
	    y=out[2:]
	    var=(x,word,y)
	    print '%s%s%s'%var
Posted
Updated 22-Jun-15 9:53am
v2
Comments
ZurdoDev 22-Jun-15 15:46pm    
It is incredibly rude to post your homework without even putting it into your own words or without even asking a question. Booo.
Sergey Alexandrovich Kryukov 22-Jun-15 17:25pm    
I paid attention for that after reading your comment (and basically agree with your, reported the post).
I guess, the inquirers "own words" are there in the title of the question.

At the same time, the issue the inquirer faces and does not even properly recognized is so characteristic that I decided to post an answer and even consider the answer pretty interesting.

—SA
Sergey Alexandrovich Kryukov 22-Jun-15 16:50pm    
You probably mean "alternative method", not "alternate". Why would you need an alternative?
—SA

I don't think the implementation needs alternative approach, but I think the formulation of the problem and the method profile strongly requires improvements.

The big problem of your formulation is that is implies strongly ad-hoc behavior, something opposite to being abstract and universal. In your implementation, you hard-code immediate constants 0 and 2, which is always bad (for maintenance and everything else), but you have to blame the problem formulation a lot more then the implementation. Even with perfect implementation, when the opening "brackets" has any number of characters other then 2, your method fails. Moreover, the signature of the method is not self-commenting. If give the user no clue on the requirements for the first parameter; and the requirements are just very unnatural. This is not just bad, this is very bad.

At the same time, more neat solution is obvious. Use different signature:
Python
def makeOutWord(bra, word, ket):
    # concatenate bra, word and ket,
    # in this order,
    # ...and return the result

I'm also not sure that such a trivial method is really needed, but let's set it aside and assume it is needed, say, due to frequent use of such word sandwiching.

Implementation just does not matter and can be based on the same formatting approach you have used. As you can see, nothing will need to be hard-coded. Moreover, the method signature is intuitive. My naming only helps me; I use this naming sometimes. I borrowed it from my highly respected colleague Paul Dirac who first introduced it :-):
https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation[^],
https://en.wikipedia.org/wiki/Paul_Dirac[^].

—SA
 
Share this answer
 
v4
Alternative way:
1. Convert string to list
2. Insert the word
3. Print the list

Code:
Python
def make_out_word(out, word):
        new_out = list(out)
        new_out.insert(2, word)
        print "".join(new_out)

make_out_word('<<>>', 'Yay')
make_out_word('<<>>', 'WooHoo')
make_out_word('[[]]', 'word')
 
Share this answer
 

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