Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Math::Combinatorics;
  5.  
  6. # Base Elements
  7. my @n = qw (a b c d e);
  8.  
  9. #Generating 5 Arrays with all combinations.
  10.  
  11. my @comb1 = combine (1,@n);
  12. my @comb2 = combine (2,@n);
  13. my @comb3 = combine (3,@n);
  14. my @comb4 = combine (4,@n);
  15. my @comb5 = combine (5,@n);
  16.  
  17. #Packing it up into an array of array
  18. my @samples = (@comb1, @comb2, @comb3, @comb4, @comb5);
  19.  
  20. #Determing the size of all the samples
  21. my $size = $#samples;
  22.  
  23. my @randoms;#Generating 10 random numbers within the the amount of the numbers
  24. for (my $i = 0; $i<=10; $i++){
  25. push (@randoms,int(rand($size+1)));
  26. }
  27. #indexing 10: 0-9 - this is superflus.
  28. my @index;
  29. for (my $i = 0; $i <= $#randoms-1; $i++){
  30. $index[$i] = $i;
  31. }
  32.  
  33. my @storage;
  34. my @results;
  35. #storage 0-9 now contains the picked values of the base elements
  36. for(my $i = 0; $i <= $#index; $i++){
  37. $storage[$i] = $samples[$randoms[$i]];
  38. }
  39. @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.
  40.  
  41. #Fun Part stars here.
  42. my %control;# hash to count within the loop how often one element was matched to the other.
  43. for (my $x = 1; $x <=1; $x++){#this loop is just here to limit the loops beneth to one run.
  44.  
  45. for (my $k = 0; $k <= $#storage; $k++){#first level over all elements of storage
  46.  
  47. 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
  48.  
  49. for (my $j = 0; $j <= $#storage; $j++){ #for the second loop over storage
  50.  
  51. for (my $t = 0;$t <= scalar @{$storage[$j]}-1; $t++ ){ #second loop for the elements
  52.  
  53. if ($k ne $j) { # Avoid self recognition.
  54.  
  55. if ($storage[$k][$m] eq $storage[$j][$t]) {
  56. $control{$k}{$j}++; #couting the matches within the loop for the elements.E.g How often did $k match $j?
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64.  
  65. foreach my $k (sort keys %control ){ # identifying the elements that are subset of others via the hash
  66. 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
  67. 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.
  68. push (@results,$k);#if true -> index the element found
  69. }
  70. }
  71. }
  72.  
  73. 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.
  74. my %seen;# unique and seen condense something like 1 1 1 1 to just 1
  75. foreach my $resultelement(@results){ # remove dubbles from the index
  76. next if $seen{$resultelement} ++; # if element seen once or more go to the next (PerlFAQ)
  77. push (@unique,$resultelement); # indexing the unique elements
  78. }
  79.  
  80. my %resultsbox; # generating a hash with all array of arrays to process and not to mess with the origin structure storage.
  81. for(my $i = 0; $i <= $#storage; $i++){
  82. $resultsbox{$i} = $storage[$i];
  83. }
  84.  
  85. foreach my $k (sort keys %resultsbox){#just here to check and give out the random elements again.
  86. print "$k : @{$resultsbox{$k}}\n";
  87. }
  88.  
  89. foreach my $element (@unique){
  90. delete $resultsbox{$element};#removing the elements which are subset of others and were indexed before.
  91. }
  92.  
  93. foreach my $k (sort keys %resultsbox){
  94. print "Element $k :\n@{$resultsbox{$k}}\nis no subset of any other!\n"; # final result done.
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement