diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-07-06 02:45:38 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-07-06 02:45:38 +0100 |
| commit | a82fe1af01de90b3ef40cf057cac87063168bb0a (patch) | |
| tree | 2af0d3ed217495264a9a7457b6824a6290dbb954 | |
| parent | a137213a1fc4ffcc1a212bcac3c078f5f0cf80a8 (diff) | |
| download | perlweeklychallenge-club-a82fe1af01de90b3ef40cf057cac87063168bb0a.tar.gz perlweeklychallenge-club-a82fe1af01de90b3ef40cf057cac87063168bb0a.tar.bz2 perlweeklychallenge-club-a82fe1af01de90b3ef40cf057cac87063168bb0a.zip | |
- Added solutions by Robert DiCicco.
- Added solutions by Ulrich Rieke.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Thomas Kohler.
- Added solutions by Simon Proctor.
- Added solutions by Mark Anderson.
- Added solutions by W. Luis Mochan.
- Added solutions by Jorg Sommrey.
- Added solutions by David Ferrone.
- Added solutions by Andread Voegele.
- Added solutions by E. Choroba.
- Added solutions by Peter Campbell Smith.
- Added solutions by Steven Wilson.
- Added solutions by Avery Adams.
- Added solutions by Lubos Kolouch.
- Added solutions by Roger Bell_West.
38 files changed, 3166 insertions, 2133 deletions
diff --git a/challenge-224/avery-adams/blog.txt b/challenge-224/avery-adams/blog.txt new file mode 100644 index 0000000000..064f2e0f74 --- /dev/null +++ b/challenge-224/avery-adams/blog.txt @@ -0,0 +1 @@ +https://dev.to/oldtechaa/perl-weekly-challenge-224-passing-notes-3kp5 diff --git a/challenge-224/avery-adams/blogs.txt b/challenge-224/avery-adams/blog1.txt index 337cd77557..91ed1fa77f 100644 --- a/challenge-224/avery-adams/blogs.txt +++ b/challenge-224/avery-adams/blog1.txt @@ -1,2 +1 @@ -https://dev.to/oldtechaa/perl-weekly-challenge-224-passing-notes-3kp5 https://blogs.perl.org/users/oldtechaa/2023/07/perl-weekly-challenge-224---passing-notes.html diff --git a/challenge-224/eric-cheung/python/ch-1.py b/challenge-224/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..d094896b54 --- /dev/null +++ b/challenge-224/eric-cheung/python/ch-1.py @@ -0,0 +1,17 @@ +
+## Example 1
+## strSource = "abc"
+## strTarget = "xyz"
+
+## Example 2
+## strSource = "scriptinglanguage"
+## strTarget = "perl"
+
+## Example 3
+strSource = "aabbcc"
+strTarget = "abc"
+
+arrSrcSplit = [charLoop for charLoop in strSource]
+arrTargetSplit = [charLoop for charLoop in strTarget]
+
+print (set(arrTargetSplit).issubset(set(arrSrcSplit)))
diff --git a/challenge-224/eric-cheung/python/ch-2.py b/challenge-224/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..8f0bbc99ee --- /dev/null +++ b/challenge-224/eric-cheung/python/ch-2.py @@ -0,0 +1,87 @@ +
+## Remarks
+## https://www.geeksforgeeks.org/string-with-additive-sequence-set-2/
+
+## python3 code to implement the approach
+
+## Variable to store the result
+bResult = False
+arrStr = []
+
+## Function to check the additive sequence
+def check_additive(strIn, subStrIn):
+ global bResult
+ global arrStr
+
+ ## If the end is reached and vector consists of more than 2 numbers, then one of the possible solution is found
+ if (subStrIn == len(strIn) and len(arrStr) > 2):
+
+ ## Mark the bResult as true to indicate the solution is found and to avoid for trying the rest of the combinations
+ bResult = True
+ return
+
+ nInt_01, nInt_02, nInt_03 = 0, 0, 0
+
+ if (len(arrStr) >= 2):
+ ## Store the previous two numbers of the sequence to check the additive sequence property for the next number
+ nInt_02 = arrStr[len(arrStr) - 1]
+ nInt_01 = arrStr[len(arrStr) - 2]
+
+ for nIndxLoop in range(subStrIn, len(strIn)):
+
+ ## Generate the number
+ nInt_03 = nInt_03 * 10 + ord(strIn[nIndxLoop]) - ord("0")
+
+ ## Try all the possible ways to generate the first two numbers
+ ## i.e. if vector consists of less than two numbers and no solution is found yet
+ if (len(arrStr) < 2):
+ arrStr.append(nInt_03)
+ check_additive(strIn, nIndxLoop + 1)
+
+ ## Pop the value to try for the other combination
+ arrStr.pop()
+
+ ## If the number generated so far is not equal the sum of previous two numbers in the sequence
+ ## then it cannot be a part of additive sequence hence no need to proceed further
+
+ return
+
+ if (nInt_03 != nInt_01 + nInt_02):
+ return
+
+ ## If the number generated so far is equal to the sum of previous two numbers
+ ## then it can be a part of additive sequence
+ ## push it into vector and check for remaining string
+
+ ## Store it in the vector
+ arrStr.append(nInt_03)
+
+ ## Recur for remaining string
+ check_additive(strIn, nIndxLoop + 1)
+
+ ## If unable to find solution
+ ## pop it and try for other combination
+ arrStr.pop()
+
+ return
+
+## Function to check if additive sequence
+def IsAdditiveSeq(strInputFunc):
+ global bResult
+
+ ## In order to form additive sequence, the length of the string must be at least three
+ if (len(strInputFunc) <= 2):
+ return False
+
+ bResult = False
+ check_additive(strInputFunc, 0)
+ return bResult
+
+## Driver Code
+if __name__ == "__main__":
+
+ ## strInput = "112358" ## Example 1
+ ## strInput = "12345" ## Example 2
+ strInput = "199100199" ## Example 3
+
+ print (IsAdditiveSeq(strInput))
diff --git a/challenge-224/laurent-rosenfeld/blog.txt b/challenge-224/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..b9db0c08b6 --- /dev/null +++ b/challenge-224/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/07/perl-weekly-challenge-224-special-notes.html diff --git a/challenge-224/laurent-rosenfeld/perl/ch-1.pl b/challenge-224/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..f1c6b3cd20 --- /dev/null +++ b/challenge-224/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use feature 'say'; + +sub special_note { + my ($source, $target) = @_; + my %chars; + $chars{$_}++ for split //, $source; + for my $let (split //, $target) { + return "false" unless $chars{$let}; + $chars{$let}--; + } + return "true"; +} +for my $test ([ "abc", "xyz"], + ["scriptinglanguage", "perl"], ["aabbcc", "abc"] ) { + printf "%-20s - %-7s => ", "@$test[0]", "@$test[1]"; + say special_note @$test[0], @$test[1]; +} diff --git a/challenge-224/laurent-rosenfeld/raku/ch-1.raku b/challenge-224/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..327cee7a34 --- /dev/null +++ b/challenge-224/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,10 @@ +sub special-note ($source, $target) { + my $chars = $source.comb.Bag; + return $target.comb.Bag ⊆ $chars; +} + +for ("abc", "xyz"), ("scriptinglanguage", "perl"), + ("aabbcc", "abc") -> @test { + printf "%-20s - %-7s => ", "@test[0]", "@test[1]"; + say special-note @test[0], @test[1]; +} diff --git a/challenge-224/robert-dicicco/julia/ch-1.jl b/challenge-224/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..fcfb28d700 --- /dev/null +++ b/challenge-224/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,43 @@ +#!usr/bin/env julia +#= +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-07-04 +Challenge 224 Task 1 Special Notes ( Julia ) +---------------------------------------------- +=# +source = ARGS[1] +target = ARGS[2] + +flag = 0 +offset = 1 + +while offset < length(target) + global offset, flag + letter = target[offset] + if ! occursin(letter,source) + println("Output: false") + flag = -1 + break + end + offset += 1 +end + +if flag == 0 + println("Output: true") +end + +#= +---------------------------------------------- +julia .\SpecialNotes.jl scriptinglanguage perl +Output: true + +julia .\SpecialNotes.jl abc xyz +Output: false + +julia .\SpecialNotes.jl aabbcc abc +Output: true +---------------------------------------------- +=# + + diff --git a/challenge-224/robert-dicicco/perl/ch-1.pl b/challenge-224/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..5ba766869a --- /dev/null +++ b/challenge-224/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl +=begin comment +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-07-03 +Challenge 224 Task 1 Special Notes ( Perl ) +---------------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; + + +my $source = $ARGV[0]; +my $target = $ARGV[1]; +my $flag = 0; +my $offset = 0; + +while ($offset < length($target)) { + my $letter = substr($target, $offset, 1); + if ($source !~ /$letter/) { + say "Output: false"; + $flag = -1; + last; + } + $offset++; +} + +if ($flag == 0) { + say "Output: true"; +} + +=begin comment +---------------------------------------------- +perl .\SpecialNotes.pl abc xyz +Output: false + +perl .\SpecialNotes.pl scriptinglanguage perl +Output: true + +perl .\SpecialNotes.pl aabbcc abc +Output: true + +=cut + + diff --git a/challenge-224/robert-dicicco/python/ch-1.py b/challenge-224/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..71a0f11b20 --- /dev/null +++ b/challenge-224/robert-dicicco/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# ---------------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-07-04 +# Challenge 224 Task 1 Special Notes ( Python ) +# ---------------------------------------------- + +import sys + +source = sys.argv[1] +target = sys.argv[2] + +flag = 0 +offset = 0 + +while offset < len(target): + letter = target[offset] + index = source.find(letter) + if index == -1: + print("Output: false") + flag = -1 + break + offset += 1 + +if flag == 0 : + print("Output: true") + + +# ---------------------------------------------- +# python .\SpecialNotes.py scriptinglanguage perl +# Output: true + +# python .\SpecialNotes.py abc xyz +# Output: false + +# python .\SpecialNotes.py aabbcc abc +# Output: true +# ---------------------------------------------- + + diff --git a/challenge-224/robert-dicicco/raku/ch-1.raku b/challenge-224/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..0d097e3755 --- /dev/null +++ b/challenge-224/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku +=begin comment +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-07-03 +Challenge 224 Task 1 Special Notes ( Raku ) +---------------------------------------------- +=end comment +use v6; +my $flag = 0; +my $offset = 0; + +sub MAIN ($source, $target) { + while $offset < $target.chars { + my $letter = substr($target, $offset, 1); + unless $source.contains($letter) { + say "Output: false"; + $flag = -1; + last; + } + $offset++; + } + if ($flag == 0) { + say "Output: true"; + } +} + +=begin comment +---------------------------------------------- +raku .\SpecialNotes.rk abc xyz +Output: false + +raku .\SpecialNotes.rk scriptinglanguage perl +Output: true + +raku .\SpecialNotes.rk aabbcc abc +Output: true +---------------------------------------------- +=end comment + + diff --git a/challenge-224/robert-dicicco/ruby/ch-1.rb b/challenge-224/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..1a8f2e768b --- /dev/null +++ b/challenge-224/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby +=begin +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-07-03 +Challenge 224 Task 1 Special Notes ( Ruby ) +---------------------------------------------- +=end + +source = ARGV[0] +target = ARGV[1] + +flag = 0 +offset = 0 + +while offset < target.length() + letter = target[offset,1] + if not source.include? letter + puts "Output: false"; + flag = -1; + break + end + offset += 1 +end + +if flag == 0 + puts("Output: true") +end + +=begin +---------------------------------------------- +ruby .\SpecialNotes.rb abc xyz +Output: false + +ruby .\SpecialNotes.rb scriptinglanguage perl +Output: true + +ruby .\SpecialNotes.rb aabbcc abc +Output: true +---------------------------------------------- +=end + + diff --git a/challenge-224/steven-wilson/perl/ch-01.pl b/challenge-224/steven-wilson/perl/ch-1.pl index ac2e83a88b..ac2e83a88b 100644 --- a/challenge-224/steven-wilson/perl/ch-01.pl +++ b/challenge-224/steven-wilson/perl/ch-1.pl diff --git a/challenge-224/ulrich-rieke/cpp/ch-1.cpp b/challenge-224/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..5bbd419642 --- /dev/null +++ b/challenge-224/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <map> +#include <string> +#include <algorithm> + +int main( ) { + std::cout << "Please enter a source string!\n" ; + std::string source ; + std::cin >> source ; + std::cout << "Please enter a target string!\n" ; + std::string target ; + std::cin >> target ; + std::map<char , int> sourceMap ; + std::map<char, int> targetMap ; + for ( char c : source ) + sourceMap[c]++ ; + for ( char c : target ) + targetMap[c]++ ; + if ( std::all_of( target.begin( ) , target.end( ) , [sourceMap, targetMap] + ( char b ) { return sourceMap.find( b ) != sourceMap.end( ) + && sourceMap.find( b )->second >= + targetMap.find( b )->second ; } )) + std::cout << "true" ; + else + std::cout << "false" ; + std::cout << std::endl ; + return 0 ; +} diff --git a/challenge-224/ulrich-rieke/haskell/ch-1.hs b/challenge-224/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..d23970ca51 --- /dev/null +++ b/challenge-224/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,30 @@ +module Challenge224 + where +import qualified Data.Set as S +import Data.Maybe ( fromJust ) + +solution :: String -> String -> Bool +solution source target = S.isSubsetOf tset sset && ( all (\c -> +(fromJust $ lookup c occurrencesTarget) <= ( fromJust $ lookup +c occurrencesSource )) $ S.toList tset ) +where + tset :: S.Set Char + tset = S.fromList target + sset :: S.Set Char + sset = S.fromList source + count :: Char -> String -> Int + count c str = length $ filter ( == c ) str + occurrencesTarget :: [(Char, Int)] + occurrencesTarget = zip ( S.toList tset ) ( map (\b -> count b target ) + $ S.toList tset ) + occurrencesSource :: [(Char , Int)] + occurrencesSource = zip ( S.toList sset ) ( map (\b -> count b source ) + $ S.toList sset ) + +main :: IO ( ) +main = do + putStrLn "Enter a source string!" + source <- getLine + putStrLn "Enter a target string!" + target <- getLine + if solution source target then print "true" else print "false" diff --git a/challenge-224/ulrich-rieke/haskell/ch-2.hs b/challenge-224/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..dd794d740c --- /dev/null +++ b/challenge-224/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,47 @@ +module Challenge224_2 + where +import Data.Char ( isDigit , digitToInt ) +import Data.List ( nub , (!!) ) +import Data.List.Split ( splitPlaces ) +import qualified Data.Set as S + +partitions :: Int -> [[Int]] +partitions n = S.toList $ S.fromList (filter ( (== n) . sum ) $ concat $ map (\i -> +combinations i totallist ) [1..n]) + +where + totallist :: [Int] + totallist = concat $ map(\i -> replicate ( div n i ) i ) [1..n] + +combinations :: Int -> [a] -> [[a]] +combinations 0 _ = [[]] +combinations n xs = [ xs !! i : x | i <- [0..(length xs ) - 1 ] , +x <- combinations (n - 1 ) ( drop ( i + 1 ) xs ) ] + +extractArray :: String -> [Int] -> [Int] +extractArray numberstr intpart = map read $ splitPlaces intpart numberstr + +isAdditive :: [Int] -> Bool +isAdditive list = l > 2 && all (\i -> (list !! (i - 2 )) + +(list !! ( i - 1 )) == list !! i ) [2..l - 1] +where + l :: Int + l = length list + +solution :: String -> Bool +solution s = any isAdditive $ map (\sublist -> extractArray s sublist ) +$ partitions ( length s ) + +askForCorrectInput :: IO String +askForCorrectInput = do + putStrLn "Please enter a string consisting of digits only!" + numberstring <- getLine + if all isDigit numberstring + then return numberstring + else do + askForCorrectInput + +main :: IO ( ) +main = do + numberstring <- askForCorrectInput + if solution numberstring then print "true" else print "false" diff --git a/challenge-224/ulrich-rieke/perl/ch-1.pl b/challenge-224/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..660c6847bd --- /dev/null +++ b/challenge-224/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( all ) ; + +say "Enter a source string!" ; +my $source = <STDIN> ; +chomp $source ; +say "Enter a target string!" ; +my $target = <STDIN> ; +chomp $target ; +my %sourceHash ; +my %targetHash ; +map { $sourceHash{$_}++ } split ( // , $source ) ; +map { $targetHash{$_}++ } split ( // , $target ) ; +if ( all { (exists( $sourceHash{$_} )) && ( $sourceHash{$_} >= + $targetHash{$_} ) } keys %targetHash ) { + say "true" ; +} +else { + say "false" ; +} diff --git a/challenge-224/ulrich-rieke/perl/ch-2.pl b/challenge-224/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..dfce9139b0 --- /dev/null +++ b/challenge-224/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use Algorithm::Combinatorics qw ( partitions ) ; + +sub isAdditive { + my $array = shift ; + my $len = scalar( @$array ) ; + if ( $len <= 2 ) { + return 0 ; + } + else { + for my $i ( 2..$len - 1 ) { + if ( $array->[ $i - 2 ] + $array->[ $i - 1] != $array->[ $i ] ) { + return 0 ; + } + } + return 1 ; + } +} + +sub concatenate { + my $array = shift ; + return join( '' , @$array ) + 0 ; +} + +say "Enter a string consisting only of digits 0 to 9!" ; +my $line = <STDIN> ; +chomp $line ; +my @numbers = split( // , $line ) ; +if ( isAdditive( \@numbers )) { + say "true" ; +} +else { + my $iter = partitions( \@numbers ) ; + while ( my $c = $iter->next( )) { + my @currentNumbers ; + for my $n ( @$c ) { + push @currentNumbers , concatenate( $n ) ; + } + if ( isAdditive( \@currentNumbers )) { + say "true" ; + exit( 0 ) ; + } + } + say "false" ; +} diff --git a/challenge-224/ulrich-rieke/raku/ch-1.raku b/challenge-224/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..cbb28e18ad --- /dev/null +++ b/challenge-224/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,20 @@ +use v6 ; + +say "Enter a source string!" ; +my $source = $*IN.get ; +say "Enter a target string!" ; +my $target = $*IN.get ; +my %sourceHash ; +my %targetHash ; +my @targetletters = $target.comb ; +my @sourceletters = $source.comb ; +@targetletters.map( {%targetHash{$_}++} ) ; +@sourceletters.map( { %sourceHash{$_}++ } ) ; +my @selected = @targetletters.grep( {%sourceHash{$_}:exists && + %sourceHash{$_} >= %targetHash{$_} } ) ; +if ( @selected.elems == @targetletters.elems ) { + say "true" ; +} +else { + say "false" ; +} diff --git a/challenge-224/ulrich-rieke/raku/ch-2.raku b/challenge-224/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..9c95906d2f --- /dev/null +++ b/challenge-224/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,43 @@ +use v6 ; +use Math::Combinatorics <partitions> ; + +sub isAdditive( @array ) { + my $len = @array.elems ; + if ( $len <= 2 ) { + return False ; + } + else { + for (2..$len - 1 ) -> $i { + if ( @array[ $i - 2 ] + @array[ $i - 1 ] != @array[ $i ] ) { + return False ; + } + } + return True ; + } +} + +#the library function partitions n provides all possible combinations of +#numbers 1 to n that add up to n. We extract the corresponding substrings +#from the original string that was entered. +sub extractArray( $string , $seq ) { + my @array ; + my $pos = 0 ; + for (0..$seq.elems - 1) -> $i { + @array.push( $string.substr($pos , $seq[ $i ] ).Int ) ; + $pos += $seq[ $i ] ; + } + return @array ; +} + +say "Enter a string consisting of digits 0 to 9 only!" ; +my $line = $*IN.get ; +my $len = $line.chars ; +my @partitions = partitions( $len ) ; +for @partitions -> $part { + my @numArray = extractArray( $line , $part ) ; + if ( isAdditive( @numArray ) ) { + say "true" ; + exit( 0 ) ; + } +} +say "false" ; diff --git a/challenge-224/ulrich-rieke/rust/ch-1.rs b/challenge-224/ulrich-rieke/rust/ch-1.rs new file mode 100644 index 0000000000..65651708ab --- /dev/null +++ b/challenge-224/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,30 @@ +use std::io ; +use std::collections::HashMap ; + +fn main() { + println!("Please enter a source string!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let source : &str = &*inline ; + println!("Enter a target string!") ; + let mut secondline : String = String::new( ) ; + io::stdin( ).read_line( &mut secondline ).unwrap( ) ; + let target : &str = &*secondline ; + let mut target_map = HashMap::new( ) ; + let mut source_map = HashMap::new( ) ; + for c in source.chars( ) { + let value : &mut u32 = source_map.entry( c ).or_default( ) ; + *value += 1 ; + } + for c in target.chars( ) { + let value : &mut u32 = target_map.entry( c ).or_default( ) ; + *value += 1 ; + } + if target.chars( ).all( | c | source_map.get( & c ).is_some( ) && + source_map.get( &c ).unwrap( ) >= target_map.get( &c ).unwrap( ) ) { + println!("true") ; + } + else { + println!("false") ; + } +} diff --git a/challenge-224/ulrich-rieke/rust/ch-2.rs b/challenge-224/ulrich-rieke/rust/ch-2.rs new file mode 100644 index 0000000000..0d6ac5af5a --- /dev/null +++ b/challenge-224/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,81 @@ +use std::io ; +use itertools::Itertools ; +use std::collections::HashSet ; + +fn is_additive( numbers : &Vec<u32> ) -> bool { + let len : usize = numbers.len( ) ; + if len > 2 { + for pos in 2..len { + if numbers[pos - 2] + numbers[ pos - 1 ] != numbers[ pos ] { + return false ; + } + } + true + } + else { + false + } +} + +fn extract_from_string( numberstring : &String , places : &Vec<usize> ) + -> Vec<u32> { + let mut extracted : Vec<u32> = Vec::new( ) ; + let mut pos : usize = 0 ; + for i in places { + let substr = &numberstring[pos..pos + *i] ; + extracted.push( substr.parse::<u32>().unwrap( ) ) ; + pos += i ; + } |
