aboutsummaryrefslogtreecommitdiff
path: root/challenge-327
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-06-25 00:03:07 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-06-25 00:03:07 +0100
commitbbfd3a7a06ba665536970cd5d9c9b227b58249a4 (patch)
tree9b6d99430872b269a598888366e839bfe37fa813 /challenge-327
parent3b709faf56a078c78e2d6dd63b8b74716fb6cb30 (diff)
downloadperlweeklychallenge-club-bbfd3a7a06ba665536970cd5d9c9b227b58249a4.tar.gz
perlweeklychallenge-club-bbfd3a7a06ba665536970cd5d9c9b227b58249a4.tar.bz2
perlweeklychallenge-club-bbfd3a7a06ba665536970cd5d9c9b227b58249a4.zip
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke. - Added solutions by Andrew Shitov. - Added solutions by Feng Chang. - Added solutions by PokGoPun. - Added solutions by Niels van Dijke. - Added solutions by Simon Proctor. - Added solutions by Peter Campbell Smith. - Added solutions by David Ferrone. - Added solutions by E. Choroba. - Added solutions by Mark Anderson. - Added solutions by Andreas Mahnke. - Added solutions by Ali Moradi. - Added solutions by Robert Ransbottom. - Added solutions by Ulrich Reining.
Diffstat (limited to 'challenge-327')
-rwxr-xr-xchallenge-327/eric-cheung/python/ch-1.py8
-rwxr-xr-xchallenge-327/eric-cheung/python/ch-2.py25
-rwxr-xr-xchallenge-327/perlboy1967/perl/ch-1.pl (renamed from challenge-327/perlboy1967/perl/ch1.pl)0
-rwxr-xr-xchallenge-327/perlboy1967/perl/ch-2.pl (renamed from challenge-327/perlboy1967/perl/ch2.pl)0
-rwxr-xr-xchallenge-327/ulrich-rieke/cpp/ch-1.cpp35
-rwxr-xr-xchallenge-327/ulrich-rieke/cpp/ch-2.cpp54
-rwxr-xr-xchallenge-327/ulrich-rieke/haskell/ch-1.hs11
-rwxr-xr-xchallenge-327/ulrich-rieke/haskell/ch-2.hs22
-rwxr-xr-xchallenge-327/ulrich-rieke/perl/ch-1.pl14
-rwxr-xr-xchallenge-327/ulrich-rieke/perl/ch-2.pl25
-rwxr-xr-xchallenge-327/ulrich-rieke/raku/ch-1.raku10
-rwxr-xr-xchallenge-327/ulrich-rieke/raku/ch-2.raku9
-rwxr-xr-xchallenge-327/ulrich-rieke/rust/ch-1.rs17
-rwxr-xr-xchallenge-327/ulrich-rieke/rust/ch-2.rs16
14 files changed, 246 insertions, 0 deletions
diff --git a/challenge-327/eric-cheung/python/ch-1.py b/challenge-327/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..6dd6b7b3dc
--- /dev/null
+++ b/challenge-327/eric-cheung/python/ch-1.py
@@ -0,0 +1,8 @@
+
+## arrInts = [1, 2, 1, 3, 2, 5] ## Example 1
+## arrInts = [1, 1, 1] ## Example 2
+arrInts = [2, 2, 1] ## Example 3
+
+arrOutput = [nLoop for nLoop in range(1, len(arrInts) + 1) if not nLoop in arrInts]
+
+print (arrOutput)
diff --git a/challenge-327/eric-cheung/python/ch-2.py b/challenge-327/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..4f5c71f173
--- /dev/null
+++ b/challenge-327/eric-cheung/python/ch-2.py
@@ -0,0 +1,25 @@
+
+from itertools import combinations
+
+## arrInts = [4, 1, 2, 3] ## Example 1
+## arrInts = [1, 3, 7, 11, 15] ## Example 2
+arrInts = [1, 5, 3, 8] ## Example 3
+
+arrCombList = combinations(arrInts, 2)
+
+arrOutput = []
+nMinAbsDiff = max(arrInts) - min(arrInts)
+
+for arrLoop in arrCombList:
+ nAbsDiff = abs(arrLoop[0] - arrLoop[1])
+
+ if nAbsDiff > nMinAbsDiff:
+ continue
+
+ if nAbsDiff < nMinAbsDiff:
+ nMinAbsDiff = nAbsDiff
+ arrOutput = []
+
+ arrOutput.append(sorted(list(arrLoop)))
+
+print (sorted(arrOutput))
diff --git a/challenge-327/perlboy1967/perl/ch1.pl b/challenge-327/perlboy1967/perl/ch-1.pl
index 3cc274747d..3cc274747d 100755
--- a/challenge-327/perlboy1967/perl/ch1.pl
+++ b/challenge-327/perlboy1967/perl/ch-1.pl
diff --git a/challenge-327/perlboy1967/perl/ch2.pl b/challenge-327/perlboy1967/perl/ch-2.pl
index 787a3d3cc0..787a3d3cc0 100755
--- a/challenge-327/perlboy1967/perl/ch2.pl
+++ b/challenge-327/perlboy1967/perl/ch-2.pl
diff --git a/challenge-327/ulrich-rieke/cpp/ch-1.cpp b/challenge-327/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..2c350933db
--- /dev/null
+++ b/challenge-327/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <algorithm>
+#include <vector>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ))
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ int len = numbers.size( ) ;
+ std::vector<int> solution ;
+ for ( int i = 1 ; i < len + 1 ; i++ ) {
+ if ( std::find( numbers.begin( ) , numbers.end( ) , i ) == numbers.end( ) )
+ solution.push_back( i ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : solution )
+ std::cout << i << ' ' ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-327/ulrich-rieke/cpp/ch-2.cpp b/challenge-327/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..f1d614ea9c
--- /dev/null
+++ b/challenge-327/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,54 @@
+#include <vector>
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <algorithm>
+#include <cstdlib>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ))
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+std::vector<std::vector<int>> findCombinations( const std::vector<int> &
+ numbers ) {
+ std::vector<std::vector<int>> combis ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ std::vector<int> aCombi ;
+ aCombi.push_back( numbers[ i ] ) ;
+ for ( int j = i + 1 ; j < len ; j++ ) {
+ aCombi.push_back( numbers[ j ] ) ;
+ combis.push_back( aCombi ) ;
+ aCombi.erase( aCombi.begin( ) + 1 ) ;
+ }
+ }
+ return combis ;
+}
+
+int main( ) {
+ std::cout << "Enter some distinct integers separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ auto allCombis { findCombinations( numbers ) } ;
+ std::vector<int> differences ;
+ for ( auto vec : allCombis ) {
+ differences.push_back( std::abs( vec[0] - vec[1] ) ) ;
+ }
+ int minimum = *std::min_element( differences.begin( ) , differences.end( ) ) ;
+ for ( auto vec : allCombis ) {
+ if ( std::abs( vec[0] - vec[1] ) == minimum ) {
+ std::cout << '[' << vec[0] << ',' << vec[1] << "] " ;
+ }
+ }
+ std::cout << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-327/ulrich-rieke/haskell/ch-1.hs b/challenge-327/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..6e9351891c
--- /dev/null
+++ b/challenge-327/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,11 @@
+module Challenge327
+ where
+
+solution :: [Int] -> [Int]
+solution list = filter (\i -> notElem i list ) [1..length list]
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-327/ulrich-rieke/haskell/ch-2.hs b/challenge-327/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..acd9d6d723
--- /dev/null
+++ b/challenge-327/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,22 @@
+module Challenge327_2
+ where
+
+combinations :: Int -> [a] -> [[a]]
+combinations 0 _ = [[]]
+combinations n xs = [ xs !! i : x | i <- [0..(length xs ) - 1 ] ,
+ x <- combinations (n - 1 ) ( drop ( i + 1 ) xs ) ]
+
+solution :: [Int] ->[[Int]]
+solution numberlist = filter (\subli -> (abs ( head subli - last subli )) == mini )
+ allCombis
+ where
+ allCombis :: [[Int]]
+ allCombis = combinations 2 numberlist
+ mini :: Int
+ mini = minimum $ map (\subli -> abs( head subli - last subli ) ) allCombis
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a list of distinct integers!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-327/ulrich-rieke/perl/ch-1.pl b/challenge-327/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..02a86b24fa
--- /dev/null
+++ b/challenge-327/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/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 %found ;
+map { $found{$_}++ } @numbers ;
+my @selected = grep { not exists( $found{$_} ) } (1..$len) ;
+say '(' . join( ',' , @selected ) . ')' ;
diff --git a/challenge-327/ulrich-rieke/perl/ch-2.pl b/challenge-327/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..b5f1c7bb27
--- /dev/null
+++ b/challenge-327/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( combinations ) ;
+use List::Util qw ( min ) ;
+
+say "Enter some distinct integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @allCombis = combinations( \@numbers , 2 ) ;
+my @differences ;
+map { push( @differences , abs( $_->[0] - $_->[1] )) } @allCombis ;
+my $minimum = min( @differences ) ;
+my @selected = grep { abs( $_->[0] - $_->[1] ) == $minimum } @allCombis ;
+for my $pair ( @selected ) {
+ print '[' . $pair->[0] . ',' . $pair->[1] . ']' ;
+ if ( $pair != $selected[-1] ) {
+ print " ," ;
+ }
+ else {
+ say " " ;
+ }
+}
diff --git a/challenge-327/ulrich-rieke/raku/ch-1.raku b/challenge-327/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..9b1fb6ad23
--- /dev/null
+++ b/challenge-327/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,10 @@
+use v6 ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $len = @numbers.elems ;
+my %found ;
+@numbers.map( {%found{$_}++} ) ;
+my @selected = (1..$len).grep( {not %found{$_}:exists} ) ;
+say '(' ~ @selected.join( ',' ) ~ ')' ;
diff --git a/challenge-327/ulrich-rieke/raku/ch-2.raku b/challenge-327/ulrich-rieke/raku/ch-2.raku
new file mode 100755
index 0000000000..b517159fc1
--- /dev/null
+++ b/challenge-327/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,9 @@
+use v6 ;
+
+say "Enter some distinct integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my @allCombis = @numbers.combinations: 2 ;
+my $minimum = @allCombis.map( {abs( $_[0] - $_[1] )} ).min ;
+my @selected = @allCombis.grep( {abs( $_[0] - $_[1] ) == $minimum } ) ;
+@selected.say ;
diff --git a/challenge-327/ulrich-rieke/rust/ch-1.rs b/challenge-327/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..26a6a4e27d
--- /dev/null
+++ b/challenge-327/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,17 @@
+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 numbers : Vec<i32> = inline.trim( ).split_whitespace( ).map( |s|
+ s.parse::<i32>( ).unwrap( )).collect( ) ;
+ let mut solution : Vec<i32> = Vec::new( ) ;
+ let len : i32 = numbers.len( ) as i32 ;
+ for i in 1i32..=len {
+ if ! numbers.contains( &i ) {
+ solution.push( i ) ;
+ }
+ }
+ println!("{:?}" , solution ) ;
+}
diff --git a/challenge-327/ulrich-rieke/rust/ch-2.rs b/challenge-327/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..77cd7e13a6
--- /dev/null
+++ b/challenge-327/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,16 @@
+use std::io ;
+use itertools::Itertools ;
+
+fn main() {
+ println!("Enter some distinct integers separated by blanks!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let numbers : Vec<i32> = inline.trim( ).split_whitespace( ).map( |s|
+ s.parse::<i32>( ).unwrap( )).collect( ) ;
+ let pairs : Vec<Vec<i32>> = numbers.into_iter( ).combinations( 2 ).
+ collect( ) ;
+ let min_diff : u32 = pairs.iter( ).map( |v| v[0].abs_diff(v[1] ) ).
+ min( ).unwrap( ) ;
+ println!("{:?}" , pairs.into_iter( ).filter( |v| v[0].abs_diff(v[1])
+ == min_diff ).collect::<Vec<Vec<i32>>>( ) ) ;
+}