计算机语言枪战的代码 [英] code for Computer Language Shootout

查看:93
本文介绍了计算机语言枪战的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shootout.alioth.debian.org上有很多新的测试。

Python还没有代码。我已经对其中一个进行了破解,这是一项打印基因转录反向互补的任务。由于在这个新闻组中有很多想法优于

我,我正在发布我想出的代码,看看是否有人看到任何实质性改善的机会。不用多说:


table = string.maketrans(''ACBDGHK \\\
MNSRUTWVY'',''TGVHCDM \\\
KNSYAAWBR'')


def show(s):

i = 0

for s inup()。translate(table)[:: - 1]:

如果我= = 60:

打印

i = 0

sys.stdout.write(char)

i + = 1

打印

def main():

seq =''''
sys.stdin中的行


如果行[0] ==''>''或行[0] =='';'':

if seq!='''':

show(seq)

seq =''''

打印行,

else:

seq + = line [: - 1]

show(seq)


main()

将seq放入列表而不是字符串(并使用.extend而不是

+运算符)并没有提高速度。既没有使用

字典而不是翻译函数,或者使用reverse()代替

s [:: - 1]。后者让我感到惊讶,因为我猜想使用

迭代器会更有效率。由于枪战也测试了内存的使用情况,因此我应该使用逆转
吗?有没有人有任何其他

的想法来优化这些代码?


顺便说一下 - 是否有一个很好的方法来找出程序的最大内存? />
使用(以time命令的方式)?除了下载和运行枪战基准脚本的
之外,当然。


-

Jacob Lee
< a href =mailto:je **** @ uiuc.edu> je **** @ uiuc.edu | www.nearestneighbor.net

解决方案

Jacob Lee写道:

顺便说一句 - 是否有一个很好的方法来找出一个程序使用的最大内存(在时间命令的方式)?除了下载和运行枪战基准脚本之外,当然。




用raw_input()插入适当的暂停并记录内存
$ b使用top(1)使用$ b是一种快速而肮脏的方式。


-

Robert Kern
rk *** @ ucsd.edu


在草地长得高的地狱里

是否允许死亡的坟墓死亡。

- Richard Harter


这是我的解决方案问题[1]:


[1] http://shootout.alioth.debian.org/be...p?test=revcomp


import sys

导入字符串


basetable = string.maketrans(''ACBDGHKMNSRUTWVYacbdghkmnsrutwvy'',

''TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR'')

def revcomp(seqlines ,linelength = 60,basetable = basetable):

seq =''''。join(seqlines)

complement = seq.translate(basetable)

revcomplement = complement [:: - 1]
$ x $ b for x in xrange(0,len(revcomplement),linelength):

print revcomplement [i:i + lineme]


def main():

seqlines = []

for sys.stdin中的行:

如果line.startswith(''>'')或line.startswith('';''):

if seqlines:

revcomp(seqlines )

sys.stdout.write(行)

seqlines = []

else:

seqlines.append (line.strip())

revcomp(seqlines)


if __name__ ==''__ main__'':

main ()


-

Robert Kern
rk ** *@ucsd.edu


在地狱的地方,草地长得很高

梦想的坟墓是否已经死亡。

- Richard Harter

Jacob Lee写道:

在shootout.alioth.debian.org上有一堆新的测试,其中Python还没有代码。我已经对其中一个进行了破解,这是一项打印基因转录反向互补的任务。由于这个新闻组有很多想法比我更优化,我发布了我想出的代码,看看有没有人看到任何机会。实质性改善。不用多说:

table = string.maketrans(''ACBDGHK \\\
MNSRUTWVY'',''TGVHCDM \\\
KNSYAAWBR'')

string.translate是个不错的选择理念。请注意,您可以通过添加从小写到

补充的映射来在一个操作中处理上壳和

转换,即


table = string.maketrans(''ACBDGHK \\\
MNSRUTWVYacbdghkmnsrutw vy'',

''TGVHCDM\\\
KNSYAAWBRTGVHCDMKNSYAAWBR'')

def show(s):
i = 0
对于s.upper()中的char。translate(table)[:: - 1]:


if i == 60:
打印
i = 0
sys.stdout.write(char)
我+ = 1
打印


def main( ):
seq =''''
对于sys.stdin中的行:
如果行[0] ==''>''或行[0] ==''; '':
如果seq!='''':
显示(seq)
seq =''''
打印行,
否则:
seq + = line [: - 1]
show(seq)

main()




这看起来很笨重 - 特别是一次写入sys.stdout oe字符。

我可能不完全理解规范,但这不是同一个:


表示sys.stdin中的行:

if line和(line [0] ==">"或者行[0] ==" ;;"):

打印行

else:

print line.translate(table)


HTH


Michael


There are a bunch of new tests up at shootout.alioth.debian.org for which
Python does not yet have code. I''ve taken a crack at one of them, a task
to print the reverse complement of a gene transcription. Since there are a
lot of minds on this newsgroup that are much better at optimization than
I, I''m posting the code I came up with to see if anyone sees any
opportunities for substantial improvement. Without further ado:

table = string.maketrans(''ACBDGHK\nMNSRUTWVY'', ''TGVHCDM\nKNSYAAWBR'')

def show(s):
i = 0
for char in s.upper().translate(table)[::-1]:
if i == 60:
print
i = 0
sys.stdout.write(char)
i += 1
print

def main():
seq = ''''
for line in sys.stdin:
if line[0] == ''>'' or line[0] == '';'':
if seq != '''':
show(seq)
seq = ''''
print line,
else:
seq += line[:-1]
show(seq)

main()
Making seq into a list instead of a string (and using .extend instead of
the + operator) didn''t give any speed improvements. Neither did using a
dictionary instead of the translate function, or using reversed() instead
of s[::-1]. The latter surprised me, since I would have guessed using an
iterator to be more efficient. Since the shootout also tests memory usage,
should I be using reversed for that reason? Does anyone have any other
ideas to optimize this code?

By the way - is there a good way to find out the maximum memory a program
used (in the manner of the "time" command)? Other than downloading and
running the shootout benchmark scripts, of course.

--
Jacob Lee
je****@uiuc.edu | www.nearestneighbor.net

解决方案

Jacob Lee wrote:

By the way - is there a good way to find out the maximum memory a program
used (in the manner of the "time" command)? Other than downloading and
running the shootout benchmark scripts, of course.



Inserting appropriate pauses with raw_input() and recording the memory
usage using top(1) is a quick and dirty way.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


Here''s my solution to the problem[1]:

[1] http://shootout.alioth.debian.org/be...p?test=revcomp

import sys
import string

basetable = string.maketrans(''ACBDGHKMNSRUTWVYacbdghkmnsrutwvy '',
''TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR'')
def revcomp(seqlines, linelength=60, basetable=basetable):
seq = ''''.join(seqlines)
complement = seq.translate(basetable)
revcomplement = complement[::-1]
for i in xrange(0, len(revcomplement), linelength):
print revcomplement[i:i+linelength]

def main():
seqlines = []
for line in sys.stdin:
if line.startswith(''>'') or line.startswith('';''):
if seqlines:
revcomp(seqlines)
sys.stdout.write(line)
seqlines = []
else:
seqlines.append(line.strip())
revcomp(seqlines)

if __name__ == ''__main__'':
main()

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


Jacob Lee wrote:

There are a bunch of new tests up at shootout.alioth.debian.org for which
Python does not yet have code. I''ve taken a crack at one of them, a task
to print the reverse complement of a gene transcription. Since there are a
lot of minds on this newsgroup that are much better at optimization than
I, I''m posting the code I came up with to see if anyone sees any
opportunities for substantial improvement. Without further ado:

table = string.maketrans(''ACBDGHK\nMNSRUTWVY'', ''TGVHCDM\nKNSYAAWBR'')
string.translate is a good idea. Note you can handle upper-casing and
translation in one operation by adding a mapping from lower case to the
complement i.e.,

table = string.maketrans(''ACBDGHK\nMNSRUTWVYacbdghkmnsrutw vy'',
''TGVHCDM\nKNSYAAWBRTGVHCDMKNSYAAWBR'')
def show(s):
i = 0
for char in s.upper().translate(table)[::-1]:
if i == 60:
print
i = 0
sys.stdout.write(char)
i += 1
print
def main():
seq = ''''
for line in sys.stdin:
if line[0] == ''>'' or line[0] == '';'':
if seq != '''':
show(seq)
seq = ''''
print line,
else:
seq += line[:-1]
show(seq)

main()



This looks unwieldy - especially writing to sys.stdout oe character at a time.
I may not have understood the spec exactly, but isn''t this the same as:

for line in sys.stdin:
if line and (line[0] == ">" or line[0] == ";"):
print line
else:
print line.translate(table)

HTH

Michael


这篇关于计算机语言枪战的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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