diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-11-15 21:58:16 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-11-15 21:58:16 +0000 |
| commit | 46bef5068ef2a61feb02ee68942af6cb9b1bbafe (patch) | |
| tree | 152fd486af72f4fb4faa5ce11b4911f96ab9d6cc /challenge-243 | |
| parent | 98ed69a96a5be7e2e3da225d28af307323aaa4c0 (diff) | |
| download | perlweeklychallenge-club-46bef5068ef2a61feb02ee68942af6cb9b1bbafe.tar.gz perlweeklychallenge-club-46bef5068ef2a61feb02ee68942af6cb9b1bbafe.tar.bz2 perlweeklychallenge-club-46bef5068ef2a61feb02ee68942af6cb9b1bbafe.zip | |
- Added solutions by E. Choroba.
- Added solutions by Dave Jacoby.
- Added solutions by Matthew Neleigh.
- Added solutions by Luca Ferrari.
- Added solutions by Roger Bell_West.
- Added solutions by Mariano Spadacinni.
- Added solutions by Bob Lied.
- Added solutions by Peter Meszaros.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Robbie Hatley.
- Added solutions by Athanasius.
- Added solutions by Ali Moradi.
- Added solutions by Clifton Wood.
- Added solutions by Ulrich Rieke.
- Added solutions by Peter Campbell Smith.
Diffstat (limited to 'challenge-243')
| -rw-r--r-- | challenge-243/clifton-wood/raku/ch-1.raku | 16 | ||||
| -rw-r--r-- | challenge-243/clifton-wood/raku/ch-2.raku | 6 | ||||
| -rwxr-xr-x | challenge-243/eric-cheung/python/ch-1.py | 18 | ||||
| -rwxr-xr-x | challenge-243/eric-cheung/python/ch-2.py | 13 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/cpp/ch-1.cpp | 36 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/cpp/ch-2.cpp | 49 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/haskell/ch-1.hs | 17 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/haskell/ch-2.hs | 25 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/perl/ch-2.pl | 29 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/raku/ch-1.raku | 15 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/raku/ch-2.raku | 24 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/rust/ch-1.rs | 20 | ||||
| -rwxr-xr-x | challenge-243/ulrich-rieke/rust/ch-2.rs | 33 |
14 files changed, 320 insertions, 0 deletions
diff --git a/challenge-243/clifton-wood/raku/ch-1.raku b/challenge-243/clifton-wood/raku/ch-1.raku new file mode 100644 index 0000000000..9c87a02298 --- /dev/null +++ b/challenge-243/clifton-wood/raku/ch-1.raku @@ -0,0 +1,16 @@ +my @p1 = (1, 3, 2, 3, 1); +my @p2 = (2, 4, 3, 5, 1); + +sub reverse-pair (@a) { + #A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j]. + for @a.keys.combinations(2).map( *.sort.cache ).unique.cache { + if @a[ .head ] > 2 * @a[ .tail ] { + say "({ .head }, { .tail }) => num[{ .head }] ({ @a[ .head ] }) = { + @a[.head ] } > 2 * num[{ .tail }] ({ @a[ .tail ] })"; + } + } + say '-' x 40; +} + +reverse-pair( @p1 ); +reverse-pair( @p2 ); diff --git a/challenge-243/clifton-wood/raku/ch-2.raku b/challenge-243/clifton-wood/raku/ch-2.raku new file mode 100644 index 0000000000..399b79d450 --- /dev/null +++ b/challenge-243/clifton-wood/raku/ch-2.raku @@ -0,0 +1,6 @@ +sub floor-sums (*@a) { + (@a X @a).map({ ( .head / .tail ).floor }).sum.say +} + +floor-sums(2, 5, 9); +floor-sums( |(7 xx 7) ); diff --git a/challenge-243/eric-cheung/python/ch-1.py b/challenge-243/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..9ee2534f4b --- /dev/null +++ b/challenge-243/eric-cheung/python/ch-1.py @@ -0,0 +1,18 @@ +
+from itertools import combinations
+
+def IsReversePair (arrInput):
+ if arrInput[0] > 2 * arrInput[1]:
+ return True
+ return False
+
+
+## arrNum = [1, 3, 2, 3, 1] ## Example 1
+arrNum = [2, 4, 3, 5, 1] ## Example 2
+
+
+arrCombList = combinations(arrNum, 2)
+arrOutputList = [arrLoop for arrLoop in list(arrCombList) if IsReversePair(arrLoop)]
+
+
+print (len(arrOutputList))
diff --git a/challenge-243/eric-cheung/python/ch-2.py b/challenge-243/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..8d186c9bf5 --- /dev/null +++ b/challenge-243/eric-cheung/python/ch-2.py @@ -0,0 +1,13 @@ +
+from itertools import combinations
+
+def GetFloorSum (arrInput):
+ return int(arrInput[0] / arrInput[1]) + int(arrInput[1] / arrInput[0])
+
+## arrNum = [2, 5, 9] ## Example 1
+arrNum = [7, 7, 7, 7, 7, 7, 7] ## Example 2
+
+arrCombList = combinations(arrNum, 2)
+nSum = len(arrNum) + sum([GetFloorSum(arrLoop) for arrLoop in list(arrCombList)])
+
+print (nSum)
diff --git a/challenge-243/ulrich-rieke/cpp/ch-1.cpp b/challenge-243/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..52de66f21a --- /dev/null +++ b/challenge-243/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,36 @@ +#include <vector> +#include <iostream> +#include <string> + +std::vector<std::string> split( const std::string & startline , + const std::string & sep ) { + std::vector<std::string> separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr( start , pos - start ) ) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Please enter some integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings { split ( line , " ") } ; + std::vector<int> numbers ; + for ( auto & s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + int sum = 0 ; + int size = numbers.size( ) ; + for ( int i = 0 ; i < size - 1 ; i++ ) { + for ( int j = i + 1 ; j < size ; j++ ) { + if ( numbers[ i ] > 2 * numbers[ j ] ) + sum++ ; + } + } + std::cout << sum << std::endl ; + return 0 ; +} diff --git a/challenge-243/ulrich-rieke/cpp/ch-2.cpp b/challenge-243/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..27a6f46268 --- /dev/null +++ b/challenge-243/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,49 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> + +std::vector<std::string> split( const std::string & startline , + const std::string & sep ) { + std::vector<std::string> separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr( start , pos - start ) ) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Enter an array of positive integers >= 1 , separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings { split( line , " " ) } ; + std::vector<int> numbers ; + for ( auto & s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + int comparedTo { numbers[ 0 ] } ; + int len = numbers.size( ) ; + if ( std::all_of( numbers.begin( ) , numbers.end( ) , [comparedTo]( int i ) { + return i == comparedTo ; } ) ) { + std::cout << comparedTo * len << std::endl ; + } + else { + int sum = 0 ; + for ( int i = 0 ; i < len ; i++ ) { + sum += 1 ; // representing the floor of dividing a number by itself + if ( i < len - 1 ) { + for ( int j = i + 1 ; j < len ; j++ ) { + if ( numbers[ i ] != numbers[ j ] ) { + sum += numbers[ i ] / numbers[ j ] ; + sum += numbers[ j ] / numbers[ i ] ; + } + } + } + } + std::cout << sum << std::endl ; + } + return 0 ; +} diff --git a/challenge-243/ulrich-rieke/haskell/ch-1.hs b/challenge-243/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..7b5dcfab25 --- /dev/null +++ b/challenge-243/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,17 @@ +module Challenge243 + where +import Data.List ( (!!) ) + +findGreater :: [Int] -> Int -> Int +findGreater list index = length $ filter (\n -> (list !! index ) > ( 2 * n ) ) + $ drop (index + 1 ) list + +solution :: [Int] -> Int +solution list = sum $ map (\i -> findGreater list i ) [0..length list - 2] + +main :: IO ( ) +main = do + putStrLn "Enter some integers, separated by blanks!" + numberstrings <- getLine + let numbers = map read $ words numberstrings + print $ solution numbers diff --git a/challenge-243/ulrich-rieke/haskell/ch-2.hs b/challenge-243/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..225a4f23b5 --- /dev/null +++ b/challenge-243/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,25 @@ +module Challenge243_2 + where +import Data.List ( (!!) ) + +findPairs :: [Int] -> [(Int , Int) ] +findPairs list = concat $ map (\i -> map (\el -> (list !! i , el )) $ drop ( i + i ) + list ) [0..length list - 2] + +solution :: [Int] -> Int +solution list + |all (\i -> i == h ) list = h * ( length list ) + |otherwise = length list + ( sum $ map (\p -> div (fst p ) ( snd p) + div + ( snd p ) ( fst p ) ) suitablePairs ) + where + h :: Int + h = head list + suitablePairs :: [(Int , Int ) ] + suitablePairs = filter (\pa -> fst pa /= snd pa ) $ findPairs list + +main :: IO ( ) +main = do + putStrLn "Enter some positive integers >= 1 , separated by blanks!" + numberstrings <- getLine + let numbers = map read $ words numberstrings + print $ solution numbers diff --git a/challenge-243/ulrich-rieke/perl/ch-1.pl b/challenge-243/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..825fe2cd05 --- /dev/null +++ b/challenge-243/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $len = scalar( @numbers ) ;
+my $sum = 0 ;
+for my $i ( 0..$len - 2 ) {
+ for my $j ( $i + 1 .. $len - 1 ) {
+ if ( $numbers[ $i ] > 2 * $numbers[ $j ] ) {
+ $sum++ ;
+ }
+ }
+}
+say $sum ;
\ No newline at end of file diff --git a/challenge-243/ulrich-rieke/perl/ch-2.pl b/challenge-243/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..1ebbdc078b --- /dev/null +++ b/challenge-243/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use POSIX ;
+
+say "Enter some integers >= 1 , separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $len = scalar( @numbers ) ;
+if ( scalar( grep { $_ == $numbers[ 0 ] } @numbers ) == $len ) {
+ say ( $numbers[ 0 ] * $len ) ;
+}
+else {
+ my $sum = 0 ;
+ for my $i ( 0..$len - 1 ) {
+ $sum += 1 ; # representing the floor of dividing a number by itself
+ if ( $i < $len - 1 ) {
+ for my $j ( $i + 1 .. $len - 1 ) {
+ if ( $numbers[ $i ] != $numbers[ $j ] ) {
+ $sum += floor( $numbers[ $i ] / $numbers[ $j ] ) ;
+ $sum += floor( $numbers[ $j ] / $numbers[ $i ] ) ;
+ }
+ }
+ }
+ }
+ say $sum ;
+}
\ No newline at end of file diff --git a/challenge-243/ulrich-rieke/raku/ch-1.raku b/challenge-243/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..2a70ebaefc --- /dev/null +++ b/challenge-243/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,15 @@ +use v6 ; + +say "Enter some integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my $sum = 0 ; +my $len = @numbers.elems ; +for (0..$len - 2 ) -> $i { + for ( $i + 1 .. $len - 1 ) -> $j { + if ( @numbers[ $i ] > 2 * @numbers[ $j ] ) { + $sum++ ; + } + } +} +$sum.say ; diff --git a/challenge-243/ulrich-rieke/raku/ch-2.raku b/challenge-243/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..40f762fde9 --- /dev/null +++ b/challenge-243/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,24 @@ +use v6 ; + +say "Enter some integers >= 1, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my $len = @numbers.elems ; +if ( @numbers.grep( { $_ == @numbers[0] } ).elems == $len ) { + say ( @numbers[ 0 ] * $len ) ; +} +else { + my $sum = 0 ; + for (0..$len - 1 ) -> $i { + $sum += @numbers[ $i ] div @numbers[ $i ] ; + if ( $i < $len - 1 ) { + for ( $i + 1 .. $len - 1 ) -> $j { + if ( @numbers[ $i ] != @numbers[ $j ] ) { + $sum += @numbers[ $i ] div @numbers[ $j ] ; + $sum += @numbers[ $j ] div @numbers[ $i ] ; + } + } + } + } + $sum.say ; +} diff --git a/challenge-243/ulrich-rieke/rust/ch-1.rs b/challenge-243/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..2ea91301dc --- /dev/null +++ b/challenge-243/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,20 @@ +use std::io ; + +fn main() { + println!("Enter some integers , separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec<i32> = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::<i32>().unwrap( ) ).collect( ) ; + let len = numbers.len( ) ; + let mut sum : usize = 0 ; + for i in 0..len - 1 { + for j in i + 1..len { + if numbers[ i ] > 2 * numbers[ j ] { + sum += 1 ; + } + } + } + println!("{}" , sum ) ; +} diff --git a/challenge-243/ulrich-rieke/rust/ch-2.rs b/challenge-243/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..3c62208f0e --- /dev/null +++ b/challenge-243/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,33 @@ +use std::io ; + +fn main() { + println!("Enter some integers >= 1 , separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( & mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec<i32> = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::<i32>().unwrap( ) ).collect( ) ; + if numbers.iter( ).all( | n | *n == numbers[ 0 ] ) { + println!("{}" , numbers[ 0 ] * ( numbers.len( ) as i32 ) ) ; + } + else { + let len : usize = numbers.len( ) ; + let mut sum : i32 = 0 ; + for i in 0..len { + sum += numbers[ i ] / numbers[ i ] ; + if i < len - 1 { + for j in i + 1..len { + if numbers[ i ] != numbers[ j ] { + let mut the_floor : i32 = ((numbers[ i ] as f32 ) / ( + numbers[j ] as f32 )).floor( ) as i32 ; + sum += the_floor ; + the_floor = ((numbers[ j ] as f32 ) / ( numbers[ i ] as + f32 )).floor( ) as i32 ; + sum += the_floor ; + } + } + } + } + println!("{}" , sum ) ; + } +} |
