Last active
September 20, 2016 14:28
-
-
Save adokoy001/7890136538e3c979e339d0f1b890eab3 to your computer and use it in GitHub Desktop.
List comparison in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
sub list_unique{ | |
my $left = shift; | |
my $right = shift; | |
my @list = (@$left,@$right); | |
my %tmp1; | |
my @unique = grep { !$tmp1{$_}++ } (@list); | |
return @unique; | |
} | |
sub list_union{ | |
return(&list_unique(@_)); | |
} | |
sub list_or{ | |
return(&list_unique(@_)); | |
} | |
sub list_unique_multi{ | |
my @list = @_; | |
my %tmp1; | |
my @unique = grep { !$tmp1{$_}++ } (@list); | |
return @unique; | |
} | |
sub list_union_multi{ | |
return(&list_unique_multi(@_)); | |
} | |
sub list_or_multi{ | |
return(&list_unique_multi(@_)); | |
} | |
sub list_intersection{ | |
my $left = shift; | |
my $right = shift; | |
my %tmp_left = map {$_ => 1} @$left; | |
my @intersection = grep { $tmp_left{$_}++ } @$right; | |
return @intersection; | |
} | |
sub list_and{ | |
return(&list_intersection(@_)); | |
} | |
sub list_intersection_multi{ | |
my @lists = @_; | |
my @uniques; | |
foreach my $list (@lists){ | |
my %tmp_pre; | |
push(@uniques,grep { !$tmp_pre{$_}++ } @$list); | |
} | |
my %tmp; | |
grep { $tmp{$_}++ } @uniques; | |
my @and_multi; | |
foreach my $key (keys %tmp){ | |
if( $tmp{$key} == scalar(@lists)){ | |
push(@and_multi,$key); | |
} | |
} | |
return @and_multi; | |
} | |
sub list_and_multi{ | |
return(&list_intersection_multi(@_)); | |
} | |
sub list_leftonly{ | |
my $left = shift; | |
my $right = shift; | |
my %tmp_right = map {$_ => 1} @$right; | |
my @leftonly = grep { !$tmp_right{$_}++ } @$left; | |
return @leftonly; | |
} | |
sub list_rightonly{ | |
my $left = shift; | |
my $right = shift; | |
my %tmp_left = map {$_ => 1} @$left; | |
my @rightonly = grep { !$tmp_left{$_}++ } @$right; | |
return @rightonly; | |
} | |
sub list_symmetric_diff{ | |
my $left = shift; | |
my $right = shift; | |
my @leftonly = &list_leftonly($left,$right); | |
my @rightonly = &list_rightonly($left,$right); | |
return(&list_unique_multi(@leftonly,@rightonly)); | |
} | |
sub list_xor{ | |
return(&list_symmetric_diff(@_)); | |
} | |
sub list_symmetric_diff_multi{ | |
my @lists = @_; | |
my @uniques; | |
foreach my $list (@lists){ | |
my %tmp_pre; | |
push(@uniques,grep { !$tmp_pre{$_}++ } @$list); | |
} | |
my %tmp; | |
grep { $tmp{$_}++ } @uniques; | |
my @xor_multi; | |
foreach my $key (keys %tmp){ | |
if(($tmp{$key}) % 2 == 1){ | |
push(@xor_multi,$key); | |
} | |
} | |
return @xor_multi; | |
} | |
sub list_xor_multi{ | |
return(&list_symmetric_diff_multi(@_)); | |
} | |
sub list_is_proper_subset{ | |
my $left = shift; | |
my $right = shift; | |
if( | |
scalar(&list_leftonly($left,$right)) == 0 | |
and scalar(&list_rightonly($left,$right)) != 0 | |
){ | |
return 1; | |
}else{ | |
return 0; | |
} | |
} | |
# if list_is_proper_subset(A,B) == 1, | |
# A is proper subset of B(B is proper superset of A). | |
# | |
# if list_is_proper_subset(B,A) == 1, | |
# B is proper subset of A(A is proper superset of B). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment