aboutsummaryrefslogtreecommitdiff
path: root/challenge-253
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-01-23 12:24:56 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-01-23 12:24:56 +0000
commitb5525a20e03db4d3e70604ea8d853652ccae447f (patch)
treee23bb82a3ffe3b2d2658dc1af3185eff0a82f769 /challenge-253
parent970a3c83f6fa129f05df9f223fb4115e400e4e81 (diff)
downloadperlweeklychallenge-club-b5525a20e03db4d3e70604ea8d853652ccae447f.tar.gz
perlweeklychallenge-club-b5525a20e03db4d3e70604ea8d853652ccae447f.tar.bz2
perlweeklychallenge-club-b5525a20e03db4d3e70604ea8d853652ccae447f.zip
- Added solutions by E. Choroba.
- Added solutions by Dave Jacoby. - Added solutions by Robbie Hatley. - Added solutions by Packy Anderson. - Added solutions by Mariano Spadaccini. - Added solutions by Roger Bell_West. - Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-253')
-rwxr-xr-xchallenge-253/ulrich-rieke/cpp/ch-1.cpp41
-rwxr-xr-xchallenge-253/ulrich-rieke/cpp/ch-2.cpp50
-rwxr-xr-xchallenge-253/ulrich-rieke/haskell/ch-1.hs15
-rwxr-xr-xchallenge-253/ulrich-rieke/haskell/ch-2.hs38
-rwxr-xr-xchallenge-253/ulrich-rieke/perl/ch-1.pl25
-rwxr-xr-xchallenge-253/ulrich-rieke/perl/ch-2.pl32
-rwxr-xr-xchallenge-253/ulrich-rieke/raku/ch-1.raku21
-rwxr-xr-xchallenge-253/ulrich-rieke/rust/ch-1.rs25
-rwxr-xr-xchallenge-253/ulrich-rieke/rust/ch-2.rs49
9 files changed, 296 insertions, 0 deletions
diff --git a/challenge-253/ulrich-rieke/cpp/ch-1.cpp b/challenge-253/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..eda7831788
--- /dev/null
+++ b/challenge-253/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,41 @@
+#include <vector>
+#include <string>
+#include <iostream>
+#include <algorithm>
+#include <iterator>
+
+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 some strings, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> strings { split( line, " " ) } ;
+ std::cout << "Enter a separator!\n" ;
+ std::string separator ;
+ std::getline( std::cin, separator ) ;
+ std::vector<std::string> result ;
+ for ( auto & s : strings ) {
+ std::vector<std::string> parts { split( s , separator ) } ;
+ for ( auto & part : parts ) {
+ if ( part.length( ) > 0 )
+ result.push_back( part ) ;
+ }
+ }
+ std::cout << "( " ;
+ std::copy( result.begin( ) , result.end( ) ,
+ std::ostream_iterator<std::string>(std::cout , " " ) ) ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-253/ulrich-rieke/cpp/ch-2.cpp b/challenge-253/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..145c231367
--- /dev/null
+++ b/challenge-253/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,50 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+#include <utility>
+
+bool mySorter( const std::pair<int, std::vector<int>> & ,
+ const std::pair<int, std::vector<int>> & ) ;
+
+
+int main( ) {
+ std::cout << "Enter some lines of 1 and 0, beginning with 1!\n" ;
+ std::cout << "Enter a negative number to end line entry!\n" ;
+ std::cout << "How many lines do you want to enter?\n" ;
+ int lines ;
+ std::cin >> lines ;
+ std::vector<std::pair<int , std::vector<int>>> matrix ;
+ int count = 0 ;
+ while ( count < lines ) {
+ std::vector<int> numbers ;
+ int nextnum ;
+ std::cin >> nextnum ;
+ while ( nextnum > -1 ) {
+ numbers.push_back( nextnum ) ;
+ std::cin >> nextnum ;
+ }
+ matrix.push_back( std::make_pair( count , numbers ) ) ;
+ count++ ;
+ }
+ std::sort( matrix.begin( ) , matrix.end( ) , mySorter ) ;
+ std::cout << "(" ;
+ for ( const auto & p : matrix ) {
+ std::cout << p.first << " " ;
+ }
+ std::cout << ")\n" ;
+ return 0 ;
+}
+
+bool mySorter( const std::pair<int, std::vector<int>> & someFirst ,
+ const std::pair<int, std::vector<int>> & someSecond ) {
+ int ones_a = std::count( someFirst.second.begin( ) ,
+ someFirst.second.end( ) , 1 ) ;
+ int ones_b = std::count( someSecond.second.begin( ) ,
+ someSecond.second.end( ) , 1 ) ;
+ if ( ones_a != ones_b ) {
+ return ones_a < ones_b ;
+ }
+ else {
+ return someFirst.first < someSecond.first ;
+ }
+}
diff --git a/challenge-253/ulrich-rieke/haskell/ch-1.hs b/challenge-253/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..742b20896e
--- /dev/null
+++ b/challenge-253/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,15 @@
+module Challenge253
+ where
+import Data.List.Split( splitOn )
+
+solution :: String -> String -> [String]
+solution line separator = filter ( not . null ) $ concat $ map
+ (\w -> splitOn separator w ) $ words line
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some strings, separated by blanks!" ;
+ strings <- getLine
+ putStrLn "Enter a separator!"
+ separator <- getLine
+ print $ solution strings separator
diff --git a/challenge-253/ulrich-rieke/haskell/ch-2.hs b/challenge-253/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..db22b6f60e
--- /dev/null
+++ b/challenge-253/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,38 @@
+module Challenge253_2
+ where
+import Data.List ( sortBy )
+
+count :: Eq a => a -> [a] -> Int
+count _ [] = 0
+count d (x:xs)
+ |d == x = 1 + count d xs
+ |otherwise = count d xs
+
+solution :: [[Int]] -> [Int]
+solution list = map fst $ sortBy mySorter $ zip [0..] list
+ where
+ mySorter :: (Int ,[Int]) -> (Int, [Int]) -> Ordering
+ mySorter firstList secondList = if firstOnes /= secondOnes then
+ compare firstOnes secondOnes else compare ( fst firstList )
+ ( fst secondList )
+ where
+ firstOnes = count 1 ( snd firstList )
+ secondOnes = count 1 ( snd secondList )
+
+getMultipleLines :: Int -> IO [String]
+getMultipleLines n
+ |n <= 0 = return []
+ |otherwise = do
+ x <- getLine
+ xs <- getMultipleLines ( n - 1 )
+ let ret = (x:xs)
+ return ret
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a matrix of 1 and 0 only, beginning with 1's in line!"
+ putStrLn "How many lines do you want to enter ?"
+ myLines <- getLine
+ allNumbers <- getMultipleLines ( read myLines )
+ let matrix = map ( map read . words ) allNumbers
+ print $ solution matrix
diff --git a/challenge-253/ulrich-rieke/perl/ch-1.pl b/challenge-253/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..294e5edae9
--- /dev/null
+++ b/challenge-253/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some strings, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @strings = split( /\s/ , $line ) ;
+say "Enter a separator!" ;
+my $sep = <STDIN> ;
+chomp $sep ;
+my @result ;
+for my $str( @strings ) {
+ my $pos = index( $str , $sep ) ;
+ while ( $pos != -1 ) {
+ substr( $str , $pos , 1 ) = " " ;
+ $pos = index( $str , $sep , $pos + 1) ;
+ }
+ my @parts = split( /\s/ , $str ) ;
+ for my $p ( @parts ) {
+ push @result, $p ;
+ }
+}
+say ( "(" . join( ',' , grep {length $_ > 0 } @result ) . ")" ) ;
diff --git a/challenge-253/ulrich-rieke/perl/ch-2.pl b/challenge-253/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..0fa620e08c
--- /dev/null
+++ b/challenge-253/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub count {
+ my $numbers = shift ;
+ return scalar( grep { $_ == 1 } @$numbers ) ;
+}
+
+sub mySorter {
+ count( $a->[1] ) <=> count( $b->[1] ) || $a->[0] <=> $b->[0] ;
+}
+
+say "Enter some lines of 1 and 0, starting with 1, <return> to end!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @matrix ;
+my $current = 0 ;
+while ( $line ) {
+ my @numbers = split( /\s/ , $line ) ;
+ push( @matrix, [ $current , \@numbers ] ) ;
+ $current++ ;
+ $line = <STDIN> ;
+ chomp $line ;
+}
+my @sorted = sort mySorter @matrix ;
+print "( " ;
+for my $p ( @sorted ) {
+ print "$p->[0] " ;
+}
+say ")" ;
diff --git a/challenge-253/ulrich-rieke/raku/ch-1.raku b/challenge-253/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..0ac00ff74f
--- /dev/null
+++ b/challenge-253/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,21 @@
+use v6 ;
+
+say "Enter some strings, separated by whitespace!" ;
+my $line = $*IN.get ;
+my @strings = $line.words ;
+say "Enter a separator!" ;
+my $sep = $*IN.get ;
+my @result ;
+for @strings <-> $str {
+ if ( $str ~~ /$sep/ ) {
+ $str ~~ s:g/$sep/ / ;
+ my @parts = $str.words ;
+ for @parts -> $p {
+ @result.push( $p ) ;
+ }
+ }
+ else {
+ @result.push( $str ) ;
+ }
+}
+say ( "(" ~ @result.join( ' ' ) ~ ")" ) ;
diff --git a/challenge-253/ulrich-rieke/rust/ch-1.rs b/challenge-253/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..28f3d4046b
--- /dev/null
+++ b/challenge-253/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,25 @@
+use std::io ;
+
+fn main() {
+ println!("Enter some strings, separated by whitespace!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = &*inline ;
+ let strings : Vec<&str> = entered_line.split_whitespace( ).map(
+ | s | s.trim( ) ).collect( ) ;
+ println!("Enter a separator!") ;
+ let mut sep_line : String = String::new( ) ;
+ io::stdin( ).read_line( &mut sep_line ).unwrap( ) ;
+ let separator : &str = &*sep_line ;
+ let changed = separator.trim( ) ;
+ let mut result : Vec<&str> = Vec::new( ) ;
+ for s in &strings {
+ let v : Vec<&str> = s.split( changed ).collect( ) ;
+ for substr in &v {
+ if substr.len( ) > 0 {
+ result.push( substr ) ;
+ }
+ }
+ }
+ println!("{:?}" , result ) ;
+}
diff --git a/challenge-253/ulrich-rieke/rust/ch-2.rs b/challenge-253/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..856a224a00
--- /dev/null
+++ b/challenge-253/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,49 @@
+use std::io ;
+use std::io::BufRead ;
+
+fn main() -> io::Result<()> {
+ println!("Enter n rows of m binary digits, 1 before 0!");
+ println!("Enter <return> to end!") ;
+ 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 rows : Vec<&str> = all_input.split( "\n").collect( ) ;
+ let mut matrix : Vec<Vec<u8>> = Vec::new( ) ;
+ for r in &rows {
+ if r.len( ) > 0 {
+ let row_nums : Vec<u8> = r.split_whitespace( ).map( | s |
+ s.trim( ).parse::<u8>( ).unwrap( ) ).collect( ) ;
+ matrix.push( row_nums ) ;
+ }
+ }
+ let mut sorted_rows : Vec<(usize , &Vec<u8>)> = Vec::new( ) ;
+ let mut iter = matrix.iter( ).enumerate( ) ;
+ while let Some( pair ) = iter.next( ) {
+ sorted_rows.push( pair ) ;
+ }
+ let for_sorting = sorted_rows.as_mut_slice( ) ;
+ for_sorting.sort_by( | a , b| {
+ let ones_a = a.1.iter( ).filter( | d | **d == 1 ).count( ) ;
+ let ones_b = b.1.iter( ).filter( | d | **d == 1 ).count( ) ;
+ if ones_a != ones_b {
+ return ones_a.cmp( &ones_b ) ;
+ }
+ else {
+ return a.0.cmp( &b.0 ) ;
+ }
+ }) ;
+ for pair in for_sorting {
+ print!("{} " , pair.0 ) ;
+ }
+ println!("") ;
+ Ok(())
+}