daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest May 16th, 2018 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use strict;
  2. use Algorithm::Permute qw/permute/;
  3.  
  4. my $num = shift or die "pls specify some number";
  5.  
  6. # generator
  7. my $len = length($num) - 1;
  8. my %ops;
  9. foreach my $i (0..$len) {
  10.     $ops{'+' x $i . '-' x ($len - $i)}++;
  11.     $ops{'+' x $i . ' ' x ($len - $i)}++;
  12.     $ops{'-' x $i . ' ' x ($len - $i)}++;
  13. }
  14.  
  15. my @operands = split //, $num;
  16. my %res;
  17. foreach my $k (keys %ops) {
  18.     my @o = split //, $k;
  19.     # permutasi
  20.     permute {
  21.         my @ary = @o;
  22.         my $e = join '', map { my $o = shift(@ary) || ''; "$_$o" } @operands;
  23.         $e =~ s/ //g;
  24.         $res{eval $e}++;
  25.     } @o;
  26. }
  27. my $total = scalar keys %res;
  28. print "$total\n";
  29. # print join(', ', keys %res), "\n";
RAW Paste Data
Top