fork download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4.  
  5. sub indx { (grep{ $_[0]->[$_] == $_[1] } (0 .. $#{$_[0]}))[0] }
  6.  
  7. sub f {
  8. my ($seq_a, $am, $seq_b) = @_;
  9.  
  10. my $i = indx($seq_a, $am);
  11.  
  12. my %index;
  13. @index{@{$seq_a}[$i .. $#{$seq_a}, 0 .. $i - 1]} = (0 .. $#{$seq_a});
  14.  
  15. return [sort{ $index{$a} <=> $index{$b} } @{$seq_b}];
  16. }
  17.  
  18. use Data::Dumper;
  19. local $Data::Dumper::Indent = 0;
  20. local $Data::Dumper::Terse = 1;
  21.  
  22. say Dumper f([2, 3, 5, 7, 9], 2, [9, 2, 5]);
  23. say Dumper f([2, 3, 5, 7, 9], 9, [9, 2, 5]);
  24. say Dumper f([2, 3, 5, 7, 9], 5, [9, 2, 5]);
  25. say Dumper f([2, 3, 5, 7, 9], 3, [9, 2, 5, 5, 2]);
  26.  
Success #stdin #stdout 0.03s 5176KB
stdin
Standard input is empty
stdout
[2,5,9]
[9,2,5]
[5,9,2]
[5,5,9,2,2]