diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-03-02 12:49:03 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-03-02 12:49:03 +0000 |
| commit | 23ff3c432d6c4cf04d74d658de8fced1557c1b5a (patch) | |
| tree | a1ccdfb6f613d57fcced955d9330060a18ba64f0 /challenge-050/wanderdoc/perl | |
| parent | 16cc6c3136cfd77620c12e2f80cc09cfd5ee98cf (diff) | |
| download | perlweeklychallenge-club-23ff3c432d6c4cf04d74d658de8fced1557c1b5a.tar.gz perlweeklychallenge-club-23ff3c432d6c4cf04d74d658de8fced1557c1b5a.tar.bz2 perlweeklychallenge-club-23ff3c432d6c4cf04d74d658de8fced1557c1b5a.zip | |
- Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-050/wanderdoc/perl')
| -rw-r--r-- | challenge-050/wanderdoc/perl/ch-1.pl | 57 | ||||
| -rw-r--r-- | challenge-050/wanderdoc/perl/ch-2.pl | 38 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-050/wanderdoc/perl/ch-1.pl b/challenge-050/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..a984b4112a --- /dev/null +++ b/challenge-050/wanderdoc/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +Write a script to merge the given intervals where ever possible. +Example: [2,7], [3,9], [10,12], [15,19], [18,22] +The script should merge [2, 7] and [3, 9] together to return [2, 9]. +Similarly it should also merge [15, 19] and [18, 22] together to return [15, 22]. +The final result should be something like below: +[2, 9], [10, 12], [15, 22] +=cut + +use Bit::Vector; +use List::Util qw(max); + +my @intervals = ([2,7], [3,9], [10,12], [15,19], [18,22]); + +# Pushing of, say, [10, 35] would not otherwise work correctly +@intervals = sort { $a->[0] <=> $b->[0] } @intervals; + +my $max = max(map @$_, @intervals); +my @veclist = Bit::Vector->new($max + 1, scalar @intervals); +$veclist[$_]->Interval_Fill(@{$intervals[$_]}) for 0 .. $#veclist; + + +my @results; +my $res = $veclist[0]->Shadow(); + +for my $v ( @veclist ) +{ + if ($res->is_empty()) + { + $res->Or($res, $v); + } + else + { + # to prevent merging [3,9] and [10,12]. + if ( $v->Min() < $res->Max() ) + { + $res->Or($res, $v); + } + else + { + my $res_p = $res->Clone(); + push @results, $res_p; + $res->Empty(); + $res->Or($res, $v); + + } + } +} +push @results, $res; +print '[' . $_->to_Enum() . ']' for @results; +# print $/ x 2; +# or +# print join('-', $_->Index_List_Read()), $/ for @results;
\ No newline at end of file diff --git a/challenge-050/wanderdoc/perl/ch-2.pl b/challenge-050/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..b6cc78cb67 --- /dev/null +++ b/challenge-050/wanderdoc/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +Write a script to generate a list of 20 random integers between 1 and 50. It should then find an integer, say N, in the list so that there are exactly N number of integers greater than N. If you can't find the number print -1 otherwise 1. +For example, +Suppose we have list of 4 integers [2, 6, 1, 3]. +Here we have 2 in the above list, known as Noble Integer, since there are exactly 2 integers in the list i.e.3 and 6, which are greater than 2. +Therefore the script would print 1. +=cut + + +use List::Util qw(sample); + +my $MIN = 1; +my $MAX = 50; +my $SAMPLE = 20; + +my @range = ($MIN .. $MAX); +noble(\@range, $SAMPLE) for 1 .. 20; + +sub noble +{ + my ($aref, $n) = @_; + + my @list = sort {$a <=> $b} sample($n, @$aref); + for my $int ( @list ) + { + my $count = grep $_ > $int, @list; + if ( $int == $count ) + { + print "1$/"; # "1: $int $count$/"; + return; + } + } + print "-1$/"; +}
\ No newline at end of file |
