From 153f22e7dc6b1860d8561da89efeeefdfa83eab9 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 5 Aug 2025 11:55:34 +0100 Subject: - Added solutions by Lukas Mai. - Added solutions by E. Choroba. --- challenge-333/conor-hoekstra/bqn/ch-1.bqn | 13 +++++++++ challenge-333/conor-hoekstra/bqn/ch-2.bqn | 19 ++++++++++++ challenge-333/conor-hoekstra/ch-1.bqn | 13 --------- challenge-333/conor-hoekstra/ch-2.bqn | 19 ------------ challenge-333/eric-cheung/python/ch-1.py | 26 +++++++++++++++++ challenge-333/eric-cheung/python/ch-2.py | 17 +++++++++++ challenge-333/ulrich-rieke/cpp/ch-1.cpp | 47 ++++++++++++++++++++++++++++++ challenge-333/ulrich-rieke/cpp/ch-2.cpp | 39 +++++++++++++++++++++++++ challenge-333/ulrich-rieke/haskell/ch-1.hs | 36 +++++++++++++++++++++++ challenge-333/ulrich-rieke/haskell/ch-2.hs | 13 +++++++++ challenge-333/ulrich-rieke/perl/ch-1.pl | 43 +++++++++++++++++++++++++++ challenge-333/ulrich-rieke/perl/ch-2.pl | 21 +++++++++++++ challenge-333/ulrich-rieke/raku/ch-1.raku | 27 +++++++++++++++++ challenge-333/ulrich-rieke/raku/ch-2.raku | 17 +++++++++++ challenge-333/ulrich-rieke/rust/ch-1.rs | 35 ++++++++++++++++++++++ challenge-333/ulrich-rieke/rust/ch-2.rs | 22 ++++++++++++++ 16 files changed, 375 insertions(+), 32 deletions(-) create mode 100644 challenge-333/conor-hoekstra/bqn/ch-1.bqn create mode 100644 challenge-333/conor-hoekstra/bqn/ch-2.bqn delete mode 100644 challenge-333/conor-hoekstra/ch-1.bqn delete mode 100644 challenge-333/conor-hoekstra/ch-2.bqn create mode 100755 challenge-333/eric-cheung/python/ch-1.py create mode 100755 challenge-333/eric-cheung/python/ch-2.py create mode 100755 challenge-333/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-333/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-333/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-333/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-333/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-333/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-333/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-333/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-333/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-333/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-333') diff --git a/challenge-333/conor-hoekstra/bqn/ch-1.bqn b/challenge-333/conor-hoekstra/bqn/ch-1.bqn new file mode 100644 index 0000000000..95dfaa217b --- /dev/null +++ b/challenge-333/conor-hoekstra/bqn/ch-1.bqn @@ -0,0 +1,13 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/333-1.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +StraightLine ← { ∧´≡´¨(⌽-´2↑𝕩)⊸ר(⊑𝕩)⊸-¨(1↓𝕩) } + +# Tests +u.UnitTest (StraightLine ⟨⟨2, 1⟩, ⟨2, 3⟩, ⟨2, 5⟩⟩) ≡ 1 +u.UnitTest (StraightLine ⟨⟨1, 4⟩, ⟨3, 4⟩, ⟨10, 4⟩⟩) ≡ 1 +u.UnitTest (StraightLine ⟨⟨0, 0⟩, ⟨1, 1⟩, ⟨2, 3⟩⟩) ≡ 0 +u.UnitTest (StraightLine ⟨⟨1, 1⟩, ⟨1, 1⟩, ⟨1, 1⟩⟩) ≡ 1 +u.UnitTest (StraightLine ⟨⟨1000000, 1000000⟩, ⟨2000000, 2000000⟩, ⟨3000000, 3000000⟩⟩) ≡ 1 diff --git a/challenge-333/conor-hoekstra/bqn/ch-2.bqn b/challenge-333/conor-hoekstra/bqn/ch-2.bqn new file mode 100644 index 0000000000..9e1c5a24bf --- /dev/null +++ b/challenge-333/conor-hoekstra/bqn/ch-2.bqn @@ -0,0 +1,19 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/333-2.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +DuplicateZeros ← { (≠𝕩)↑𝕩/˜1+0=𝕩 } # Explicit +DuplicateZeros2 ← ≠↑(1+0=⊢)⊸/ # Tacit + +# Tests +u.UnitTest (DuplicateZeros ⟨1, 0, 2, 3, 0, 4, 5, 0⟩) ≡ ⟨1, 0, 0, 2, 3, 0, 0, 4⟩ +u.UnitTest (DuplicateZeros ⟨1, 2, 3⟩) ≡ ⟨1, 2, 3⟩ +u.UnitTest (DuplicateZeros ⟨1, 2, 3, 0⟩) ≡ ⟨1, 2, 3, 0⟩ +u.UnitTest (DuplicateZeros ⟨0, 0, 1, 2⟩) ≡ ⟨0, 0, 0, 0⟩ +u.UnitTest (DuplicateZeros ⟨1, 2, 0, 3, 4⟩) ≡ ⟨1, 2, 0, 0, 3⟩ +u.UnitTest (DuplicateZeros2 ⟨1, 0, 2, 3, 0, 4, 5, 0⟩) ≡ ⟨1, 0, 0, 2, 3, 0, 0, 4⟩ +u.UnitTest (DuplicateZeros2 ⟨1, 2, 3⟩) ≡ ⟨1, 2, 3⟩ +u.UnitTest (DuplicateZeros2 ⟨1, 2, 3, 0⟩) ≡ ⟨1, 2, 3, 0⟩ +u.UnitTest (DuplicateZeros2 ⟨0, 0, 1, 2⟩) ≡ ⟨0, 0, 0, 0⟩ +u.UnitTest (DuplicateZeros2 ⟨1, 2, 0, 3, 4⟩) ≡ ⟨1, 2, 0, 0, 3⟩ diff --git a/challenge-333/conor-hoekstra/ch-1.bqn b/challenge-333/conor-hoekstra/ch-1.bqn deleted file mode 100644 index 95dfaa217b..0000000000 --- a/challenge-333/conor-hoekstra/ch-1.bqn +++ /dev/null @@ -1,13 +0,0 @@ -# For up to date code: -# https://github.com/codereport/bqn-code/blob/main/pwc/333-1.bqn - -u ⇐ •Import "/home/cph/bqn-test/test.bqn" - -StraightLine ← { ∧´≡´¨(⌽-´2↑𝕩)⊸ר(⊑𝕩)⊸-¨(1↓𝕩) } - -# Tests -u.UnitTest (StraightLine ⟨⟨2, 1⟩, ⟨2, 3⟩, ⟨2, 5⟩⟩) ≡ 1 -u.UnitTest (StraightLine ⟨⟨1, 4⟩, ⟨3, 4⟩, ⟨10, 4⟩⟩) ≡ 1 -u.UnitTest (StraightLine ⟨⟨0, 0⟩, ⟨1, 1⟩, ⟨2, 3⟩⟩) ≡ 0 -u.UnitTest (StraightLine ⟨⟨1, 1⟩, ⟨1, 1⟩, ⟨1, 1⟩⟩) ≡ 1 -u.UnitTest (StraightLine ⟨⟨1000000, 1000000⟩, ⟨2000000, 2000000⟩, ⟨3000000, 3000000⟩⟩) ≡ 1 diff --git a/challenge-333/conor-hoekstra/ch-2.bqn b/challenge-333/conor-hoekstra/ch-2.bqn deleted file mode 100644 index 9e1c5a24bf..0000000000 --- a/challenge-333/conor-hoekstra/ch-2.bqn +++ /dev/null @@ -1,19 +0,0 @@ -# For up to date code: -# https://github.com/codereport/bqn-code/blob/main/pwc/333-2.bqn - -u ⇐ •Import "/home/cph/bqn-test/test.bqn" - -DuplicateZeros ← { (≠𝕩)↑𝕩/˜1+0=𝕩 } # Explicit -DuplicateZeros2 ← ≠↑(1+0=⊢)⊸/ # Tacit - -# Tests -u.UnitTest (DuplicateZeros ⟨1, 0, 2, 3, 0, 4, 5, 0⟩) ≡ ⟨1, 0, 0, 2, 3, 0, 0, 4⟩ -u.UnitTest (DuplicateZeros ⟨1, 2, 3⟩) ≡ ⟨1, 2, 3⟩ -u.UnitTest (DuplicateZeros ⟨1, 2, 3, 0⟩) ≡ ⟨1, 2, 3, 0⟩ -u.UnitTest (DuplicateZeros ⟨0, 0, 1, 2⟩) ≡ ⟨0, 0, 0, 0⟩ -u.UnitTest (DuplicateZeros ⟨1, 2, 0, 3, 4⟩) ≡ ⟨1, 2, 0, 0, 3⟩ -u.UnitTest (DuplicateZeros2 ⟨1, 0, 2, 3, 0, 4, 5, 0⟩) ≡ ⟨1, 0, 0, 2, 3, 0, 0, 4⟩ -u.UnitTest (DuplicateZeros2 ⟨1, 2, 3⟩) ≡ ⟨1, 2, 3⟩ -u.UnitTest (DuplicateZeros2 ⟨1, 2, 3, 0⟩) ≡ ⟨1, 2, 3, 0⟩ -u.UnitTest (DuplicateZeros2 ⟨0, 0, 1, 2⟩) ≡ ⟨0, 0, 0, 0⟩ -u.UnitTest (DuplicateZeros2 ⟨1, 2, 0, 3, 4⟩) ≡ ⟨1, 2, 0, 0, 3⟩ diff --git a/challenge-333/eric-cheung/python/ch-1.py b/challenge-333/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..59ee932762 --- /dev/null +++ b/challenge-333/eric-cheung/python/ch-1.py @@ -0,0 +1,26 @@ + +import numpy as np + +def GetSlope (arrPt_1, arrPt_2): + dX1, dY1 = arrPt_1 + dX2, dY2 = arrPt_2 + + if dX1 == dX2: + return np.nan + + return (dY2 - dY1) / (dX2 - dX1) + +## arrList = [[2, 1], [2, 3], [2, 5]] ## Example 1 +## arrList = [[1, 4], [3, 4], [10, 4]] ## Example 2 +## arrList = [[0, 0], [1, 1], [2, 3]] ## Example 3 +## arrList = [[1, 1], [1, 1], [1, 1]] ## Example 4 +arrList = [[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]] ## Example 5 + +arrX = [arrLoop[0] for arrLoop in arrList] +arrY = [arrLoop[1] for arrLoop in arrList] + +if len(set(arrX)) == 1 or len(set(arrY)) == 1: + print (True) +else: + arrSlope = [GetSlope(arrList[nIndx - 1], arrList[nIndx]) for nIndx in range(1, len(arrList))] + print (len(set(arrSlope)) == 1) diff --git a/challenge-333/eric-cheung/python/ch-2.py b/challenge-333/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..7e96b1df40 --- /dev/null +++ b/challenge-333/eric-cheung/python/ch-2.py @@ -0,0 +1,17 @@ + +## arrInt = [1, 0, 2, 3, 0, 4, 5, 0] ## Example 1 +## arrInt = [1, 2, 3] ## Example 2 +## arrInt = [1, 2, 3, 0] ## Example 3 +## arrInt = [0, 0, 1, 2] ## Example 4 +arrInt = [1, 2, 0, 3, 4] ## Example 5 + +arrOut = arrInt[:] + +arrZeroPos = [nPos for nPos, nElem in enumerate(arrInt) if nElem == 0] + +if len(arrZeroPos) == 0: + print (arrOut) +else: + for nPos in arrZeroPos[::-1]: + arrOut.insert(nPos, 0) + print (arrOut[:len(arrInt)]) diff --git a/challenge-333/ulrich-rieke/cpp/ch-1.cpp b/challenge-333/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..dff850ba55 --- /dev/null +++ b/challenge-333/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +using namespace std::string_literals ; + +bool on_straight_line( const std::vector> & coords ) { + int first_x = coords[0].first ; + int first_y = coords[0].second ; + if ( std::all_of( coords.begin( ) , coords.end( ) , [first_x]( auto p ) { + return p.first == first_x ; } ) || std::all_of( coords.begin( ) , + coords.end( ) , [first_y]( auto b ) { return b.second == + first_y ; })) + return true ; + else { + double slope_one = (static_cast(coords[1].second) - + static_cast(coords[0].second )) / (static_cast( + coords[1].first) - static_cast(coords[0].first )) ; + double slope_two = (static_cast(coords[2].second) - + static_cast(coords[1].second )) / (static_cast( + coords[2].first) - static_cast(coords[1].first )) ; + return slope_one == slope_two ; + } +} + +int main( ) { + std::cout << "Enter 3 coordinates in brackets!\n" ; + std::string inputline ; + std::getline( std::cin , inputline ) ; + auto pattern {R"(\d+)"s} ; + auto re = std::regex { pattern } ; + std::vector> coordinates ; + std::vector numbers ; + auto end = std::sregex_token_iterator{} ; + for ( auto it = std::sregex_token_iterator { std::begin( inputline ) , + std::end( inputline ) , re } ; it != end ; ++it ) { + numbers.push_back( *it ) ; + } + for ( int i = 0 ; i < numbers.size( ) - 1 ; i += 2) { + coordinates.push_back( std::make_pair( std::stoi( numbers[i] ) , + std::stoi( numbers[i + 1] ) )) ; + } + std::cout << std::boolalpha << on_straight_line( coordinates ) << '\n' ; + return 0 ; +} diff --git a/challenge-333/ulrich-rieke/cpp/ch-2.cpp b/challenge-333/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..2894348b28 --- /dev/null +++ b/challenge-333/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,39 @@ +#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 whitespace!\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 afterDoubling ; + for ( auto it = numbers.begin( ) ; it != numbers.end( ) ; ++it ) { + if ( *it == 0 ) { + afterDoubling.push_back( 0 ) ; + afterDoubling.push_back( 0 ) ; + } + else + afterDoubling.push_back( *it ) ; + } + std::cout << "( " ; + for ( int i = 0 ; i < numbers.size( ) ; i++ ) { + std::cout << afterDoubling[i] << ' ' ; + } + std::cout << ")\n" ; + return 0 ; +} + diff --git a/challenge-333/ulrich-rieke/haskell/ch-1.hs b/challenge-333/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..8100b4a2fb --- /dev/null +++ b/challenge-333/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,36 @@ +module Challenge333 + where +import Data.Char( isDigit ) +import Data.List.Split ( chunksOf ) +import Data.List ( (!!) ) + +parseEntryLine :: String -> [(Int , Int)] +parseEntryLine line = + let parts = words line + numbers = map (filter isDigit ) parts + relevant = filter ( not . null ) numbers + nums = map read relevant + coords = chunksOf 2 nums + in map (\subli -> (head subli , last subli) ) coords + +onStraightLine :: [(Int , Int)] -> Bool +onStraightLine coordinates = all (\p -> fst p == ( fst $ head coordinates )) + coordinates || all (\p -> snd p == ( snd $ head coordinates )) coordinates || + slopeOne == slopeTwo + where + slopeOne :: Double + slopeOne = (( fromIntegral $ snd ( coordinates !! 1 ) ) - ( fromIntegral $ + snd ( coordinates !! 0 ) )) / (( fromIntegral $ fst ( coordinates !! 1 )) - + (fromIntegral $ fst ( coordinates !! 0 ) ) ) + slopeTwo = (( fromIntegral $ snd ( coordinates !! 2 ) ) - ( fromIntegral $ + snd ( coordinates !! 1 ) )) / (( fromIntegral $ fst ( coordinates !! 2 )) - + (fromIntegral $ fst ( coordinates !! 1 ) ) ) + +main :: IO ( ) +main = do + putStrLn "Enter some coordinates in brackets separated by comma and space!" + numberLine <- getLine + print $ onStraightLine $ parseEntryLine numberLine + + + diff --git a/challenge-333/ulrich-rieke/haskell/ch-2.hs b/challenge-333/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..0927419ce5 --- /dev/null +++ b/challenge-333/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,13 @@ +module Challenge333_2 + where + +solution :: [Int] -> [Int] +solution list = take ( length list ) $ foldl1 ( ++ ) $ map (\i -> if i == 0 + then replicate 2 0 else [i] ) list + + +main :: IO ( ) +main = do + putStrLn "Enter some integers separated by whitespace!" + numberline <- getLine + print $ solution $ map read $ words numberline diff --git a/challenge-333/ulrich-rieke/perl/ch-1.pl b/challenge-333/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..89e0b9769e --- /dev/null +++ b/challenge-333/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some coordinates in brackets!" ; +my $line = ; +chomp $line ; +my @parts = split( /\]\s*\,\s*\[/ , $line ) ; +my @pairs ; +for my $pair( @parts ) { + if ( $pair =~ /(\d+)\s*\,\s*(\d+)/ ) { + push( @pairs , [$1 , $2] ) ; + } +} +if ( on_straight_line(\@pairs)) { + say "true" ; +} +else { + say "false" ; +} + +sub on_straight_line { + my $coords = shift ; + if (($coords->[1]->[1] == $coords->[0]->[1] && $coords->[2]->[1] == + $coords->[1]->[1] ) || ( $coords->[1]->[0] == $coords->[0]->[0] && + $coords->[2]->[0] == $coords->[1]->[0] )) { + return 1 ; + } + else { + my $slope_one = ($coords->[1]->[1] - $coords->[0]->[1] ) / + ($coords->[1]->[0] - $coords->[0]->[0] ) ; + my $slope_two = ( $coords->[2]->[1] - $coords->[1]->[1] ) / ( + $coords->[2]->[0] - $coords->[1]->[0] ) ; + if ( $slope_one == $slope_two ) { + return 1 ; + } + else { + return 0 ; + } + } +} + diff --git a/challenge-333/ulrich-rieke/perl/ch-2.pl b/challenge-333/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..9a91827e69 --- /dev/null +++ b/challenge-333/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some integers separated by whitespace!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s+/ , $line ) ; +my @after_doubling ; +my $len = scalar( @numbers ) ; +for my $n ( @numbers ) { + if ( $n == 0 ) { + push( @after_doubling , 0 ) ; + push( @after_doubling , 0 ) ; + } + else { + push( @after_doubling , $n ) ; + } +} +say join( ',' , @after_doubling[0..$len - 1] ) ; diff --git a/challenge-333/ulrich-rieke/raku/ch-1.raku b/challenge-333/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..e713d999d9 --- /dev/null +++ b/challenge-333/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,27 @@ +use v6 ; + +say "Enter 3 coordinate pairs in brackets!" ; +my $line = $*IN.get ; +my @pairs ; +my @parts = $line.split( /\] ',' \s+\[/ ) ; +for @parts -> $pair { + if ($pair ~~ /(\d+)\s* ',' \s* (\d+) / ) { + my $numberpair = [+$0 , +$1] ; + @pairs.push( $numberpair ) ; + } +} +say on_straight_line( @pairs ) ; + +sub on_straight_line( @numbers ) { + if (@numbers[0][0] == @numbers[1][0] && @numbers[1][0] == @numbers[2][0] || + @numbers[0][1] == @numbers[1][1] && @numbers[1][1] == @numbers[2][1] ) { + return True ; + } + else { + my $slope_one = (@numbers[1][1] - @numbers[0][1]) / (@numbers[1][0] - + @numbers[0][0] ) ; + my $slope_two = (@numbers[2][1] - @numbers[1][1] ) / (@numbers[2][0] - + @numbers[1][0] ) ; + return $slope_one == $slope_two ; + } +} diff --git a/challenge-333/ulrich-rieke/raku/ch-2.raku b/challenge-333/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..ae856c1613 --- /dev/null +++ b/challenge-333/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,17 @@ +use v6 ; + +say "Enter some integers separated by whitespace!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my @after_doubling ; +for @numbers -> $n { + if ( $n == 0 ) { + @after_doubling.push( 0 ) ; + @after_doubling.push( 0 ) ; + } + else { + @after_doubling.push( $n ) ; + } +} +my $len = @numbers.elems ; +say @after_doubling[0..$len - 1].join( ',' ) ; diff --git a/challenge-333/ulrich-rieke/rust/ch-1.rs b/challenge-333/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..7a6096aad0 --- /dev/null +++ b/challenge-333/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,35 @@ +use std::io ; +use regex::Regex ; + +fn parse_pair( bracket : &str ) -> (i32 , i32) { + let re = Regex::new( r"([0-9]+)").unwrap( ) ; + let numbers : Vec = re.find_iter( bracket ).map( |m| { + let found = m.as_str( ) ; + let num : i32 = found.parse::( ).unwrap( ) ; + num + }).collect( ) ; + (numbers[0] , numbers[1]) +} + +fn on_straight_line( points : Vec<(i32 , i32)> ) -> bool { + points.iter( ).all( |p| p.0 == points[0].0 || p.1 == points[0].1 ) || + { let first_slope : f32 = ((points[1].1 as f32) - (points[0].1 as f32)) / + ((points[1].0 as f32) - (points[0].0 as f32 )) ; + let second_slope : f32 = ((points[2].1 as f32 ) - (points[1].1 as f32 )) / + ((points[2].0 as f32) - ( points[1].0 as f32 ) ) ; + first_slope == second_slope + } +} + +fn main() { + println!("Enter 3 coordinates as a pair of integers in brackets!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let re = Regex::new( r"\[\s*([0-9]+)\s*\,\s*([0-9]+)\]").unwrap( ) ; + let trimmed = inline.trim( ) ; + let parts : Vec<&str> = re.find_iter(trimmed).map( |m| m.as_str( )). + collect( ) ; + let coordinates : Vec<(i32 , i32)> = parts.into_iter( ).map( |s| + parse_pair( s ) ).collect( ) ; + println!("{}" , on_straight_line( coordinates ) ) ; +} diff --git a/challenge-333/ulrich-rieke/rust/ch-2.rs b/challenge-333/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..97b83455dd --- /dev/null +++ b/challenge-333/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,22 @@ +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 after_doubling : Vec = Vec::new( ) ; + let len : usize = numbers.len( ) ; + for n in numbers { + if n == 0 { + after_doubling.push( 0 ) ; + after_doubling.push( 0 ) ; + } + else { + after_doubling.push( n ) ; + } + } + println!("{:?}" , after_doubling.into_iter( ).take( len ). + collect::>( ) ) ; +} -- cgit