解决它的另一种方法 [英] alternate method for solving it

查看:102
本文介绍了解决它的另一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定out字符串长度4,例如<<>>和单词,返回一个新字符串,其中该单词位于out字符串的中间,例如<<字GT;> 中。



 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

解决方案

我不认为实施需要替代方法,但我认为问题的制定和方法简介强烈要求改进。



你的表述的一个大问题是强烈地暗示 ad-hoc 行为,这与抽象和普遍性相反。在您的实现中,您硬编码立即常量 0和2,这总是很糟糕(对于维护和其他所有事情),但您必须将问题的表述归咎于实现。即使具有完美的实现,当开头括号具有除2以外的任何数量的字符时,您的方法也会失败。而且,该方法的签名不是自我评论。如果给用户不了解第一个参数的要求;而且要求非常不自然。 这不仅是坏事,这是非常糟糕的。



同时,更明智的解决方案显而易见。使用不同的签名:

  def  makeOutWord(bra,word,ket):
concatenate bra,word and ket,
按此顺序,
...并返回结果



我也不确定真的需要这么简单的方法,但让我们把它放在一边并假设它是比如,由于频繁使用这样的单词夹心所需要的。



实现无关紧要,可以基于您使用的相同格式化方法。如您所见,没有什么需要硬编码。此外,方法签名是直观的。我的命名只对我有所帮助;我有时会使用这个命名。我是从我备受尊敬的同事Paul Dirac那里借来的,他首先介绍了它:-):

https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation [ ^ ],

https: //en.wikipedia.org/wiki/Paul_Dirac [ ^ ]。



-SA


替代方式:

1.将字符串转换为列表

2.插入单词

3.打印列表



代码:

  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(' [[]]'' 字'


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>>".

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

解决方案

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:

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


Alternative way:
1. Convert string to list
2. Insert the word
3. Print the list

Code:

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')


这篇关于解决它的另一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆