Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Math::Combinatorics;
- # Base Elements
- my @n = qw (a b c d e);
- #Generating 5 Arrays with all combinations.
- my @comb1 = combine (1,@n);
- my @comb2 = combine (2,@n);
- my @comb3 = combine (3,@n);
- my @comb4 = combine (4,@n);
- my @comb5 = combine (5,@n);
- #Packing it up into an array of array
- my @samples = (@comb1, @comb2, @comb3, @comb4, @comb5);
- #Determing the size of all the samples
- my $size = $#samples;
- my @randoms;#Generating 10 random numbers within the the amount of the numbers
- for (my $i = 0; $i<=10; $i++){
- push (@randoms,int(rand($size+1)));
- }
- #indexing 10: 0-9 - this is superflus.
- my @index;
- for (my $i = 0; $i <= $#randoms-1; $i++){
- $index[$i] = $i;
- }
- my @storage;
- my @results;
- #storage 0-9 now contains the picked values of the base elements
- for(my $i = 0; $i <= $#index; $i++){
- $storage[$i] = $samples[$randoms[$i]];
- }
- @storage = sort{scalar(@{$a}) <=> scalar(@{$b})}@storage;#sorting it by array sizes to put the high element picks at the end since they have the lesser chance to be a combination.
- #Fun Part stars here.
- my %control;# hash to count within the loop how often one element was matched to the other.
- for (my $x = 1; $x <=1; $x++){#this loop is just here to limit the loops beneth to one run.
- for (my $k = 0; $k <= $#storage; $k++){#first level over all elements of storage
- for (my $m = 0; $m <= scalar @{$storage[$k]}-1;$m++){ #over the lenghts of element k, using scalar since $# does not seem to work on array ref
- for (my $j = 0; $j <= $#storage; $j++){ #for the second loop over storage
- for (my $t = 0;$t <= scalar @{$storage[$j]}-1; $t++ ){ #second loop for the elements
- if ($k ne $j) { # Avoid self recognition.
- if ($storage[$k][$m] eq $storage[$j][$t]) {
- $control{$k}{$j}++; #couting the matches within the loop for the elements.E.g How often did $k match $j?
- }
- }
- }
- }
- }
- }
- }
- foreach my $k (sort keys %control ){ # identifying the elements that are subset of others via the hash
- foreach my $x (sort keys %{$control{$k}}){ #practically a hash of hash with the array identifier and a count how often it was machted to $j
- if ($control{$k}{$x} eq scalar @{$storage[$k]} ) { #comparing the amount of matches with the length of the element to verify that it is a subset.
- push (@results,$k);#if true -> index the element found
- }
- }
- }
- my @unique;#since loop matches more then once and thank god for arrays every match for every round of looping gets stored seperatly which is easily removed.
- my %seen;# unique and seen condense something like 1 1 1 1 to just 1
- foreach my $resultelement(@results){ # remove dubbles from the index
- next if $seen{$resultelement} ++; # if element seen once or more go to the next (PerlFAQ)
- push (@unique,$resultelement); # indexing the unique elements
- }
- my %resultsbox; # generating a hash with all array of arrays to process and not to mess with the origin structure storage.
- for(my $i = 0; $i <= $#storage; $i++){
- $resultsbox{$i} = $storage[$i];
- }
- foreach my $k (sort keys %resultsbox){#just here to check and give out the random elements again.
- print "$k : @{$resultsbox{$k}}\n";
- }
- foreach my $element (@unique){
- delete $resultsbox{$element};#removing the elements which are subset of others and were indexed before.
- }
- foreach my $k (sort keys %resultsbox){
- print "Element $k :\n@{$resultsbox{$k}}\nis no subset of any other!\n"; # final result done.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement