aboutsummaryrefslogtreecommitdiff
path: root/challenge-183
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-09-21 13:19:56 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-09-21 13:19:56 +0100
commit06903e154012dd6da0cedbc803d702fea9334492 (patch)
tree16e5bf291d393469f70af7ac5a053fba0063e998 /challenge-183
parente66131f9e712f8da717bb0694dd5b67f06424666 (diff)
downloadperlweeklychallenge-club-06903e154012dd6da0cedbc803d702fea9334492.tar.gz
perlweeklychallenge-club-06903e154012dd6da0cedbc803d702fea9334492.tar.bz2
perlweeklychallenge-club-06903e154012dd6da0cedbc803d702fea9334492.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-183')
-rw-r--r--challenge-183/ulrich-rieke/cpp/ch-1.cpp34
-rw-r--r--challenge-183/ulrich-rieke/haskell/ch-1.hs14
-rw-r--r--challenge-183/ulrich-rieke/haskell/ch-2.hs63
-rw-r--r--challenge-183/ulrich-rieke/perl/ch-1.pl33
-rw-r--r--challenge-183/ulrich-rieke/perl/ch-2.pl83
-rw-r--r--challenge-183/ulrich-rieke/raku/ch-1.raku26
-rw-r--r--challenge-183/ulrich-rieke/raku/ch-2.raku59
7 files changed, 312 insertions, 0 deletions
diff --git a/challenge-183/ulrich-rieke/cpp/ch-1.cpp b/challenge-183/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..b6496b0a06
--- /dev/null
+++ b/challenge-183/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,34 @@
+#include <utility>
+#include <algorithm>
+#include <vector>
+#include <iostream>
+#include <set>
+
+int main( ) {
+ std::cout << "Enter some numbers sequentially in pairs ( 0 to end )!\n" ;
+ std::pair<int , int> input ;
+ std::vector<std::pair<int , int>> allInput ;
+ std::set<std::pair<int , int>> seenSoFar ;
+ int a ;
+ int b ;
+ std::cin >> a ;
+ std::cout << "second number?\n" ;
+ std::cin >> b ;
+ while ( b != 0 ) {
+ input = std::make_pair( a , b ) ;
+ allInput.push_back( input ) ;
+ std::cout << "First integer:\n" ;
+ std::cin >> a ;
+ std::cout << "second integer ( 0 to end ):\n" ;
+ std::cin >> b ;
+ }
+ std::cout << '[' ;
+ for ( auto it = allInput.begin( ) ; it != allInput.end( ) ; ++it ) {
+ if ( seenSoFar.find( *it ) == seenSoFar.end( ) ) {
+ std::cout << '[' << it->first << ',' << it->second << "]," ;
+ seenSoFar.insert( *it ) ;
+ }
+ }
+ std::cout << ']' << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-183/ulrich-rieke/haskell/ch-1.hs b/challenge-183/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..6284954694
--- /dev/null
+++ b/challenge-183/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,14 @@
+{-#LANGUAGE ScopedTypeVariables #-}
+module Challenge183
+ where
+import Data.List.Split ( chunksOf )
+import qualified Data.Set as S
+
+main :: IO ( )
+main = do
+ putStrLn "Enter an even number of integers!"
+ input <- getLine
+ let (numbers :: [Int]) = map read $ words input
+ pairs = chunksOf 2 numbers
+ uniquePairs = S.toList $ S.fromList pairs
+ print uniquePairs
diff --git a/challenge-183/ulrich-rieke/haskell/ch-2.hs b/challenge-183/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..52fda918a6
--- /dev/null
+++ b/challenge-183/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,63 @@
+module Challenge183_2
+ where
+import Data.Time.Calendar
+
+parseDate :: String -> Day
+parseDate s = fromGregorian ( myFirst $ extractFieldsOfDate s ) ( mySecond $
+extractFieldsOfDate s ) ( myThird $ extractFieldsOfDate s )
+where
+ myFirst :: ( Integer , Int , Int ) -> Integer
+ myFirst ( a , _ , _ ) = a
+ mySecond :: ( Integer , Int , Int ) -> Int
+ mySecond ( _ , b , _ ) = b
+ myThird :: ( Integer , Int , Int ) -> Int
+ myThird ( _ , _ , c ) = c
+
+extractFieldsOfDate :: String -> ( Integer , Int , Int )
+extractFieldsOfDate s = ( toInteger $ read $ take 4 s , read $ take 2 $ drop 5 s ,
+read $ drop 8 s )
+
+findDayDifference :: String -> String -> Integer
+findDayDifference startDate endDate = diffDays end start - toBeSubtracted
+where
+ start :: Day
+ start = parseDate startDate
+ end :: Day
+ end = parseDate endDate
+ (year1 , month1 , day1 ) = extractFieldsOfDate startDate
+ (year2 , month2 , day2 ) = extractFieldsOfDate endDate
+ toBeSubtracted :: Integer
+ toBeSubtracted = if ( year2 - year1) == 0 then 0 else findStartCondition
+ startDate + findEndCondition endDate + leapsInBetween
+ where
+ leapsInBetween :: Integer
+ leapsInBetween = toInteger $ length $ filter isLeapYear $ init $
+ tail [year1 ..year2]
+ findStartCondition :: String -> Integer
+ findStartCondition date = if isLeapYear theYear && theMonth == 1 && theDay == 1
+ then 1 else 0
+ where
+ (theYear , theMonth , theDay ) = extractFieldsOfDate date
+ findEndCondition :: String -> Integer
+ findEndCondition date = if isLeapYear myYear && myMonth == 12 &&
+ myDay == 31 then 1 else 0
+ where
+ (myYear , myMonth , myDay ) = extractFieldsOfDate date
+
+findAnswer :: Integer -> String
+findAnswer days =
+ let years = div days 365
+ theDays = days - years * 365
+ yearPart = if years == 0 then "" else if years == 1 then "1 year "
+ else ( show years ++ " years " )
+ dayPart = if theDays == 0 then "" else if theDays == 1 then "1 day" else
+ (show theDays ++ " days" )
+ in yearPart ++ dayPart
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a start date as yyyy-mm-dd!"
+ start <- getLine
+ putStrLn "Enter an end date as yyyy-mm-dd!"
+ end <- getLine
+ putStrLn $ findAnswer $ findDayDifference start end
diff --git a/challenge-183/ulrich-rieke/perl/ch-1.pl b/challenge-183/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..204ccf2831
--- /dev/null
+++ b/challenge-183/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter an even number of integers!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+say join( ',' , @numbers ) ;
+while ( scalar( @numbers ) % 2 != 0 ) {
+ say "For the purposes of this task enter an even number of integers!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ @numbers = split( /\s+/ , $line ) ;
+}
+my @list ;
+my @solution ;
+while ( @numbers ) {
+ my $first = shift @numbers ;
+ my $second = shift @numbers ;
+ my $pair = [$first , $second] ;
+ push @list , $pair ;
+}
+my %seen ;
+print "[" ;
+for my $p ( @list ) {
+ unless ( exists $seen{ "$p->[0],$p->[1]" } ) {
+ print "[" . $p->[0] . ',' . $p->[1] . "]" ;
+ }
+ $seen{ "$p->[0],$p->[1]" }++ ;
+}
+say "]" ;
diff --git a/challenge-183/ulrich-rieke/perl/ch-2.pl b/challenge-183/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..bb21aae7b4
--- /dev/null
+++ b/challenge-183/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Date::Calc qw ( Delta_Days leap_year ) ;
+
+sub formatOutput {
+ my $value = shift ;
+ my $unit = shift ;
+ if ( $value == 1 ) {
+ return "1 $unit" ;
+ }
+ if ( $value > 1 ) {
+ return "$value $unit" . "s" ;
+ }
+}
+
+say "Please enter a start date in the form yyyy-mm-dd!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my ( $year1 , $month1 , $days1 ) ;
+my ( $year2 , $month2 , $days2 ) ;
+while ( $line !~ /\A(\d{4})-(\d{2})-(\d{2})\z/ ) {
+ say "Enter the date as yyyy-mm-dd!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+if ( $line =~ /\A(\d{4})-(\d{2})-(\d{2})\z/ ) {
+ $year1 = $1 ;
+ $month1 = $2 ;
+ $days1 = $3 ;
+}
+say "Please enter an end date in the form yyyy-mm-dd!" ;
+$line = <STDIN> ;
+chomp $line ;
+while ( $line !~ /\A(\d{4})\-(\d{2})\-(\d{2})\z/ ) {
+ say "Enter the date as yyyy-mm-dd!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+if ( $line =~ /\A(\d{4})-(\d{2})-(\d{2})\z/ ) {
+ $year2 = $1 ;
+ $month2 = $2 ;
+ $days2 = $3 ;
+}
+my $toBeSubtracted = 0 ;
+if ( $year2 - $year1 >= 1 ) {
+ if ( $year2 - $year1 == 1 ) {
+ if ( leap_year( $year1 )) {
+ if ( $month1 == 1 && $days1 == 1 ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ if ( leap_year( $year2 )) {
+ if ( $month2 == 12 && $days2 == 31 ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ }
+ if ( leap_year( $year2 ) ) {
+ if ( $month2 == 31 && $days2 == 31 ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ if ( ($year2 - $year1 ) > 1 ) {
+ for my $y ( $year1 + 1 .. $year2 - 1 ) {
+ if ( leap_year( $y ) ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ }
+}
+my $diffDays = Delta_Days( $year1 , $month1 , $days1 , $year2 , $month2 ,
+ $days2 ) ;
+$diffDays -= $toBeSubtracted ;
+my $ys = int( $diffDays / 365 ) ;
+my $ds = $diffDays - $ys * 365 ;
+if ( $ys > 0 ) {
+ say formatOutput( $ys , "year" ) . " " . formatOutput( $ds , "day" ) ;
+}
+else {
+ say formatOutput( $ds , "day" ) ;
+}
diff --git a/challenge-183/ulrich-rieke/raku/ch-1.raku b/challenge-183/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..852cc34378
--- /dev/null
+++ b/challenge-183/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,26 @@
+use v6 ;
+
+say "Please enter an even number of integers, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.split( /\s+/ ).map( {.Int} ) ;
+unless ( @numbers.elems %% 2 ) {
+ say "For the purposes of this task , enter an even number of integers!" ;
+ $line = $*IN.get ;
+ @numbers = $line.split( /\s+/ ).map( {.Int} ) ;
+}
+my @list ;
+my %seen ;
+while ( @numbers ) {
+ my $a = shift @numbers ;
+ my $b = shift @numbers ;
+ my $pair = [ $a , $b ] ;
+ @list.push( $pair ) ;
+}
+print "[" ;
+for @list -> $p {
+ unless ( %seen{ "$p[0],$p[1]" }:exists ) {
+ print "[" ~ $p[0] ~ "," ~ $p[1] ~ "]" ;
+ }
+ %seen{ "$p[0],$p[1]" }++ ;
+}
+say "]" ;
diff --git a/challenge-183/ulrich-rieke/raku/ch-2.raku b/challenge-183/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..04361d6cf2
--- /dev/null
+++ b/challenge-183/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,59 @@
+use v6 ;
+
+sub format( $var , Str $unit --> Str ) {
+ if ( $var == 0 ) {
+ return "" ;
+ }
+ if ( $var == 1 ) {
+ return "1 $unit" ;
+ }
+ if ( $var > 1 ) {
+ return ( "$var $unit" ~ "s" ) ;
+ }
+}
+
+say "Enter a first date as yyyy-mm-dd!" ;
+my $line = $*IN.get ;
+while ( $line !~~ /^(\d ** 4)'-'(\d ** 2)'-'(\d ** 2 )$/ ) {
+ say "Please enter date as yyyy-mm-dd!" ;
+ $line = $*IN.get ;
+}
+my $date1 = Date.new( +$0 , +$1 , +$2 ) ;
+say "Enter a second date as yyyy-mm-dd!" ;
+$line = $*IN.get ;
+while ( $line !~~ /^(\d ** 4)'-'(\d ** 2)'-'(\d ** 2 )$/ ) {
+ say "Please enter date as yyyy-mm-dd!" ;
+ $line = $*IN.get ;
+}
+my $date2 = Date.new( +$0 , +$1 , +$2 ) ;
+#for every full leap year we have to count
+#1 year as 366 days! So we have to subtract the number of leap years
+#between the 2 days from the number of days between the dates
+my $toBeSubtracted = 0 ; #days we have to subtract for full leap years
+if ( $date2.year( ) - $date1.year( ) >= 1 ) {
+ if ( $date1.is-leap-year( ) ) {
+ if ( $date1.month() == 1 && $date1.day( ) == 1 ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ if ( $date2.is-leap-year( ) ) {
+ if ( $date2.month( ) == 12 && $date2.day( ) == 31 ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ if ( $date2.year( ) - $date1.year( ) > 1 ) {
+ my @yearsCovered = ($date1.year( ) + 1 .. $date2.year( ) - 1 ) ;
+ for @yearsCovered -> $y {
+ my $dt = Date.new( "$y-01-01" ) ;
+ if ( $dt.is-leap-year( ) ) {
+ $toBeSubtracted++ ;
+ }
+ }
+ }
+}
+my $diffDays = $date2 - $date1 ;
+$diffDays -= $toBeSubtracted ;
+my $years = $diffDays div 365 ;
+my $days = $diffDays - $years * 365 ;
+print( format( $years , "year" ) ) ;
+say( " " ~ format( $days , "day") ) ;