gpt4 book ai didi

Perl:获取数组中所有增加和减少的 strip (在生物信息学中使用)

转载 作者:行者123 更新时间:2023-12-01 09:28:45 25 4
gpt4 key购买 nike

我是 Perl 的新手,我在用 Perl 设计某个函数时遇到了问题。

函数应该找到并返回所有增加和减少的 strip 。那是什么意思?如果两个位置是相邻数字,则它们是邻居。即(2,3)或(8,7)。一个增加的地带是一个增加的邻居地带。即(3,4,5,6)。 Decreasing Strip 的定义类似。在每个数组的开头添加 0,在末尾添加数组的长度 +1。没有邻居的单例号码正在减少。 0和n+1递增。

所以如果我有数组 (0,3,4,5,9,8,6,2,1,7,10) 我应该得到以下结果:增加的 strip 是:(3,4,5) (10) (0)递减 strip 是:(9,8), (6), (2,1) (7)

我试图将问题减少到只获取所有递减 strip ,但据我所知:http://pastebin.com/yStbgNme

代码在这里:

sub getIncs{
my @$bar = shift;
my %incs;
my $inccount = 0;
my $i=0;
while($i<@bar-1){
for($j=$i; 1; $j++;){
if($bar[$j] == $bar[$j+1]+1){
$incs{$inccount} = ($i,$j);
} else {
$inccount++;
last;
}
}
}

//edit1:我找到了一个包含上述函数 getStrips() 的 Python 程序,但我的 python 充其量只是零星的。 http://www.csbio.unc.edu/mcmillan/Media/breakpointReversalSort.txt

//edit2:每个数字在数组中正好是一次所以不能重叠。

最佳答案

use strict;
my @s = (0,3,4,5,9,8,6,2,1,7,10);
my $i = 0;
my $j = 0; #size of @s
my $inc = "Increasing: ";
my $dec = "Decreasing: ";

# Prepend the beginning with 0, if necessary
if($s[0] != 0 || @s == 0 ) { unshift @s, 0; }
$j = @s;

foreach(@s) {
# Increasing
if( ($s[$i] == 0) || ($i == $j-1) || ($s[$i+1] - $s[$i]) == 1 || ($s[$i] - $s[$i-1] == 1)) {
if($s[$i] - $s[$i-1] != 1) { $inc .= "("; }
$inc .= $s[$i];
if($s[$i+1] - $s[$i] != 1) { $inc .= ")"; }
if($s[$i+1] - $s[$i] == 1) { $inc .= ","; }
}

#Decreasing
if( ($s[$i]-$s[$i-1] != 1) && ($s[$i+1] - $s[$i] != 1) && ($s[$i] != 0) && ($i != $j-1) ) {
if($s[$i-1] - $s[$i] != 1) { $dec .= "("; }
$dec .= $s[$i];
if($s[$i] - $s[$i+1] != 1) { $dec .= ")"; }
if($s[$i] - $s[$i+1] == 1) { $dec .= ","; }
}
$i++;
}

$inc =~ s/\)\(/\),\(/g;
$dec =~ s/\)\(/\),\(/g;
print "$inc\n";
print "$dec\n";

结果:

Increasing: (0),(3,4,5),(10)
Decreasing: (9,8),(6),(2,1),(7)

关于Perl:获取数组中所有增加和减少的 strip (在生物信息学中使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10666929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com