forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
- Loading branch information
Showing
25 changed files
with
7,083 additions
and
6,673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) . ")" ) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ")" ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( ' ' ) ~ ")" ) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |
Oops, something went wrong.