diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-28 00:14:47 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-28 00:14:47 +0100 |
| commit | b5f241b838e9c5bb1016bd6ccefa85ca3a1689b4 (patch) | |
| tree | 4a93ce8c8fa43ff9319abe2e792cfadb65d45a08 /challenge-123 | |
| parent | 5c96f84c63dc0f64961f531eadb152e4b822f219 (diff) | |
| download | perlweeklychallenge-club-b5f241b838e9c5bb1016bd6ccefa85ca3a1689b4.tar.gz perlweeklychallenge-club-b5f241b838e9c5bb1016bd6ccefa85ca3a1689b4.tar.bz2 perlweeklychallenge-club-b5f241b838e9c5bb1016bd6ccefa85ca3a1689b4.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-123')
| -rw-r--r-- | challenge-123/ulrich-rieke/cpp/ch-1.cpp | 35 | ||||
| -rw-r--r-- | challenge-123/ulrich-rieke/cpp/ch-2.cpp | 45 | ||||
| -rw-r--r-- | challenge-123/ulrich-rieke/haskell/ch-2.hs | 26 | ||||
| -rw-r--r-- | challenge-123/ulrich-rieke/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-123/ulrich-rieke/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-123/ulrich-rieke/raku/ch-1.raku | 31 | ||||
| -rw-r--r-- | challenge-123/ulrich-rieke/raku/ch-2.raku | 31 |
7 files changed, 251 insertions, 0 deletions
diff --git a/challenge-123/ulrich-rieke/cpp/ch-1.cpp b/challenge-123/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..af79006582 --- /dev/null +++ b/challenge-123/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,35 @@ +#include <iostream> +#include <cmath> +#include <set> +#include <algorithm> +#include <cstdlib> + +bool isUgly( int n , const std::set<int> firstPrimes ) { + std::set<int> divisors ; + int current = 2 ; + while ( n > 1 ) { + if ( n % current == 0 ) { + divisors.insert( current ) ; + n /= current ; + } + else + current++ ; + } + return std::all_of( divisors.begin( ) , divisors.end( ) , + [&firstPrimes]( int i ) { return firstPrimes.find( i ) != + firstPrimes.end( ) ; } ) ; +} + +int main( int argc , char * argv[ ] ) { + int n = std::atoi( argv[ 1 ] ) ; + std::set<int> somePrimes { 2 , 3 , 5 } ; + int current = 1 ; + int sumUglies = 1 ; + while ( sumUglies < n ) { + current++ ; + if ( isUgly( current , somePrimes ) ) + sumUglies++ ; + } + std::cout << current << std::endl ; + return 0 ; +} diff --git a/challenge-123/ulrich-rieke/cpp/ch-2.cpp b/challenge-123/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..c3ab8c3c04 --- /dev/null +++ b/challenge-123/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,45 @@ +#include <string> +#include <iostream> +#include <utility> +#include <vector> +#include <cmath> +#include <set> +using Point = std::pair<int, int> ; + +double computeDistance( const Point & p1 , const Point & p2 ) { + return std::sqrt( std::pow( static_cast<double>(p2.first - p1.first) , + 2.0) + std::pow( static_cast<double>(p2.second - p1.second) , + 2.0 ) ) ; +} + +int main( ) { + std::vector<Point> points ; + int num = 0 ; + for ( int i = 0 ; i < 4 ; i++ ) { + Point p { 0 , 0 } ; + std::cout << "For " << i + 1 << ". point, enter x: \n" ; + std::cin >> num ; + p.first = num ; + std::cout << " and y: \n" ; + std::cin >> num ; + p.second = num ; + points.push_back( p ) ; + } + std::set<double> distances ; + for ( int i = 0 ; i < 3 ; i++ ) { + distances.insert( computeDistance( points[ i ] , points[ i++ ] )) ; + } + distances.insert( computeDistance( points[ 0 ] , points[ 3 ] )) ; + std::set<double> diagonals ; + diagonals.insert( computeDistance( points[ 0 ] , points[ 2 ] )) ; + diagonals.insert( computeDistance( points[ 1 ] , points[ 3 ] )) ; + if ( distances.size( ) == 1 && diagonals.size( ) == 1 ) { + std::cout << std::endl ; + std::cout << 1 << std::endl ; + } + else { + std::cout << std::endl ; + std::cout << 0 << std::endl ; + } + return 0 ; +} diff --git a/challenge-123/ulrich-rieke/haskell/ch-2.hs b/challenge-123/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..089186becf --- /dev/null +++ b/challenge-123/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,26 @@ +module Challenge123_2 + where +import GHC.Float +import qualified Data.Set as S +import Data.List ( (!!) ) + +findDistance :: (Int, Int) -> (Int , Int) -> Double +findDistance p1 p2 = float2Double $ sqrt +((fromIntegral ((fst p2 - fst p1) ^ 2 )) + (fromIntegral +((snd p2 - snd p1 ) ^ 2))) + +allDistances :: [(Int , Int)] -> [Double] +allDistances points = map (\i -> findDistance ( points !! i ) +( points !! (i + 1) ) ) [0 .. 2] ++ [findDistance ( points !! 0 ) + (points !! 3 )] + +allDiagonals :: [(Int , Int)] -> [Double] +allDiagonals points = [ findDistance ( points !! 0 ) ( points !! 2 ) , +findDistance ( points !! 1 ) ( points !! 3 ) ] + +solution :: [(Int , Int)] -> Int +solution points = if myCondition then 1 else 0 +where + myCondition :: Bool + myCondition = ((length $ S.toList $ S.fromList $ allDistances points) == 1) + && (( length $ S.toList $ S.fromList $ allDiagonals points ) == 1) diff --git a/challenge-123/ulrich-rieke/perl/ch-1.pl b/challenge-123/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..6b23273aed --- /dev/null +++ b/challenge-123/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +#we keep dividing the number passed as argument until it is equal to 1 +#we start with the divisor 2 +#in the end we check if only 2 , 3 or 5 occurred as divisors +#after joining the divisors into a string we apply a regular expression +#to it +sub isUgly { + my $num = shift ; + my %divisors ; + my $current = 2 ; + while ( $num > 1 ) { + if ( ($num % $current) == 0 ) { + $divisors{$current}++ ; + $num /= $current ; + } + else { + $current++ ; + } + } + my $str = join( '' , sort { $a <=> $b } keys %divisors ) ; + return ( $str =~ /\b[235]+\b/ ) ; +} + +my $n = $ARGV[ 0 ] ; +my $current = 1 ; #1 assumed to be an ugly number +my $sumUglies = 1 ;#see above +while ( $sumUglies < $n ) { + $current++ ; + if (isUgly ( $current ) ) { + $sumUglies++ ; + } +} +say $current ; diff --git a/challenge-123/ulrich-rieke/perl/ch-2.pl b/challenge-123/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..b0d1c86ef5 --- /dev/null +++ b/challenge-123/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +#distance of 2 points +sub computeDistance { + my $point1 = shift ; + my $point2 = shift ; + my @coordinates1 = @$point1 ; + my @coordinates2 = @$point2 ; + return sqrt( (($coordinates2[0] - $coordinates1[0]) ** 2 ) + + ((($coordinates2[1] - $coordinates1[1] ) ** 2) )) ; +} + +my @points ; +say "Enter point coordinates ( numbers , separated by blanks )!" ; +for my $i ( 1 .. 4 ) { + say "Enter $i. point!" ; + my $line = <STDIN> ; + chomp $line ; + my @point = split( /\s+/ , $line ) ; + push @points , \@point ; +} +my %distances ; +#it is required that all neighbouring points have the same distance +for my $i (0 .. 2) { + my $distance = computeDistance( $points[ $i ] , $points[ $i + 1 ] ) ; + $distances{ $distance }++ ; +} +my $distance = computeDistance( $points[0] , $points[ 3 ] ) ; +$distances{ $distance }++ ; +#it is required that the diagonals have the same length +my %diagonals ; +$distance = computeDistance( $points[ 0 ] , $points[ 2 ] ) ; +$diagonals{ $distance }++ ; +$distance = computeDistance( $points[ 1 ] , $points[ 3 ] ) ; +$diagonals{ $distance }++ ; +#there should be only one length in the %distances and %diagonals hashes +if ( (scalar( keys %distances ) == 1 ) && ( scalar( keys %diagonals ) + == 1 )) { + say 1 ; +} +else { + say 0 ; +} diff --git a/challenge-123/ulrich-rieke/raku/ch-1.raku b/challenge-123/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..539435eaca --- /dev/null +++ b/challenge-123/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,31 @@ +use v6 ; + +#find all divisors starting with 2, keep dividing until the number +#passed as argument is equal to 1 +sub isUgly( Int $N is copy --> Bool ) { + my @divisors ; + my $current = 2 ; + while ( $N > 1 ) { + if ( $N %% $current ) { + @divisors.push( $current ) ; + $N div= $current ; + } + else { + $current++ ; + } + } + return @divisors.Set (<=) (2 , 3 , 5 ).Set ; +} + +sub MAIN( Int $n ) { + my $current = 1 ; + my $sumUglies = 1 ;#we consider 1 an ugly number , so we start with 1 +#ugly number + while ( $sumUglies < $n ) { + $current++ ; + if ( isUgly( $current ) ) { + $sumUglies++ ; + } + } + say $current ; +} diff --git a/challenge-123/ulrich-rieke/raku/ch-2.raku b/challenge-123/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..aefc150fe3 --- /dev/null +++ b/challenge-123/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,31 @@ +use v6 ; + +sub computeDistance( @point1 , @point2 --> Num ) { + return sqrt ( (( @point2[0] - @point1[ 0 ]) ** 2) + (( @point2[1] - + @point1[1] ) ** 2) ) ; +} + +my @points ; +say "Enter the coordinates of four points( blank between x and y! ) ! " ; +for (1 .. 4 ) -> $i { + say "Enter $i. point:" ; + my $line = $*IN.get ; + my @point = $line.split( /\s+/ ) ; + @points.push( @point ) ; +} +#we know it is a square if the differences of all pairwise neighbours are +#the same and if the diagonals have the same length +my @distances ; +for (1 .. 3 ) -> $i { + @distances.push( computeDistance( @points[ $i - 1 ] , @points[ $i ] ) ) ; +} +@distances.push( computeDistance( @points[ 3 ] , @points[ 0 ] )) ; +my @diagonals ; +@diagonals.push( computeDistance( @points[ 0 ] , @points[ 2 ] ) ) ; +@diagonals.push( computeDistance( @points[ 1 ] , @points[ 3 ] ) ) ; +if ( @distances.unique.elems == 1 && @diagonals.unique.elems == 1 ) { + say 1 ; +} +else { + say 0 ; +} |
