From 2afb6105c4f9494d93bbf6bae3d7a71c77043ae6 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Tue, 28 Apr 2020 11:53:17 +0800 Subject: Add files via upload --- ch-2.pl | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ch-2.pl diff --git a/ch-2.pl b/ch-2.pl new file mode 100644 index 0000000000..3be4ef6dfb --- /dev/null +++ b/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl +use strict; + +# reference: +# http://mathonline.wikidot.com/generating-permutations-with-inversion-sequences + +# begin: from the question statement +# Heights +my @H = (27, 21, 37, 4, 19, 52, 23, 64, 1, 7, 51, 17, 24, 50, 3, 2, + 34, 40, 47, 20, 8, 56, 14, 16, 42, 38, 62, 53, 31, 41, 55, 59, + 48, 12, 32, 61, 9, 60, 46, 26, 58, 25, 15, 36, 11, 44, 63, 28, + 5, 54, 10, 49, 57, 30, 29, 22, 35, 39, 45, 43, 18, 6, 13, 33); + +# Number taller people in front + my @T = ( 6, 41, 1, 49, 38, 12, 1, 0, 58, 47, 4, 17, 26, 1, 61, 12, + 29, 3, 4, 11, 45, 1, 32, 5, 9, 19, 1, 4, 28, 12, 2, 2, + 13, 18, 19, 3, 4, 1, 10, 16, 4, 3, 29, 5, 49, 1, 1, 24, + 2, 1, 38, 7, 7, 14, 35, 25, 0, 5, 4, 19, 10, 13, 4, 12); + +#my @H = (2, 6, 4, 5, 1, 3) ; +##my @T = (1, 0, 2, 0, 1, 2) ; + +# end: from the question statement + +my $N = $#H+1; +my @inversion_seq; + +my %height_appeartime; +for (1..$N) { $height_appeartime{$_} = $H[$_-1]; } +my %id_sorted = reverse %height_appeartime; + +# @id --> id according to height +my @id = map { $id_sorted{$_} } ( sort {$a<=>$b} keys %height_appeartime); + +my @inversion_seq = map {$T[$_-1] } @id; + + + +my @placement = ((-1) x $N); + +for my $i (0..$N-1) { + my @possible_place = (); + for (0..$N-1) { + push @possible_place, $_ if $placement[$_] == -1; + } + $placement[ $possible_place[ $inversion_seq[$i] ] ] = $i; +} + +@placement = map { $_+1 } @placement; + +print join ", " , @placement; -- cgit From a26ffe4606ca77c66eda7c81c7e2d9f4bb611c8c Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Tue, 28 Apr 2020 11:54:05 +0800 Subject: Rename ch-2.pl to challenge-058/cheok-yin-fung/perl/ch-2.pl --- ch-2.pl | 51 ------------------------------- challenge-058/cheok-yin-fung/perl/ch-2.pl | 51 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 51 deletions(-) delete mode 100644 ch-2.pl create mode 100644 challenge-058/cheok-yin-fung/perl/ch-2.pl diff --git a/ch-2.pl b/ch-2.pl deleted file mode 100644 index 3be4ef6dfb..0000000000 --- a/ch-2.pl +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/perl -use strict; - -# reference: -# http://mathonline.wikidot.com/generating-permutations-with-inversion-sequences - -# begin: from the question statement -# Heights -my @H = (27, 21, 37, 4, 19, 52, 23, 64, 1, 7, 51, 17, 24, 50, 3, 2, - 34, 40, 47, 20, 8, 56, 14, 16, 42, 38, 62, 53, 31, 41, 55, 59, - 48, 12, 32, 61, 9, 60, 46, 26, 58, 25, 15, 36, 11, 44, 63, 28, - 5, 54, 10, 49, 57, 30, 29, 22, 35, 39, 45, 43, 18, 6, 13, 33); - -# Number taller people in front - my @T = ( 6, 41, 1, 49, 38, 12, 1, 0, 58, 47, 4, 17, 26, 1, 61, 12, - 29, 3, 4, 11, 45, 1, 32, 5, 9, 19, 1, 4, 28, 12, 2, 2, - 13, 18, 19, 3, 4, 1, 10, 16, 4, 3, 29, 5, 49, 1, 1, 24, - 2, 1, 38, 7, 7, 14, 35, 25, 0, 5, 4, 19, 10, 13, 4, 12); - -#my @H = (2, 6, 4, 5, 1, 3) ; -##my @T = (1, 0, 2, 0, 1, 2) ; - -# end: from the question statement - -my $N = $#H+1; -my @inversion_seq; - -my %height_appeartime; -for (1..$N) { $height_appeartime{$_} = $H[$_-1]; } -my %id_sorted = reverse %height_appeartime; - -# @id --> id according to height -my @id = map { $id_sorted{$_} } ( sort {$a<=>$b} keys %height_appeartime); - -my @inversion_seq = map {$T[$_-1] } @id; - - - -my @placement = ((-1) x $N); - -for my $i (0..$N-1) { - my @possible_place = (); - for (0..$N-1) { - push @possible_place, $_ if $placement[$_] == -1; - } - $placement[ $possible_place[ $inversion_seq[$i] ] ] = $i; -} - -@placement = map { $_+1 } @placement; - -print join ", " , @placement; diff --git a/challenge-058/cheok-yin-fung/perl/ch-2.pl b/challenge-058/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..3be4ef6dfb --- /dev/null +++ b/challenge-058/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl +use strict; + +# reference: +# http://mathonline.wikidot.com/generating-permutations-with-inversion-sequences + +# begin: from the question statement +# Heights +my @H = (27, 21, 37, 4, 19, 52, 23, 64, 1, 7, 51, 17, 24, 50, 3, 2, + 34, 40, 47, 20, 8, 56, 14, 16, 42, 38, 62, 53, 31, 41, 55, 59, + 48, 12, 32, 61, 9, 60, 46, 26, 58, 25, 15, 36, 11, 44, 63, 28, + 5, 54, 10, 49, 57, 30, 29, 22, 35, 39, 45, 43, 18, 6, 13, 33); + +# Number taller people in front + my @T = ( 6, 41, 1, 49, 38, 12, 1, 0, 58, 47, 4, 17, 26, 1, 61, 12, + 29, 3, 4, 11, 45, 1, 32, 5, 9, 19, 1, 4, 28, 12, 2, 2, + 13, 18, 19, 3, 4, 1, 10, 16, 4, 3, 29, 5, 49, 1, 1, 24, + 2, 1, 38, 7, 7, 14, 35, 25, 0, 5, 4, 19, 10, 13, 4, 12); + +#my @H = (2, 6, 4, 5, 1, 3) ; +##my @T = (1, 0, 2, 0, 1, 2) ; + +# end: from the question statement + +my $N = $#H+1; +my @inversion_seq; + +my %height_appeartime; +for (1..$N) { $height_appeartime{$_} = $H[$_-1]; } +my %id_sorted = reverse %height_appeartime; + +# @id --> id according to height +my @id = map { $id_sorted{$_} } ( sort {$a<=>$b} keys %height_appeartime); + +my @inversion_seq = map {$T[$_-1] } @id; + + + +my @placement = ((-1) x $N); + +for my $i (0..$N-1) { + my @possible_place = (); + for (0..$N-1) { + push @possible_place, $_ if $placement[$_] == -1; + } + $placement[ $possible_place[ $inversion_seq[$i] ] ] = $i; +} + +@placement = map { $_+1 } @placement; + +print join ", " , @placement; -- cgit From 9091fef736477a14d7f8092e1355bd49d52beac0 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Tue, 28 Apr 2020 13:56:05 +0800 Subject: Update ch-2.pl --- challenge-058/cheok-yin-fung/perl/ch-2.pl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/challenge-058/cheok-yin-fung/perl/ch-2.pl b/challenge-058/cheok-yin-fung/perl/ch-2.pl index 3be4ef6dfb..443054141a 100644 --- a/challenge-058/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-058/cheok-yin-fung/perl/ch-2.pl @@ -23,7 +23,6 @@ my @H = (27, 21, 37, 4, 19, 52, 23, 64, 1, 7, 51, 17, 24, 50, 3, 2, # end: from the question statement my $N = $#H+1; -my @inversion_seq; my %height_appeartime; for (1..$N) { $height_appeartime{$_} = $H[$_-1]; } @@ -43,7 +42,11 @@ for my $i (0..$N-1) { for (0..$N-1) { push @possible_place, $_ if $placement[$_] == -1; } - $placement[ $possible_place[ $inversion_seq[$i] ] ] = $i; + if ( defined($possible_place[$inversion_seq[$i]]) ) { + $pos = $possible_place[ $inversion_seq[$i] ]; + } else {die "ERROR";} + + $placement[ $pos ] = $i; } @placement = map { $_+1 } @placement; -- cgit From 7e1c2de792dce48ccc3ba230b5c577d424a110ae Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Tue, 28 Apr 2020 13:56:48 +0800 Subject: Update ch-2.pl --- challenge-058/cheok-yin-fung/perl/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-058/cheok-yin-fung/perl/ch-2.pl b/challenge-058/cheok-yin-fung/perl/ch-2.pl index 443054141a..5c655c1409 100644 --- a/challenge-058/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-058/cheok-yin-fung/perl/ch-2.pl @@ -12,13 +12,13 @@ my @H = (27, 21, 37, 4, 19, 52, 23, 64, 1, 7, 51, 17, 24, 50, 3, 2, 5, 54, 10, 49, 57, 30, 29, 22, 35, 39, 45, 43, 18, 6, 13, 33); # Number taller people in front - my @T = ( 6, 41, 1, 49, 38, 12, 1, 0, 58, 47, 4, 17, 26, 1, 61, 12, +my @T = ( 6, 41, 1, 49, 38, 12, 1, 0, 58, 47, 4, 17, 26, 1, 61, 12, 29, 3, 4, 11, 45, 1, 32, 5, 9, 19, 1, 4, 28, 12, 2, 2, 13, 18, 19, 3, 4, 1, 10, 16, 4, 3, 29, 5, 49, 1, 1, 24, 2, 1, 38, 7, 7, 14, 35, 25, 0, 5, 4, 19, 10, 13, 4, 12); -#my @H = (2, 6, 4, 5, 1, 3) ; -##my @T = (1, 0, 2, 0, 1, 2) ; +# my @H = (2, 6, 4, 5, 1, 3) ; +# my @T = (1, 0, 2, 0, 1, 2) ; # end: from the question statement -- cgit