diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-23 18:08:13 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-23 18:08:13 +0100 |
| commit | 8fa4acfae67dcf037675971ca48c73ef9460191e (patch) | |
| tree | f0f09063b963c45c9844d29aa5858f14f6a82d41 /challenge-079 | |
| parent | a7d9a3210528d31dec5de06b688eaab954f13436 (diff) | |
| download | perlweeklychallenge-club-8fa4acfae67dcf037675971ca48c73ef9460191e.tar.gz perlweeklychallenge-club-8fa4acfae67dcf037675971ca48c73ef9460191e.tar.bz2 perlweeklychallenge-club-8fa4acfae67dcf037675971ca48c73ef9460191e.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-079')
| -rw-r--r-- | challenge-079/ulrich-rieke/perl/ch-2.pl | 55 | ||||
| -rw-r--r-- | challenge-079/ulrich-rieke/raku/ch-1.raku | 18 | ||||
| -rw-r--r-- | challenge-079/ulrich-rieke/raku/ch-2.raku | 44 |
3 files changed, 117 insertions, 0 deletions
diff --git a/challenge-079/ulrich-rieke/perl/ch-2.pl b/challenge-079/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..f57840592a --- /dev/null +++ b/challenge-079/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub enterArray { + my @numbers ; + print "Enter a number ( end to end ) :\n" ; + my $number = <STDIN> ; + chomp $number ; + while ( $number ne "end" ) { + if ( $number =~ /\d+/ ) { + push( @numbers, $number ) ; + } + print "Enter a number ( end to end ) :\n" ; + $number = <STDIN> ; + chomp $number ; + } + return @numbers ; +} + +sub findSmaller { + my $a = shift ; + my $b = shift ; + if ( $a < $b ) { + return $a ; + } + else { + return $b ; + } +} + +my @array = enterArray( ) ; +my $len = scalar @array ; +my $drops = 0 ; +my $oldMax ; +my $oldMaxIndex ; +my $newMax ; +my $newMaxIndex ; +my $current = 0 ; +while ( $current < $len - 1 ) { + if ( $array[ $current + 1 ] < $array[ $current ] ) { + $oldMax = $array[ $current ] ; + $oldMaxIndex = $current ; + } + if ( $array[ $current + 1 ] > $array[ $current ] ) { + $newMax = $array[ $current + 1 ] ; + $newMaxIndex = $current + 1 ; + my $smaller = findSmaller( $oldMax , $newMax ) ; + map { $drops += $smaller - $_ ; $_ = $smaller } + @array[$oldMaxIndex + 1 .. $newMaxIndex - 1] ; + } + $current++ ; +} +say $drops ; diff --git a/challenge-079/ulrich-rieke/raku/ch-1.raku b/challenge-079/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..980bed8eef --- /dev/null +++ b/challenge-079/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,18 @@ +use v6 ; + +sub countOnes( Str $num ) { + my $ones = 0 ; + my $len = $num.chars ; + for ( 0 .. $len - 1 ) -> $i { + if ( $num.substr( $i , 1 ) eq "1" ) { + $ones++ ; + } + } + return $ones ; +} + +sub MAIN( Int $N ) { + my @array = (1 ..$N).map( {.base(2).Str} ) ; + my $ones = [+] @array.map( { countOnes( $_ ) } ) ; + say "$ones % 1000000007 = { $ones % 1000000007 }" ; +} diff --git a/challenge-079/ulrich-rieke/raku/ch-2.raku b/challenge-079/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..aeb88b8dcf --- /dev/null +++ b/challenge-079/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,44 @@ +use v6 ; + +sub enterArray( ) { + my @array ; + my $line = prompt "Enter an integer ( -1 to end ) : " ; + while ( $line != -1 ) { + @array.push( $line ) ; + $line = prompt "Next number : " ; + } + return @array ; +} + +sub findSmaller( Int $a , Int $b --> Int ) { + if ( $a > $b ) { + return $b ; + } + else { + return $a ; + } +} + +my @array = enterArray( ) ; +my $drops = 0 ; +my $oldMax ; #for a local number bigger than its right neighbour +my $oldMaxIndex ; #index of $oldMax in @array +my $newMax ; #for a local number bigger than its left neighbour +my $newMaxIndex ; #index of $newMax in @array +my $currentIndex = 0 ; +my $len = @array.elems ; +while ( $currentIndex < $len - 1) { + if ( @array[$currentIndex + 1] < @array[$currentIndex] ) { + $oldMax = @array[ $currentIndex ] ; + $oldMaxIndex = $currentIndex ; + } + if ( @array[ $currentIndex + 1 ] > @array[ $currentIndex ] ) { + $newMax = @array[ $currentIndex + 1 ] ; + $newMaxIndex = $currentIndex + 1 ; + my $smaller = findSmaller( $oldMax, $newMax ) ; + @array[ $oldMaxIndex + 1 .. $newMaxIndex - 1].map: { $drops += + $smaller - $_ ; $_ = $smaller } ; + } + $currentIndex++ ; +} +say $drops ; |
