diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-11-04 22:01:52 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-11-04 22:01:52 +0000 |
| commit | 3e3453d23c7de1c054de694f4a3dbe86599d038b (patch) | |
| tree | 1cdfa53124438ef3089aea7c676870d0bb2b3b64 | |
| parent | 8e2f7ab112e9cbd026d58f2fa910aba5e77ef3e1 (diff) | |
| download | perlweeklychallenge-club-3e3453d23c7de1c054de694f4a3dbe86599d038b.tar.gz perlweeklychallenge-club-3e3453d23c7de1c054de694f4a3dbe86599d038b.tar.bz2 perlweeklychallenge-club-3e3453d23c7de1c054de694f4a3dbe86599d038b.zip | |
- Added solutions by Ulrich Rieke.
- Added solutions by Eric Cheung.
- Added solutions by Conor Hoekstra.
- Added solutions by E. Choroba.
- Added solutions by Mark Anderson.
- Added solutions by W. Luis Mochan.
35 files changed, 3573 insertions, 3057 deletions
diff --git a/challenge-294/eric-cheung/python/ch-1.py b/challenge-294/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..8dc9dbb2ef --- /dev/null +++ b/challenge-294/eric-cheung/python/ch-1.py @@ -0,0 +1,27 @@ +
+arrInts = [10, 4, 20, 1, 3, 2] ## Example 1
+## arrInts = [0, 6, 1, 8, 5, 2, 4, 3, 0, 7] ## Example 2
+## arrInts = [10, 30, 20] ## Example 3
+
+arrInts = sorted(arrInts)
+
+## print (arrInts)
+
+arrOutput = []
+
+arrSubOutput = [arrInts[0]]
+
+for nIndxLoop in range(1, len(arrInts)):
+ if arrInts[nIndxLoop] - arrInts[nIndxLoop - 1] != 1:
+ arrOutput.append(arrSubOutput)
+ arrSubOutput = []
+
+ arrSubOutput.append(arrInts[nIndxLoop])
+
+arrOutput.append(arrSubOutput)
+
+## print (arrOutput)
+
+nMaxLen = max([len(arrLoop) for arrLoop in arrOutput])
+
+print (-1 if nMaxLen == 1 else nMaxLen)
diff --git a/challenge-294/eric-cheung/python/ch-2.py b/challenge-294/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..19ba9bdc1b --- /dev/null +++ b/challenge-294/eric-cheung/python/ch-2.py @@ -0,0 +1,12 @@ +
+from itertools import permutations
+
+## arrInput = [1, 2, 3] ## Example 1
+## arrInput = [2, 1, 3] ## Example 2
+arrInput = [3, 1, 2] ## Example 3
+
+arrPermList = [list(arrSet) for arrSet in list(permutations(arrInput))]
+
+## print (arrPermList)
+
+print (arrPermList[arrPermList.index(arrInput) + 1])
diff --git a/challenge-294/ulrich-rieke/cpp/ch-1.cpp b/challenge-294/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..b1cf653829 --- /dev/null +++ b/challenge-294/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,49 @@ +#include <vector>
+#include <algorithm>
+#include <string>
+#include <sstream>
+#include <iostream>
+
+std::vector<std::string> split( const std::string & text , char delim ) {
+ std::istringstream istr { text } ;
+ std::vector<std::string> tokens ;
+ std::string word ;
+ while ( std::getline( istr , word , delim ) ) {
+ 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 numberstrings { split( line , ' ' ) } ;
+ std::vector<int> numbers , lengths ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ int len = numbers.size( ) ;
+ int run { 1 } ;
+ int i = 1 ;
+ while ( i < len ) {
+ if ( numbers[i] - numbers[i - 1] == 1 ) {
+ run++ ;
+ }
+ else {
+ lengths.push_back( run ) ;
+ run = 1 ;
+ }
+ i++ ;
+ }
+ lengths.push_back( run ) ;
+ int maximum = *std::max_element( lengths.begin( ) , lengths.end( ) ) ;
+ if ( maximum == 1 )
+ std::cout << -1 ;
+ else
+ std::cout << maximum ;
+ std::cout << '\n' ;
+ return 0 ;
+}
+
+
diff --git a/challenge-294/ulrich-rieke/cpp/ch-2.cpp b/challenge-294/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..c60bf76cdb --- /dev/null +++ b/challenge-294/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,50 @@ +#include <iostream>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <sstream>
+
+std::vector<std::string> split( const std::string & text , char delim ) {
+ std::istringstream istr { text } ;
+ std::vector<std::string> tokens ;
+ std::string word ;
+ while ( std::getline( istr , word , delim ) ) {
+ tokens.push_back( word ) ;
+ }
+ return tokens ;
+}
+
+void printVector( const std::vector<int> & selected ) {
+ std::cout << "( ";
+ for ( int i : selected ) {
+ std::cout << i << ' ' ;
+ }
+ std::cout << ')' ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<int> numbers ;
+ auto numberstrings { split( line , ' ' ) } ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::vector<int> myCopy( numbers ) ;
+ std::sort( myCopy.begin( ) , myCopy.end( ) ) ;
+ std::vector<std::vector<int>> allPermus ;
+ allPermus.push_back( myCopy ) ;
+ while ( std::next_permutation( myCopy.begin( ) , myCopy.end( ) ) ) {
+ allPermus.push_back( myCopy ) ;
+ }
+ auto pos = std::find( allPermus.begin( ) , allPermus.end( ) ,
+ numbers ) ;
+ if ( *pos == allPermus.back( ) ) {
+ printVector( allPermus.front( ) ) ;
+ }
+ else {
+ printVector( *(pos + 1 ) ) ;
+ }
+ std::cout << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-294/ulrich-rieke/haskell/ch-1.hs b/challenge-294/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..c2cd46c096 --- /dev/null +++ b/challenge-294/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,30 @@ +module Challenge294
+ where
+import Data.List.Split ( divvy )
+import Data.List ( (!! ) , maximumBy , sort )
+
+isContiguous :: [Int] -> Bool
+isContiguous list = all (\v -> last v - head v == 1 ) $ divvy 2 1 list
+
+cutFromList :: [Int] -> Int -> Int -> [Int]
+cutFromList list from to = take ( to - from + 1 ) $ drop from list
+
+findSublists :: [Int] -> [[Int]]
+findSublists list = [ cutFromList sorted i j | i <- [0..l - 2] ,
+ j <- [i + 1 .. l - 1]]
+ where
+ l = length list
+ sorted = sort list
+
+solution :: [Int] -> Int
+solution list =
+ let sublists = findSublists list
+ selected = filter isContiguous sublists
+ in if null selected then -1 else length $ maximumBy(\a b -> compare ( length a )
+ (length b) ) selected
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by blanks!"
+ numberstrings <- getLine
+ print $ solution $ map read $ words numberstrings
diff --git a/challenge-294/ulrich-rieke/haskell/ch-2.hs b/challenge-294/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..b945542032 --- /dev/null +++ b/challenge-294/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,23 @@ +module Challenge294_2
+ where
+import Data.List ( sortBy , (!!) , permutations , sort , findIndices )
+
+lexi_compare :: [Int] -> [Int] -> Ordering
+lexi_compare firstList secondList = compare firstString secondString
+ where
+ firstString = foldl1 ( ++ ) $ map show firstList
+ secondString = foldl1 (++) $ map show secondList
+
+solution :: [Int] -> [Int]
+solution numbers =
+ let sorted = sort numbers
+ permus = sortBy lexi_compare $ permutations sorted
+ index = head $ findIndices ( == numbers ) permus
+ in if index == (length permus - 1) then head permus else
+ permus !! ( index + 1 )
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers, separated by blanks!"
+ numbers <- getLine
+ print $ solution $ map read $ words numbers
diff --git a/challenge-294/ulrich-rieke/perl/ch-1.pl b/challenge-294/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..adfed5e7f2 --- /dev/null +++ b/challenge-294/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( max ) ;
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @sorted = sort { $a <=> $b } @numbers ;
+my $len = scalar( @sorted ) ;
+my $run = 1 ;
+my @lengths ;
+my $i = 1 ;
+while ( $i < $len ) {
+ if ( $sorted[ $i ] - $sorted[ $i - 1 ] == 1 ) {
+ $run++ ;
+ }
+ else {
+ push( @lengths , $run ) ;
+ $run = 1 ;
+ }
+ $i++ ;
+}
+push( @lengths , $run ) ;
+my $maximum = max( @lengths ) ;
+if ( $maximum == 1 ) {
+ say -1 ;
+}
+else {
+ say $maximum ;
+}
diff --git a/challenge-294/ulrich-rieke/perl/ch-2.pl b/challenge-294/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..d2936170b2 --- /dev/null +++ b/challenge-294/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( permutations ) ;
+
+#when working on this task I unfortunately realized that Perl is
+#unable to compare the contents of two arrays as Raku can do
+#@a == @b means : as many elements in @a as in @b!! It's a bit of
+#a shame, although, of course, there are modules for it!!
+sub areEqual {
+ my $firstArray = shift ;
+ my $secondArray = shift ;
+ if ( scalar( @{$firstArray}) != scalar( @{$secondArray} ) ) {
+ return 0 ;
+ }
+ else {
+ for my $i ( 0..scalar( @{$firstArray} ) - 1 ) {
+ if ( $firstArray->[$i] != $secondArray->[$i] ) {
+ return 0 ;
+ }
+ }
+ return 1 ;
+ }
+}
+
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @for_sorting = @numbers ;
+my @sorted = sort {$a <=> $b} @for_sorting ;
+my @permus ;
+my $iter = permutations( \@sorted ) ;
+while ( my $c = $iter->next ) {
+ push( @permus , $c ) ;
+}
+my $len = scalar( @permus ) ;
+my $i = 0 ;
+while ( not ( areEqual( $permus[$i] , \@numbers ))) {
+ $i++ ;
+}
+if ( $i == $len - 1 ) {
+ say '(' . join( ',' , @{$permus[0]} ) . ')' ;
+}
+else {
+ say '(' . join( ',' , @{$permus[$i + 1] } ) .')' ;
+}
diff --git a/challenge-294/ulrich-rieke/raku/ch-1.raku b/challenge-294/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..64b305ccd5 --- /dev/null +++ b/challenge-294/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,28 @@ +use v6 ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my @sorted = @numbers.sort( {$^a <=> $^b} ) ;
+my $len = @numbers.elems ;
+my @lengths ;
+my $run = 1 ;
+my $i = 1 ;
+while ( $i < $len ) {
+ if ( @sorted[$i] - @sorted[$i - 1] == 1 ) {
+ $run++ ;
+ }
+ else {
+ @lengths.push( $run ) ;
+ $run = 1 ;
+ }
+ $i++ ;
+}
+@lengths.push( $run ) ;
+my $maximum = @lengths.max ;
+if ( $maximum == 1 ) {
+ say -1 ;
+}
+else {
+ $maximum.say ;
+}
diff --git a/challenge-294/ulrich-rieke/raku/ch-2.raku b/challenge-294/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..8cf0a8c1f1 --- /dev/null +++ b/challenge-294/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,21 @@ +use v6 ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my @for_sorting = @numbers ;
+@for_sorting.sort( {$^a <=> $^b} ) ;
+my $permus = permutations( @for_sorting ) ;
+my $len = $permus.elems ;
+my $i = 0 ;
+while ( $permus[$i].Array != @numbers ) {
+ $i++ ;
+}
+#the original number sequence corresponds to the last permutation of the
+#sorted numbers , then go back to the first permutation!
+if ( $i == $len - 1 ) {
+ say $permus[0] ;
+}
+else {
+ say $permus[$i + 1] ;
+}
diff --git a/challenge-294/ulrich-rieke/rust/ch-1.rs b/challenge-294/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..34d4451616 --- /dev/null +++ b/challenge-294/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,33 @@ +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 mut numbers : Vec<i32> = inline.trim( ).split_whitespace( ).map( + |s| s.parse::<i32>( ).unwrap( ) ).collect( ) ; + numbers.sort( ) ; + let len = numbers.len( ) ; + let mut lengths : Vec<usize> = Vec::new( ) ; + let mut i : usize = 1 ; + let mut run : usize = 1 ; + while i < len { + if numbers[i] - numbers[i - 1] == 1 { + run += 1 ; + } + else { + lengths.push( run ) ; + run = 1 ; + } + i += 1 ; + } + lengths.push( run ) ; + let maxlen : usize = lengths.into_iter( ).max( ).unwrap( ) ; + if maxlen == 1 { + println!("-1") ; + } + else { + println!("{maxlen}") ; + } +} + diff --git a/challenge-294/ulrich-rieke/rust/ch-2.rs b/challenge-294/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..33e31d6337 --- /dev/null +++ b/challenge-294/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,24 @@ +use std::io ; +use itertools::Itertools ; + +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 for_comparison : Vec<i32> = numbers.clone( ) ; + for_comparison.sort( ) ; + let perms : Vec<Vec<i32>> = for_comparison.into_iter( ) + .permutations( numbers.len( ) ).collect() ; + let len = perms.len( ) ; + let original : usize = perms.binary_search( &numbers ).unwrap( ) ; + //if the original number sequence happens to be the last in + //lexicographical compare take the first in the list of permutations + if original == len - 1 { + println!("{:?}" , perms[0] ) ; + } + else { + println!("{:?}" , perms[original + 1]) ; + } +} diff --git a/stats/pwc-challenge-293.json b/stats/pwc-challenge-293.json new file mode 100644 index 0000000000..92da22f26f --- /dev/null +++ b/stats/pwc-challenge-293.json @@ -0,0 +1,653 @@ +{ + "drilldown" : { + "series" : [ + { + "id" : "Ali Moradi", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Ali Moradi" + }, + { + "id" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Arne Sommer" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Athanasius", + "name" : "Athanasius" + }, + { + "name" : "BarrOff", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "BarrOff" + }, + { + "name" : "Bob Lied", + "id" : "Bob Lied", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Cheok-Yin Fung", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Cheok-Yin Fung" + }, + { + "name" : "Clifton Wood", + "data" : [ + [ + "Raku", + 1 + ] + ], + "id" : "Clifton Wood" + }, + { + "name" : "Dave Jacoby", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Dave Jacoby" + }, + { + "id" : "David Ferrone", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "David Ferrone" + }, + { + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "E. Choroba" + }, + { + "name" : "Feng Chang", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Feng Chang" + }, + { + "name" : "Jaldhar H. Vyas", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jaldhar H. Vyas" + }, + { + "name" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Kjetil Skotheim", + "name" : "Kjetil Skotheim" + }, + { + "id" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Laurent Rosenfeld" + }, + { + "name" : "Lubos Kolouch", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Lubos Kolouch" + }, + { + "name" : "Mariano Ortega", + "id" : "Mariano Ortega", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Mark Anderson", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Mark Anderson" + }, + { + "name" : "Matthew Neleigh", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Matthew Neleigh" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Matthias Muth", + "name" : "Matthias Muth" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Packy Anderson", + "name" : "Packy Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Pawel Kuciel", + "name" : "Pawel Kuciel" + }, + { + "name" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Peter Campbell Smith" + }, + { + "id" : "Peter Meszaros", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Peter Meszaros" + }, + { + "id" : "Robbie Hatley", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Robbie Hatley" + }, + { + "id" : "Robert Ransbottom", + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Robert Ransbottom" + }, + { + "name" : "Roger Bell_West", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Roger Bell_West" + }, + { + "name" : "Santiago Leyva", + "id" : "Santiago Leyva", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Simon Green", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Simon Green" + }, + { + "name" : "Thomas Kohler", + "id" : "Thomas Kohler", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Tomasz Mucha", + "name" : "Tomasz Mucha" + }, + { + "id" : "Ulrich Rieke", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Ulrich Rieke" + }, + { + "name" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 |
