diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 22:30:37 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 22:30:37 +0100 |
| commit | fecf841be0c99e005e24b74793ddfcf6a2f173c5 (patch) | |
| tree | 548720f6fc6c183884959fc34615ea300ae3c735 /challenge-080 | |
| parent | fd6e597b96c1700fd86aa0e50ee16dc87fc8760f (diff) | |
| download | perlweeklychallenge-club-fecf841be0c99e005e24b74793ddfcf6a2f173c5.tar.gz perlweeklychallenge-club-fecf841be0c99e005e24b74793ddfcf6a2f173c5.tar.bz2 perlweeklychallenge-club-fecf841be0c99e005e24b74793ddfcf6a2f173c5.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-080')
| -rw-r--r-- | challenge-080/ulrich-rieke/cpp/ch-1.cpp | 35 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/cpp/ch-2.cpp | 30 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/haskell/ch-1.hs | 9 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/haskell/ch-2.hs | 7 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/raku/ch-1.raku | 21 | ||||
| -rw-r--r-- | challenge-080/ulrich-rieke/raku/ch-2.raku | 30 |
8 files changed, 204 insertions, 0 deletions
diff --git a/challenge-080/ulrich-rieke/cpp/ch-1.cpp b/challenge-080/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..c4f51b0787 --- /dev/null +++ b/challenge-080/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,35 @@ +#include <vector> +#include <iostream> +#include <algorithm> + +std::vector<int> enterArray ( int limit ) { + std::vector<int> numbers ; + int num ; + while ( numbers.size( ) < limit ) { + std::cin >> num ; + numbers.push_back( num ) ; + } + return numbers ; +} + +int main( ) { + std::cout << "How many numbers do you want to enter ?\n" ; + int num ; + std::cin >> num ; + std::cout << "Enter " << num << " numbers!\n" ; + std::vector<int> numbers = enterArray( num ) ; + std::vector<int> positives ; + for ( int i : numbers ) { + if ( i > 0 ) + positives.push_back( i ) ; + } + std::sort( positives.begin( ) , positives.end( ) ) ; + int current = 1 ; + auto found = std::find( positives.begin( ) , positives.end( ) , current ) ; + while ( found != positives.end( ) ) { + current++ ; + found = std::find( positives.begin( ) , positives.end( ) , current ) ; + } + std::cout << current << std::endl ; + return 0 ; +} diff --git a/challenge-080/ulrich-rieke/cpp/ch-2.cpp b/challenge-080/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..42ad98efcb --- /dev/null +++ b/challenge-080/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,30 @@ +#include <vector> +#include <iostream> + +std::vector<int> enterArray ( int limit ) { + std::vector<int> numbers ; + int num ; + while ( numbers.size( ) < limit ) { + std::cin >> num ; + numbers.push_back( num ) ; + } + return numbers ; +} + +int main( ) { + int limit ; + std::cout << "How many numbers do you want to enter ?\n" ; + std::cin >> limit ; + std::cout << "Enter " << limit << " numbers!\n" ; + std::vector<int> numbers = enterArray( limit ) ; + int len = numbers.size( ) ; + int candies = len ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + if ( numbers[ i ] != numbers[ i + 1 ] ) { + candies++ ; + } + } + std::cout << std::endl ; + std::cout << candies << std::endl ; + return 0 ; +} diff --git a/challenge-080/ulrich-rieke/haskell/ch-1.hs b/challenge-080/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..667bf18c5f --- /dev/null +++ b/challenge-080/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,9 @@ +module Challenge080 + where +import Data.List ( sort ) + +solution :: [Int] -> Int +solution list = head $ filter (\i -> not $ elem i theList ) [1 , 2 ..] + where + theList :: [Int] + theList = sort $ filter ( 0 < ) list diff --git a/challenge-080/ulrich-rieke/haskell/ch-2.hs b/challenge-080/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..5d7e878a95 --- /dev/null +++ b/challenge-080/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,7 @@ +module Challenge080_2 + where +import Data.List.Split ( divvy ) + +solution :: [Int] -> Int +solution list = length list + ( length $ filter (\li -> head li /= last li ) +$ divvy 2 1 list ) diff --git a/challenge-080/ulrich-rieke/perl/ch-1.pl b/challenge-080/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..ec5467de17 --- /dev/null +++ b/challenge-080/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub enterArray { + my @array ; + say "Enter number ( 'e' to end )!" ; + my $line = <STDIN> ; + chomp $line ; + while ( $line ne 'e' ) { + if ( $line =~ /\b[-]*\d+\b/ ) { + push( @array , $line ) ; + } + say "Next number :" ; + $line = <STDIN> ; + chomp $line ; + } + return @array ; +} + +my @array = enterArray( ) ; +my @positives = grep { $_ > 0 } @array ; +my %numhash ; +for my $num ( @positives ) { + $numhash{$num}++ ; +} +my $current = 1 ; +while ( exists $numhash{ $current } ) { + $current++ ; +} +say $current ; diff --git a/challenge-080/ulrich-rieke/perl/ch-2.pl b/challenge-080/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..1af98dbb20 --- /dev/null +++ b/challenge-080/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub enterArray { + my @array ; + say "Enter number ( 'e' to end )!" ; + my $line = <STDIN> ; + chomp $line ; + while ( $line ne 'e' ) { + if ( $line =~ /\b[-]*\d+\b/ ) { + push( @array , $line ) ; + } + say "Next number :" ; + $line = <STDIN> ; + chomp $line ; + } + return @array ; +} + +my @array = enterArray( ) ; +my $len = scalar @array ; +my $candies = $len ; +for my $i ( 0 .. $len - 1 ) { + if ( $i < $len - 1 ) { + if ( $array[ $i ] > $array[ $i + 1 ] ) { + $candies++ ; + } + + } + if ( $i > 0 ) { + if ( $array[ $i ] > $array[ $i - 1 ] ) { + $candies++ ; + } + } +} +say " " ; +say $candies ; + diff --git a/challenge-080/ulrich-rieke/raku/ch-1.raku b/challenge-080/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..7fd2914d20 --- /dev/null +++ b/challenge-080/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,21 @@ +use v6 ; + +sub enterArray( ) { + my @array ; + say "Enter numbers ( 'e' to end ) " ; + my $line = $*IN.get ; + while ( $line ne 'e' ) { + @array.push( +$line ) ; + say "next number :" ; + $line = $*IN.get ; + } + return @array ; +} + +my @array = enterArray( ) ; +my $positiveSet = @array.grep( { $_ > 0 } ).Set ; +my $current = 1 ; +while ( $current (elem) $positiveSet ) { + $current++ ; +} +say $current ; diff --git a/challenge-080/ulrich-rieke/raku/ch-2.raku b/challenge-080/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..249b8c624e --- /dev/null +++ b/challenge-080/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,30 @@ +use v6 ; + +sub enterArray( ) { + my @array ; + say "Enter numbers ( 'e' to end ) " ; + my $line = $*IN.get ; + while ( $line ne 'e' ) { + @array.push( +$line ) ; + say "next number :" ; + $line = $*IN.get ; + } + return @array ; +} + +my @array = enterArray( ) ; +my $len = @array.elems ; +my $candies = $len ; +for ( 0 .. $len - 1 ) -> $i { + if ( $i < $len - 1 ) { + if ( @array[ $i ] > @array[ $i + 1 ] ) { + $candies++ ; + } + } + if ( $i > 0 ) { + if ( @array[ $i ] > @array[ $i - 1 ] ) { + $candies++ ; + } + } +} +say $candies ; |
