From e51ae043517be5d93aa80deb9e8e67bf599ceecc Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 20 May 2025 09:21:46 +0100 Subject: - Added solutions by Eric Cheung. - Added solutions by Ulrich Rieke. - Added solutions by Andrew Shitov. - Added solutions by E. Choroba. - Added solutions by Niels van Dijke. - Added solutions by Feng Chang. - Added solutions by Matthias Muth. - Added solutions by Mark Anderson. - Added solutions by Luca Ferrari. - Added solutions by David Ferrone. - Added solutions by Steven Wilson. - Added solutions by Conor Hoekstra. - Added solutions by W. Luis Mochan. - Added solutions by Yitzchak Scott-Thoennes. --- challenge-322/eric-cheung/python/ch-1.py | 28 ++++++++++++++++++ challenge-322/eric-cheung/python/ch-2.py | 12 ++++++++ challenge-322/perlboy1967/perl/ch-1.pl | 46 ++++++++++++++++++++++++++++++ challenge-322/perlboy1967/perl/ch-2.pl | 39 +++++++++++++++++++++++++ challenge-322/perlboy1967/perl/ch1.pl | 46 ------------------------------ challenge-322/perlboy1967/perl/ch2.pl | 39 ------------------------- challenge-322/ulrich-rieke/cpp/ch-1.cpp | 44 ++++++++++++++++++++++++++++ challenge-322/ulrich-rieke/cpp/ch-2.cpp | 44 ++++++++++++++++++++++++++++ challenge-322/ulrich-rieke/haskell/ch-1.hs | 20 +++++++++++++ challenge-322/ulrich-rieke/haskell/ch-2.hs | 17 +++++++++++ challenge-322/ulrich-rieke/perl/ch-1.pl | 27 ++++++++++++++++++ challenge-322/ulrich-rieke/perl/ch-2.pl | 19 ++++++++++++ challenge-322/ulrich-rieke/raku/ch-1.raku | 23 +++++++++++++++ challenge-322/ulrich-rieke/raku/ch-2.raku | 12 ++++++++ challenge-322/ulrich-rieke/rust/ch-1.rs | 27 ++++++++++++++++++ challenge-322/ulrich-rieke/rust/ch-2.rs | 20 +++++++++++++ 16 files changed, 378 insertions(+), 85 deletions(-) create mode 100755 challenge-322/eric-cheung/python/ch-1.py create mode 100755 challenge-322/eric-cheung/python/ch-2.py create mode 100755 challenge-322/perlboy1967/perl/ch-1.pl create mode 100755 challenge-322/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-322/perlboy1967/perl/ch1.pl delete mode 100755 challenge-322/perlboy1967/perl/ch2.pl create mode 100755 challenge-322/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-322/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-322/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-322/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-322/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-322/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-322/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-322/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-322/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-322/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-322') diff --git a/challenge-322/eric-cheung/python/ch-1.py b/challenge-322/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..ca4df0cc2d --- /dev/null +++ b/challenge-322/eric-cheung/python/ch-1.py @@ -0,0 +1,28 @@ + +## Example 1 +## strInput = "ABC-D-E-F" +## nInput = 3 + +## Example 2 +## strInput = "A-BC-D-E" +## nInput = 2 + +## Example 3 +strInput = "-A-B-CD-E" +nInput = 4 + +arrSplit = strInput.split("-") + +arrOutput = [] +strTemp = arrSplit[-1] + +for strLoop in arrSplit[:-1][::-1]: + if len(strTemp) == nInput: + arrOutput.insert(0, strTemp) + strTemp = strLoop + else: + strTemp = strLoop + strTemp + +arrOutput.insert(0, strTemp) + +print ("-".join(arrOutput)) diff --git a/challenge-322/eric-cheung/python/ch-2.py b/challenge-322/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..bc975d09a7 --- /dev/null +++ b/challenge-322/eric-cheung/python/ch-2.py @@ -0,0 +1,12 @@ + +## arrInts = [55, 22, 44, 33] ## Example 1 +## arrInts = [10, 10, 10] ## Example 2 +arrInts = [5, 1, 1, 4, 3] ## Example 3 + +arrSet = sorted(set(arrInts)) + +## print (arrSet) + +arrOutput = [arrSet.index(nLoop) + 1 for nLoop in arrInts] + +print (arrOutput) diff --git a/challenge-322/perlboy1967/perl/ch-1.pl b/challenge-322/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..d945105fd2 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: String Format +Submitted by: Mohammad Sajid Anwar + +You are given a string and a positive integer. + +Write a script to format the string, removing any dashes, in groups of +size given by the integer. The first group can be smaller than the integer +but should have at least one character. Groups should be separated by dashes. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +sub stringFormat ($str,$len) { + my @r; + + # Get first part + $str =~ s/([^-]{1,$len})//; + push(@r,$1); + + # Get the rest + $str =~ s/-//g; + 1 while (push(@r,substr($str,0,$len,'')) && $str); + + return join('-',@r); +} + +is(stringFormat('ABC-D-E-F',3),'ABC-DEF','Example 1'); +is(stringFormat('A-BC-D-E',2),'A-BC-DE','Example 2'); +is(stringFormat('-A-B-CD-E',4),'A-BCDE','Example 3'); +is(stringFormat('ABC-DE-F',2),'AB-CD-EF','Own example'); + +done_testing; diff --git a/challenge-322/perlboy1967/perl/ch-2.pl b/challenge-322/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..5e46d4cf12 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Rank Array +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return an array of the ranks of each element: the lowest +value has rank 1, next lowest rank 2, etc. If two elements are the same +then they share the same rank. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::MoreUtils qw(uniq); + +sub rankArray (@ints) { + my ($i,%idx) = (1); + $idx{$_} = $i++ for uniq sort { $a <=> $b } @ints; + return map { $idx{$_} } @ints; +} + +is([rankArray(55,22,44,33)],[4,1,3,2],'Example 1'); +is([rankArray(10,10,10)],[1,1,1],'Example 2'); +is([rankArray(5,1,1,4,3)],[4,1,1,3,2],'Example 3'); + +done_testing; diff --git a/challenge-322/perlboy1967/perl/ch1.pl b/challenge-322/perlboy1967/perl/ch1.pl deleted file mode 100755 index d945105fd2..0000000000 --- a/challenge-322/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 322 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 1: String Format -Submitted by: Mohammad Sajid Anwar - -You are given a string and a positive integer. - -Write a script to format the string, removing any dashes, in groups of -size given by the integer. The first group can be smaller than the integer -but should have at least one character. Groups should be separated by dashes. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); -use Test2::V0 qw(-no_srand); -no warnings qw(experimental::signatures); - -sub stringFormat ($str,$len) { - my @r; - - # Get first part - $str =~ s/([^-]{1,$len})//; - push(@r,$1); - - # Get the rest - $str =~ s/-//g; - 1 while (push(@r,substr($str,0,$len,'')) && $str); - - return join('-',@r); -} - -is(stringFormat('ABC-D-E-F',3),'ABC-DEF','Example 1'); -is(stringFormat('A-BC-D-E',2),'A-BC-DE','Example 2'); -is(stringFormat('-A-B-CD-E',4),'A-BCDE','Example 3'); -is(stringFormat('ABC-DE-F',2),'AB-CD-EF','Own example'); - -done_testing; diff --git a/challenge-322/perlboy1967/perl/ch2.pl b/challenge-322/perlboy1967/perl/ch2.pl deleted file mode 100755 index 5e46d4cf12..0000000000 --- a/challenge-322/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 322 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Rank Array -Submitted by: Mohammad Sajid Anwar - -You are given an array of integers. - -Write a script to return an array of the ranks of each element: the lowest -value has rank 1, next lowest rank 2, etc. If two elements are the same -then they share the same rank. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); -use Test2::V0 qw(-no_srand); -no warnings qw(experimental::signatures); - -use List::MoreUtils qw(uniq); - -sub rankArray (@ints) { - my ($i,%idx) = (1); - $idx{$_} = $i++ for uniq sort { $a <=> $b } @ints; - return map { $idx{$_} } @ints; -} - -is([rankArray(55,22,44,33)],[4,1,3,2],'Example 1'); -is([rankArray(10,10,10)],[1,1,1],'Example 2'); -is([rankArray(5,1,1,4,3)],[4,1,1,3,2],'Example 3'); - -done_testing; diff --git a/challenge-322/ulrich-rieke/cpp/ch-1.cpp b/challenge-322/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..e751d0f201 --- /dev/null +++ b/challenge-322/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +std::vector split( const std::string & text , char delimiter ) { + std::vector tokens ; + std::istringstream istr { text } ; + std::string word ; + while ( std::getline( istr , word , delimiter ) ) + tokens.push_back( word ) ; + return tokens ; +} + +int main( ) { + std::cout << "Enter a string and a positive number!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + auto tokens { split( line , ' ' ) } ; + std::string word { tokens[0] } ; + auto it = word.find( "-" ) ; + while ( it != std::string::npos ) { + word.erase( it , 1 ) ; + it = word.find( "-" ) ; + } + int number { std::stoi( tokens[1] ) } ; + std::string solution ; + int wordlen = static_cast( word.length( ) ) ; + int firstlen = wordlen % number ; + int pos = 0 ; + if ( firstlen != 0 ) { + solution = word.substr( 0 , firstlen ) ; + solution.push_back( '-' ) ; + pos = firstlen ; + } + while ( pos + number < wordlen ) { + solution += word.substr( pos , number ) ; + solution.push_back( '-' ) ; + pos += number ; + } + solution += word.substr( pos ) ; + std::cout << solution << '\n' ; + return 0 ; +} diff --git a/challenge-322/ulrich-rieke/cpp/ch-2.cpp b/challenge-322/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..6b921e83b1 --- /dev/null +++ b/challenge-322/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include + +std::vector split( const std::string & text , char delimiter ) { + std::vector tokens ; + std::istringstream istr { text } ; + std::string word ; + 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 numbers ; + for ( auto s : tokens ) + numbers.push_back( std::stoi( s ) ) ; + std::vector for_comp { numbers } ; + std::sort( for_comp.begin( ) , for_comp.end( ) ) ; + auto up_to = std::unique( for_comp.begin( ) , for_comp.end( ) ) ; + std::vector solution ; + int n = 0 ; + std::vector> pairs ; + for ( auto it = for_comp.begin( ) ; it != up_to ; ++it ) { + pairs.push_back( std::make_pair( ++n , *it )) ; + } + for ( int i : numbers ) { + int num = std::find_if( pairs.begin( ) , pairs.end( ) , [i]( auto & p ){ + return p.second == i ;})->first ; + solution.push_back( num ) ; + } + std::cout << "( " ; + for ( auto i : solution ) + std::cout << i << ' ' ; + std::cout << ")\n" ; + return 0 ; +} diff --git a/challenge-322/ulrich-rieke/haskell/ch-1.hs b/challenge-322/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..1c5e8e915e --- /dev/null +++ b/challenge-322/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,20 @@ +module Challenge322 + where +import Data.List.Split ( chunksOf ) +import Data.List ( intercalate ) + +solution :: String -> Int -> String +solution str num = + let noHyphens = filter ( /= '-' ) str + firstlen = mod ( length noHyphens ) num + allChunks = if firstlen /= 0 then [take firstlen noHyphens] ++ + chunksOf num ( drop firstlen noHyphens ) else chunksOf num + noHyphens + in intercalate "-" allChunks + +main :: IO ( ) +main = do + putStrLn "Enter a string and a positive integer separated by blanks!" + line <- getLine + let expressions = words line + print $ solution ( head expressions ) ( read $ last expressions ) diff --git a/challenge-322/ulrich-rieke/haskell/ch-2.hs b/challenge-322/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..c8a7008f4a --- /dev/null +++ b/challenge-322/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,17 @@ +module Challenge322_2 + where +import qualified Data.Set as S +import Data.List ( sort ) +import Data.Maybe ( fromJust ) + +solution :: [Int] -> [Int] +solution list = + let uniques = S.toList $ S.fromList $ sort list + zipped = zip uniques [1, 2 ..] + in map (\n -> fromJust $ lookup n zipped ) list + +main :: IO ( ) +main = do + putStrLn "Enter some integers separated by blanks!" + numberline <- getLine + print $ solution $ map read $ words numberline diff --git a/challenge-322/ulrich-rieke/perl/ch-1.pl b/challenge-322/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..3b281108fd --- /dev/null +++ b/challenge-322/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter a string and a positive integer separated by blanks!" ; +my $line = ; +chomp $line ; +my ( $word , $number ) = split( /\s/ , $line ) ; +$word =~ s/\-//g ; #remove all '-' +my $firstlen = length( $word ) % $number ; +my $solution ; #final resut +if ( $firstlen != 0 ) { #length of first part + my $firstpart = substr( $word , 0 , $firstlen ) ; + $solution = $firstpart . "-" ; + my $secondpart = substr( $word , $firstlen ) ; + while ( $secondpart =~ /(.{$number})/g ) { + $solution .= ($1 . "-") ; + } +} +else { + while ( $word =~ /(.{$number})/g ) { + $solution .= ($1 . "-") ; + } +} +$solution =~ s/\-$// ; #remove final '-' +say $solution ; diff --git a/challenge-322/ulrich-rieke/perl/ch-2.pl b/challenge-322/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..5e732063ec --- /dev/null +++ b/challenge-322/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( zip ) ; + +say "Enter some integers separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my %numhash ; +map { $numhash{$_}++ } @numbers ; +my @zipped = zip [1..scalar( keys %numhash)] , [sort { $a <=> $b } keys + %numhash ] ; +my @solution ; +for my $n ( @numbers ) { + push( @solution , map { $_->[0] } grep { $_->[1] == $n } @zipped ) ; +} +say '(' . join( ',' , @solution ) . ')' ; diff --git a/challenge-322/ulrich-rieke/raku/ch-1.raku b/challenge-322/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..c0e90c1e57 --- /dev/null +++ b/challenge-322/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,23 @@ +use v6 ; + +say "Enter a string and a positive integer!" ; +my $line = $*IN.get ; +my ($word , $numberstring) = $line.words ; +my $number = $numberstring.Int ; +$word ~~ s:g/'-'// ; +my $wordlen = $word.chars ; +my $firstlen = $wordlen % $number ; +my $solution ; +my $pos = 0 ; +if ( $firstlen != 0 ) { + my $firstpart = $word.substr( 0 , $firstlen ) ; + $solution = $firstpart ~ "-" ; + $pos = $firstlen ; +} +while ( $pos + $number < $wordlen ) { + $solution ~= $word.substr( $pos , $number ) ; + $solution ~= "-" ; + $pos += $number ; +} +$solution ~= $word.substr( $pos ) ; +say $solution ; diff --git a/challenge-322/ulrich-rieke/raku/ch-2.raku b/challenge-322/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..e7f961feb7 --- /dev/null +++ b/challenge-322/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,12 @@ +use v6 ; + +say "Enter some integers separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my @unique_numbers = @numbers.sort( {$^a <=> $^b} ).unique ; +my @pairs = (1..@unique_numbers.elems) Z, @unique_numbers ; +my @solution ; +for @numbers -> $n { + @solution.push( @pairs.grep( {$_[1] == $n} ).map( {$_[0]} )) ; +} +say '(' ~ @solution.join( ',' ) ~ ')' ; diff --git a/challenge-322/ulrich-rieke/rust/ch-1.rs b/challenge-322/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..99f19d77f7 --- /dev/null +++ b/challenge-322/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,27 @@ +use std::io ; + +fn main() { + println!("Enter a word and a positive integer separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let words : Vec<&str> = inline.trim( ).split_whitespace( ).collect( ) ; + let number : usize = words[1].parse::( ).unwrap( ) ; + let word : String = words[0].replace("-" , "") ; + let wordlen : usize = word.len( ) ; + let firstlen : usize = wordlen % number ; + let mut solution : String = String::new( ) ; + let mut pos : usize = 0 ; + if firstlen != 0 { + let firstpart : &str = &word[..firstlen] ; + solution = firstpart.into( ) ; + solution.push('-') ; + pos = firstlen ; + } + while pos + number < wordlen { + solution += &word[pos..pos + number] ; + solution.push( '-' ) ; + pos += number ; + } + solution += &word[pos..] ; + println!("{:?}" , solution ) ; +} diff --git a/challenge-322/ulrich-rieke/rust/ch-2.rs b/challenge-322/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..e5c8bf803e --- /dev/null +++ b/challenge-322/ulrich-rieke/rust/ch-2.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 numbers : Vec = inline.trim( ).split_whitespace( ).map( |s| + s.parse::( ).unwrap( )).collect( ) ; + let mut my_numbers : Vec = numbers.clone( ) ; + my_numbers.sort( ) ; + my_numbers.dedup( ) ; + let pairs : Vec<(usize, i32)> = my_numbers.into_iter( ).enumerate( ). + map( |p| (p.0 + 1 , p.1) ).collect( ) ; + let mut solution : Vec = Vec::new( ) ; + for n in numbers { + pairs.iter( ).filter( |&p| p.1 == n ).for_each( |p| + solution.push( p.0 )) ; + } + println!("{:?}" , solution ) ; +} -- cgit