2023-02-11

Please write a? Perl script to address m'y following request:

I have a string of alphabet a, b, c, d, e as input and they are ordered differently in different dataset, or some elements are missing in some dataset:

data1 : a, b, c, d, e

data2: a, c, e, d

data3: b, c, d, a

data4: e, d, c, b, a


I want perl code to produce a 5x5 matrix to tell pairwise relationship between a, b, c, d, e. For example, for b in data1, it scores 9, 10, 9, 8, 7 with a, b, c, d, e, respectively. i.e. the closer the higer the score is. so data2, 3, 4 are scored in the same way.finally, the 5x5 matrix to tell pairwise relationship of a,b,c,d,e shall sum the scores calculated in data1,2,3,4.

#!/usr/bin/perl

use strict;

use warnings;

# Initialize the data

my @data = (

? ? [qw/a b c d e/],

? ? [qw/b a c e d/],

? ? [qw/b c d a/],

? ? [qw/e d c b a/]

);

# Initialize the array for the score

my @score;

# Loop through each data array

foreach my $data (@data) {

? ? # Initialize the score for each letter

? ? my %letters;

? ? foreach my $letter (@$data) {

? ? ? ? $letters{$letter} = 0;

? ? }

? ?

? ? # Loop through each letter

? ? for (my $i=0; $i < scalar @$data; $i++) {

? ? ? ? my $letter = $data->[$i];

? ? ? ?

? ? ? ? # Loop through comparison letters

? ? ? ? for (my $j=$i+1; $j < scalar @$data; $j++) {

? ? ? ? ? ? my $compare = $data->[$j];

? ? ? ? ? ? $letters{$letter} += scalar @$data - $j;

? ? ? ? ? ? $letters{$compare} += scalar @$data - $j;

? ? ? ? }

? ? }

? ?

? ? # Add the score of each letter to the score array

? ? push @score, [values %letters];

}

# Sum the total of each letter

my @total;

foreach my $row (@score) {

? ? for (my $i=0; $i < scalar @$row; $i++) {

? ? ? ? $total[$i] += $row->[$i];

? ? }

}

# Add the total of each letter to the score array

push @score, \@total;

# Print out the score matrix

foreach my $row (@score) {

? ? foreach my $score (@$row) {

? ? ? ? print "$score\t";

? ? }

? ? print "\n";

}


#


#!/usr/bin/perl

use strict;

use warnings;

#Define the data

my @data1 = qw/a b c d e/;

my @data2 = qw/a c e d/;

my @data3 = qw/b c d a/;

my @data4 = qw/e d c b a/;

# Initialize the matrix

my %matrix;

foreach my $x (@data1) {

? ? foreach my $y (@data1) {

? ? ? ? $matrix{$x}{$y} = 0;

? ? }

}

# Calculate scores for each dataset

foreach my $list (\@data1, \@data2, \@data3, \@data4) {

? ? my $score = 10;

? ? foreach my $i (0..(scalar @$list-2)) {

? ? ? ? foreach my $j ($i..(scalar @$list-1)) {

? ? ? ? ? ? $matrix{$list->[$i]}{$list->[$j]} += $score;

? ? ? ? ? ? $matrix{$list->[$j]}{$list->[$i]} += $score;

? ? ? ? ? ? $score--;

? ? ? ? }

? ? }

}

#Print the matrix

foreach my $x (sort keys %matrix) {

? ? print "$x\t";

? ? foreach my $y (sort keys %{$matrix{$x}}) {

? ? ? ? print "$matrix{$x}{$y}\t";

? ? }

? ? print "\n";

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子犬钢,更是在濱河造成了極大的恐慌,老刑警劉巖兄猩,帶你破解...
    沈念sama閱讀 221,331評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡枢冤,警方通過查閱死者的電腦和手機(jī)鸠姨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,372評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來淹真,“玉大人讶迁,你說我怎么就攤上這事√伺兀” “怎么了添瓷?”我有些...
    開封第一講書人閱讀 167,755評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長值纱。 經(jīng)常有香客問我鳞贷,道長,這世上最難降的妖魔是什么虐唠? 我笑而不...
    開封第一講書人閱讀 59,528評論 1 296
  • 正文 為了忘掉前任搀愧,我火速辦了婚禮,結(jié)果婚禮上疆偿,老公的妹妹穿的比我還像新娘咱筛。我一直安慰自己,他們只是感情好杆故,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,526評論 6 397
  • 文/花漫 我一把揭開白布迅箩。 她就那樣靜靜地躺著,像睡著了一般处铛。 火紅的嫁衣襯著肌膚如雪饲趋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,166評論 1 308
  • 那天撤蟆,我揣著相機(jī)與錄音奕塑,去河邊找鬼。 笑死家肯,一個(gè)胖子當(dāng)著我的面吹牛龄砰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播讨衣,決...
    沈念sama閱讀 40,768評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼换棚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了反镇?” 一聲冷哼從身側(cè)響起圃泡,我...
    開封第一講書人閱讀 39,664評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎愿险,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,205評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡辆亏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,290評論 3 340
  • 正文 我和宋清朗相戀三年风秤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扮叨。...
    茶點(diǎn)故事閱讀 40,435評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡缤弦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出彻磁,到底是詐尸還是另有隱情碍沐,我是刑警寧澤,帶...
    沈念sama閱讀 36,126評論 5 349
  • 正文 年R本政府宣布衷蜓,位于F島的核電站累提,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏磁浇。R本人自食惡果不足惜斋陪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,804評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望置吓。 院中可真熱鬧无虚,春花似錦、人聲如沸衍锚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,276評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽戴质。三九已至度宦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間置森,已是汗流浹背斗埂。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留凫海,地道東北人呛凶。 一個(gè)月前我還...
    沈念sama閱讀 48,818評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像行贪,于是被迫代替她去往敵國和親漾稀。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,442評論 2 359

推薦閱讀更多精彩內(nèi)容