diff options
Diffstat (limited to 'challenge-257')
| -rwxr-xr-x | challenge-257/ulrich-rieke/cpp/ch-1.cpp | 21 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/cpp/ch-2.cpp | 138 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/haskell/ch-1.hs | 11 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/haskell/ch-2.hs | 74 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/perl/ch-1.pl | 11 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/perl/ch-2.pl | 160 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/raku/ch-1.raku | 10 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/rust/ch-1.rs | 16 | ||||
| -rwxr-xr-x | challenge-257/ulrich-rieke/rust/ch-2.rs | 122 |
9 files changed, 563 insertions, 0 deletions
diff --git a/challenge-257/ulrich-rieke/cpp/ch-1.cpp b/challenge-257/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..292959c14a --- /dev/null +++ b/challenge-257/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,21 @@ +#include <iostream>
+#include <vector>
+#include <iterator>
+#include <algorithm>
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::cout << "Enter e to end entry!\n" ;
+ std::vector<int> numbers { std::istream_iterator<int>{ std::cin } ,
+ std::istream_iterator<int>{} } ;
+ std::vector<int> result ;
+ for ( int i : numbers ) {
+ result.push_back( std::count_if( numbers.begin( ) , numbers.end( ) ,
+ [i]( int n ) { return n < i ; } ) ) ;
+ }
+ std::cout << "( " ;
+ std::copy( result.begin( ) , result.end( ) ,
+ std::ostream_iterator<int>( std::cout , " " )) ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-257/ulrich-rieke/cpp/ch-2.cpp b/challenge-257/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..b365f93c0b --- /dev/null +++ b/challenge-257/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,138 @@ +#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+#include <iterator>
+#include <utility>
+typedef std::vector<std::vector<int>> matrix ;
+
+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 ;
+}
+
+bool hasLeadingOne( const matrix & numbers ) {
+ for ( auto & row : numbers ) {
+ auto found = std::find_if( row.begin( ) , row.end( ) , []( int n ) {
+ return n != 0 ; } ) ;
+ if ( found != row.end( ) ) {
+ if (*found != 1) {
+ return false ;
+ }
+ }
+ }
+ return true ;
+}
+
+bool zeroesAtBottom( const matrix & numbers ) {
+ std::vector<int> zeroIndices ;
+ std::vector<int> theRest ;
+ int count = 0 ;
+ for ( auto & row : numbers ) {
+ auto found = std::find_if( row.begin( ) , row.end( ) , []( int n ) {
+ return n != 0 ; } ) ;
+ if ( found != row.end( ) ) { //row does not only contain 0
+ theRest.push_back( count ) ;
+ }
+ else { //row contains only 0
+ zeroIndices.push_back( count ) ;
+ }
+ count++ ;
+ }
+ //the maximum row number of all lines not containing 0's only must be
+ //smaller than any of the line numbers of lines containing only 0's
+ int maximum = *std::max_element( theRest.begin( ) , theRest.end( ) ) ;
+ return std::all_of( zeroIndices.begin( ) , zeroIndices.end( ) ,
+ [maximum]( int n ) { return n > maximum ; } ) ;
+}
+
+//are all leading ones staggered from left to right ?
+bool staggeredOnes( const matrix & numbers ) {
+ std::vector<int> leadingOnes ; //the positions of all leading 1's
+ for ( auto & row : numbers ) {
+ auto found = std::find_if( row.begin( ) , row.end( ) , []( int n ) {
+ return n != 0 ; } ) ;
+ if ( found != row.end( ) ) {
+ if ( *found == 1 ) {
+ leadingOnes.push_back( static_cast<int>(std::distance( row.begin( ),
+ found ))) ;
+ }
+ }
+ }
+ int len = leadingOnes.size( ) ;
+ if ( len == 0 || len == 1 ) {
+ return true ;
+ }
+ if ( len == 2 ) {
+ return leadingOnes[1] > leadingOnes[0] ;
+ }
+ if ( len > 2 ) {
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ if ( leadingOnes[ i + 1 ] < leadingOnes[ i ] ) {
+ return false ;
+ }
+ }
+ }
+ return true ;
+}
+
+bool allZeroesInLeadingOneColumn( const matrix & numbers ) {
+ std::vector<std::pair<int, int>> leadingOnePositions ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ std::vector<int> row { numbers[ i ] } ;
+ auto found = std::find_if( row.begin( ) , row.end( ) , []( int n ) {
+ return n != 0 ; } ) ;
+ if ( found != row.end( ) ) {
+ if ( *found == 1 ) {
+ int pos = static_cast<int>(std::distance( row.begin( ) , found ) ) ;
+ leadingOnePositions.push_back( std::make_pair( i , pos ) ) ;
+ }
+ }
+ }
+ for ( auto & p : leadingOnePositions ) {
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( i != p.first ) {
+ if ( numbers[i][p.second] != 0 ) {
+ return false ;
+ }
+ }
+ }
+ }
+ return true ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::cout << "Enter <return> to stop entry!\n" ;
+ std::string line ;
+ matrix numbers ;
+ std::getline( std::cin , line ) ;
+ while ( line.length( ) != 0 ) {
+ std::vector<std::string> row { split( line , " " ) } ;
+ std::vector<int> numberrow ;
+ for ( auto & s : row ) {
+ numberrow.push_back( std::stoi( s ) ) ;
+ }
+ numbers.push_back( numberrow ) ;
+ line.clear( ) ;
+ std::getline( std::cin , line ) ;
+ }
+ if ( hasLeadingOne( numbers ) && zeroesAtBottom( numbers ) &&
+ staggeredOnes( numbers ) && allZeroesInLeadingOneColumn( numbers )) {
+ std::cout << 1 << '\n' ;
+ }
+ else {
+ std::cout << 0 << '\n' ;
+ }
+ return 0 ;
+}
+
diff --git a/challenge-257/ulrich-rieke/haskell/ch-1.hs b/challenge-257/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..c3b5b5daa4 --- /dev/null +++ b/challenge-257/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,11 @@ +module Challenge257
+ where
+
+solution :: [Int] -> [Int]
+solution list = map (\i -> length $ filter ( < i ) list ) list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers, separated by blanks!"
+ numbers <- getLine
+ print $ solution $ map read $ words numbers
diff --git a/challenge-257/ulrich-rieke/haskell/ch-2.hs b/challenge-257/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..5b45b72d45 --- /dev/null +++ b/challenge-257/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,74 @@ +module Challenge257_2
+ where
+import Data.List ( transpose , sortOn , findIndices , (!!))
+import Data.List.Split ( divvy )
+
+findLeadingNonNull :: [[Int]] -> [(Int , [Int] )]
+findLeadingNonNull block = map (\p -> (fst p , findIndices (/= 0 ) $ block !! (
+ fst p ) )) $ zip [0, 1 ..] block
+
+hasLeadingOne :: [[Int]] -> Bool
+hasLeadingOne block = null pairs || all (\p -> ( block !! ( fst p )
+ !! ( head $ snd p )) == 1 ) pairs
+ where
+ pairs = filter ( not . null . snd ) $ findLeadingNonNull block
+
+zeroesAtBottom :: [[Int]] -> Bool
+zeroesAtBottom block = null nulls || maximum_nonNulls < minimum_nulls
+ where
+ allNull :: [Int] -> Bool
+ allNull row = all ( == 0 ) row
+ pairs :: [(Int , [Int])]
+ pairs = zip [0,1..] block
+ nonNulls :: [(Int , [Int])]
+ nonNulls = sortOn fst $ filter ( not. allNull . snd ) pairs
+ nulls :: [(Int , [Int] )]
+ nulls = sortOn fst $ filter ( allNull . snd ) pairs
+ maximum_nonNulls :: Int
+ maximum_nonNulls = fst $ last $ nonNulls
+ minimum_nulls ::Int
+ minimum_nulls = fst $ head $ nulls
+
+hasStackedOnes :: [[Int]] -> Bool
+hasStackedOnes matrix =
+ let pairs = filter ( not . null . snd ) $ findLeadingNonNull matrix
+ leadingOnes = filter (\p -> (matrix !! ( fst p ) !! ( head $ snd p )) == 1 )
+ pairs
+ positions = map ( head . snd ) leadingOnes
+ myP = length positions
+ in null pairs || myP == 1 || ( if myP == 2 then ( positions !! 1 )
+ > ( positions !! 0 ) else all (\subli -> last subli > head subli ) $ divvy
+ 2 1 positions )
+
+leadingOnesAllNullCol :: [[Int]] -> Bool
+leadingOnesAllNullCol matrix =
+ let pairs = filter ( not . null . snd ) $ findLeadingNonNull matrix
+ leadingOnes = filter (\p -> (matrix !! ( fst p ) !! ( head $ snd p )) == 1 )
+ pairs
+ positions = map ( head . snd ) leadingOnes
+ transposed = transpose matrix
+ l = length matrix
+ in null pairs || all (\col -> (length $ findIndices (== 0) (transposed !! col))
+ == l - 1 ) positions
+
+condition :: [[Int]] -> Bool
+condition matrix = leadingOnesAllNullCol matrix && hasStackedOnes matrix
+ && zeroesAtBottom matrix && hasLeadingOne matrix
+
+getMatrix :: IO [String]
+getMatrix = do
+ line <- getLine
+ if not $ null line then do
+ restOfLines <- getMatrix
+ return ( line : restOfLines )
+ else do
+ return []
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers, separated by blanks!"
+ putStrLn "Enter <return> to end entry!"
+ matrix <- getMatrix
+ let numbers = map ( map read . words ) matrix
+ --print $ leadingOnesAllNullCol numbers
+ if condition numbers then print 1 else print 0
diff --git a/challenge-257/ulrich-rieke/perl/ch-1.pl b/challenge-257/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..2540512dc7 --- /dev/null +++ b/challenge-257/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,11 @@ +use v5.36.0 ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s+/ , $line ) ;
+my @result ;
+for my $num( @numbers ) {
+ push @result , scalar( grep { $_ < $num } @numbers ) ;
+}
+say "(" . join( ',' , @result ) . ")" ;
diff --git a/challenge-257/ulrich-rieke/perl/ch-2.pl b/challenge-257/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..c2c453d3a0 --- /dev/null +++ b/challenge-257/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,160 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( any all max) ;
+
+sub findOnePos { #find the first position of a 1 if there is one
+ my $row = shift ;
+ my $len = scalar( @$row ) ;
+ my $pos = 0 ;
+ while ( $row->[$pos] == 0 ) {
+ $pos++ ;
+ if ( $pos == $len ) {
+ last ;
+ }
+ }
+ if ( $pos < $len ) {
+ if ( $row->[$pos] == 1 ) {
+ return $pos ;
+ }
+ else {
+ return -1 ;
+ }
+ }
+ else {
+ return -1 ;
+ }
+}
+
+#determine whether a possible non-0 entry in a row is a 1
+sub hasLeadingOne {
+ my $matrix = shift ;
+ for my $row ( @$matrix ) {
+ if ( any { $_ != 0 } @$row ) {
+ my $pos = 0 ;
+ while ( $row->[$pos] == 0 ) {
+ $pos++ ;
+ }
+ if ( $row->[$pos] != 1 ) {#it not a 1
+ return 0 ; #return false
+ }
+ }
+ }
+ return 1 ;
+}
+
+sub zeroesAtBottom {
+ my $matrix = shift ;
+ my @rownum ;#create an array of row number and row content
+ my $count = 0 ;
+ for my $row( @$matrix ) {
+ my @currentrow = @$row ;#it's better to dereference...!
+ push( @rownum, [$count , join( '' , @currentrow ) ] ) ;
+ $count++ ;
+ }
+ my @allZeroIndices ;#indices of lines consisting of 0's only
+ my @theRest ; #the other lines
+ for my $pair( @rownum ) {
+ my @r = split( // , $pair->[1] ) ; #split the line content
+ if ( all { $_ eq '0' } @r ) {#if it only contains '0'
+ push @allZeroIndices , $pair->[0] ;#grab the line index
+ }
+ else {
+ push @theRest , $pair->[0] ;
+ }
+ }
+ my $maximum = max( @theRest ) ;
+#the maximum of the line indices of lines containing not only 0 must be
+#smaller than all indices of lines containing only 0
+ if ( all { $_ > $maximum } @allZeroIndices ) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+}
+
+#do leading ones follow through from left to right ?
+sub staggeredOnes {
+ my $matrix = shift ;
+ my @leadingOnes ;#positions of leading ones
+ for my $row( @$matrix ) {
+ if ( any { $_ != 0 } @$row ) {
+ my $num = findOnePos( $row ) ;
+ if ( $num != -1 ) { #we found a leading one, see function above
+ push @leadingOnes , $num ;
+ }
+ }
+ }
+ my $len = scalar( @leadingOnes ) ;
+ if ( $len == 0 || $len == 1 ) { #staggered criterion does not apply
+ return 1 ;
+ }
+ else {
+ if ( $len == 2 ) {
+ if ( $leadingOnes[1] > $leadingOnes[0] ) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+ }
+ if ( $len > 2 ) {
+ for my $i (0..$len - 2 ) {
+ if ( $leadingOnes[ $i + 1 ] < $leadingOnes[ $i ] ) {
+ return 0 ;
+ }
+ }
+ return 1 ;
+ }
+ }
+}
+
+#do all leading ones live in a column of 0's only ?
+sub allZeroesInLeadingOneColumn {
+ my $matrix = shift ;
+ my $len = scalar( @$matrix ) ;
+ my @leadingOnePositions ;#contains arrays of rows and columns of leading 1's
+ for my $r( 0..$len - 1 ) {
+ my $row = $matrix->[$r] ;#current row
+ if ( any { $_ != 0 } @$row ) {
+ my $num = findOnePos( $row ) ;
+ if ( $num != -1 ) { #we found a leading 1 position
+ push @leadingOnePositions , [$r , $num] ;
+ }
+ }
+ }
+ for my $pair( @leadingOnePositions ) {
+ for my $i (0..$len - 1 ) {#for all rows in the matrix
+ if ( $pair->[0] != $i ) {#if the current row is not equal to the row
+#number of a leading 1
+ if ( $matrix->[$i]->[$pair->[1]] != 0 ) {#it must be unequal zero
+ return 0 ; #short circuiting if the matrix entry above or below
+#is not 0
+ }
+ }
+ }
+ }
+ return 1 ;
+}
+
+say "Enter some integers, separated by blanks!" ;
+say "Enter <return> to end entry!" ;
+my @matrix ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ) {
+ my @row = split( /\s+/ , $line ) ;
+ push @matrix, \@row ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+#test all conditions
+if ( hasLeadingOne( \@matrix ) && zeroesAtBottom( \@matrix ) && staggeredOnes(
+ \@matrix ) && allZeroesInLeadingOneColumn( \@matrix ) ) {
+ say 1 ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-257/ulrich-rieke/raku/ch-1.raku b/challenge-257/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..43bcf61b3d --- /dev/null +++ b/challenge-257/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 @result ;
+for @numbers -> $num {
+ @result.push( @numbers.grep( { $_ < $num } ).elems ) ;
+}
+say "(" ~ @result.join( ',' ) ~ ")" ;
diff --git a/challenge-257/ulrich-rieke/rust/ch-1.rs b/challenge-257/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..372ad67cc7 --- /dev/null +++ b/challenge-257/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,16 @@ +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 mut result : Vec<usize> = Vec::new( ) ; + for i in &numbers { + let num = numbers.iter( ).filter( |&x| *x < *i ).count( ) ; + result.push( num ) ; + } + println!("{:?}" , result ) ; +} diff --git a/challenge-257/ulrich-rieke/rust/ch-2.rs b/challenge-257/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..eb9a0d1aa2 --- /dev/null +++ b/challenge-257/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,122 @@ +use std::io ; +use std::io::BufRead ; + +fn has_leading_one( matrix : &Vec<Vec<i32>> ) -> bool { + for row in matrix { + let found : Option<i32> = row.iter( ).find( | x | **x != 0 ).copied( ) ; + if found.is_some( ) { + let num = found.unwrap( ) ; + if num != 1 { + return false ; + } + } + } + true +} + +fn zeros_at_bottom( matrix : &Vec<Vec<i32>> ) -> bool { + let enumerated_rows : Vec<(usize, &Vec<i32>)> = matrix.iter( ). + enumerate( ).collect( ) ; + let mut all_zero_indices : Vec<usize> = Vec::new( ) ; + let mut the_rest : Vec<usize> = Vec::new( ) ; + for p in &enumerated_rows { + if p.1.iter( ).all( | x | *x == 0 ) { + all_zero_indices.push( p.0 ) ; + } + else { + the_rest.push( p.0 ) ; + } + } + let maximum = the_rest.iter( ).max().unwrap( ) ; + all_zero_indices.iter( ).all( | d | *d > *maximum ) +} + +fn staggered_ones( matrix : &Vec<Vec<i32>> ) -> bool { + let mut leading_ones : Vec<usize> = Vec::new( ) ; + for row in matrix { + let found : Option<i32> = row.iter( ).find( | x | **x != 0 ).copied( ) ; + if found.is_some( ) { + let num = found.unwrap( ) ; + if num == 1 { + let pos = row.iter( ).position( | &x | x == 1 ).unwrap( ) ; + leading_ones.push( pos ) ; + } + } + } + let len = leading_ones.len( ) ; + if len == 0 || len == 1 { + true + } + else { + if len == 2 { + leading_ones[1] > leading_ones[0] + } + else { + for i in 0..len - 1 { + if leading_ones[ i + 1 ] < leading_ones[ i ] { + return false ; + } + } + return true ; + } + } +} + +fn all_zeroes_in_leading_one_column( matrix : &Vec<Vec<i32>> ) -> bool { + let mut leading_one_positions : Vec<(usize, usize)> = Vec::new( ) ; + let len = matrix.len( ) ; + for i in 0..len { + let row = &matrix[ i ] ; + let found : Option<i32> = row.iter( ).find( |x| **x != 0 ).copied( ) ; + if found.is_some( ) { + let num = found.unwrap( ) ; + if num == 1 { + let pos = row.iter( ).position( | x | *x == 1 ).unwrap( ) ; + leading_one_positions.push( (i , pos) ) ; + } + } + } + leading_one_positions.iter( ).all( |(r , col)| { + for i in 0..len { + if i != *r { + return matrix[i][*col] == 0 ; + } + } + false + }) +} + +fn main() -> io::Result<()> { + println!("Enter some integers, separated by blanks!"); + println!( "Enter <return> to end entry!" ) ; + let mut lines = io::stdin( ).lock( ).lines( ) ; + let mut all_input : String = String::new( ) ; + while let Some( line ) = lines.next( ) { + let last_input = line.unwrap( ) ; + if last_input.len( ) == 0 { + break ; + } + else { + all_input.push_str( &last_input ) ; + all_input.push_str( "\n" ) ; + } + } + let mut matrix : Vec<Vec<i32>> = Vec::new( ) ; + let rows : Vec<&str> = all_input.split( "\n" ).collect( ) ; + for r in &rows { + if r.len( ) != 0 { + let numbers : Vec<i32> = r.split_whitespace( ).map( | s | + s.trim( ).parse::<i32>( ).unwrap( )).collect( ) ; + matrix.push( numbers ) ; + } + } + if has_leading_one( &matrix ) && zeros_at_bottom( &matrix ) && + staggered_ones( &matrix ) && all_zeroes_in_leading_one_column( + &matrix ) { + println!("1") ; + } + else { + println!("0") ; + } + Ok(()) +} |
