aboutsummaryrefslogtreecommitdiff
path: root/challenge-238
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-238')
-rwxr-xr-xchallenge-238/eric-cheung/python/ch-1.py12
-rwxr-xr-xchallenge-238/eric-cheung/python/ch-2.py23
-rw-r--r--challenge-238/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-238/laurent-rosenfeld/perl/ch-1.pl17
-rw-r--r--challenge-238/laurent-rosenfeld/raku/ch-1.raku9
-rwxr-xr-xchallenge-238/perlboy1967/perl/ch-1.pl (renamed from challenge-238/perlboy1967/perl/ch1.pl)0
-rwxr-xr-xchallenge-238/perlboy1967/perl/ch-2.pl (renamed from challenge-238/perlboy1967/perl/ch2.pl)0
-rw-r--r--challenge-238/robert-dicicco/perl/ch-1.pl37
-rw-r--r--challenge-238/robert-dicicco/raku/ch-1.raku40
-rwxr-xr-xchallenge-238/ulrich-rieke/cpp/ch-1.cpp38
-rwxr-xr-xchallenge-238/ulrich-rieke/cpp/ch-2.cpp73
-rwxr-xr-xchallenge-238/ulrich-rieke/haskell/ch-1.hs5
-rwxr-xr-xchallenge-238/ulrich-rieke/haskell/ch-2.hs33
-rwxr-xr-xchallenge-238/ulrich-rieke/perl/ch-1.pl17
-rwxr-xr-xchallenge-238/ulrich-rieke/perl/ch-2.pl42
-rwxr-xr-xchallenge-238/ulrich-rieke/raku/ch-1.raku13
-rwxr-xr-xchallenge-238/ulrich-rieke/raku/ch-2.raku55
-rwxr-xr-xchallenge-238/ulrich-rieke/rust/ch-1.rs18
-rwxr-xr-xchallenge-238/ulrich-rieke/rust/ch-2.rs52
19 files changed, 485 insertions, 0 deletions
diff --git a/challenge-238/eric-cheung/python/ch-1.py b/challenge-238/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..940af19099
--- /dev/null
+++ b/challenge-238/eric-cheung/python/ch-1.py
@@ -0,0 +1,12 @@
+
+## arrInput = [1, 2, 3, 4, 5] ## Example 1
+## arrInput = [1, 1, 1, 1, 1] ## Example 2
+arrInput = [0, -1, 1, 2] ## Example 3
+
+arrOutput = []
+for nLoop in arrInput:
+ ## print (nLoop)
+ arrOutput.append(nLoop if len(arrOutput) == 0 else nLoop + arrOutput[-1])
+ ## print (arrOutput)
+
+print (arrOutput)
diff --git a/challenge-238/eric-cheung/python/ch-2.py b/challenge-238/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..e215581ec3
--- /dev/null
+++ b/challenge-238/eric-cheung/python/ch-2.py
@@ -0,0 +1,23 @@
+
+from numpy import prod
+
+def GetStep (nStep, nInput):
+ if nInput < 10:
+ return nStep
+ return GetStep (nStep + 1, prod([int(charLoop) for charLoop in str(nInput)]))
+
+## print (GetStep(0, 1))
+
+## arrInput = [15, 99, 1, 34] ## Example 1
+arrInput = [50, 25, 33, 22] ## Example 2
+
+arrOutput = arrInput[:]
+
+for nRow in range(0, len(arrOutput) - 1):
+ for nCol in range(nRow + 1, len(arrOutput)):
+ if GetStep (0, arrOutput[nRow]) > GetStep (0, arrOutput[nCol]) or GetStep (0, arrOutput[nRow]) == GetStep (0, arrOutput[nCol]) and arrOutput[nRow] > arrOutput[nCol]:
+ vTemp = arrOutput[nRow]
+ arrOutput[nRow] = arrOutput[nCol]
+ arrOutput[nCol] = vTemp
+
+print (arrOutput)
diff --git a/challenge-238/laurent-rosenfeld/blog.txt b/challenge-238/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..8cfb474024
--- /dev/null
+++ b/challenge-238/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/laurent_r/2023/10/perl-weekly-challenge-238-running-sum.html
diff --git a/challenge-238/laurent-rosenfeld/perl/ch-1.pl b/challenge-238/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..6164c90358
--- /dev/null
+++ b/challenge-238/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub running_sum {
+ my @sum = shift;
+ for my $item (@_) {
+ push @sum, $item + $sum[-1];
+ }
+ return @sum;
+}
+
+my @tests = ([<1 2 3 4 5>], [<1 1 1 1 1>], [<0 -1 1 2>]);
+for my $test (@tests) {
+ printf "%-15s => ", "@$test";
+ say join ", ", running_sum @$test;
+}
diff --git a/challenge-238/laurent-rosenfeld/raku/ch-1.raku b/challenge-238/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..aa519c8e8f
--- /dev/null
+++ b/challenge-238/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,9 @@
+sub running-sum (@in) {
+ return [\+] @in;
+}
+
+my @tests = <1 2 3 4 5>, <1 1 1 1 1>, <0 -1 1 2>;
+for @tests -> @test {
+ printf "%-15s => ", "@test[]";
+ say join ", ", running-sum @test;
+}
diff --git a/challenge-238/perlboy1967/perl/ch1.pl b/challenge-238/perlboy1967/perl/ch-1.pl
index 159ba18e4b..159ba18e4b 100755
--- a/challenge-238/perlboy1967/perl/ch1.pl
+++ b/challenge-238/perlboy1967/perl/ch-1.pl
diff --git a/challenge-238/perlboy1967/perl/ch2.pl b/challenge-238/perlboy1967/perl/ch-2.pl
index c1e3b5be91..c1e3b5be91 100755
--- a/challenge-238/perlboy1967/perl/ch2.pl
+++ b/challenge-238/perlboy1967/perl/ch-2.pl
diff --git a/challenge-238/robert-dicicco/perl/ch-1.pl b/challenge-238/robert-dicicco/perl/ch-1.pl
new file mode 100644
index 0000000000..a5729284dc
--- /dev/null
+++ b/challenge-238/robert-dicicco/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+=begin comment
+AUTHOR: Robert DiCicco
+DATE : 2023-10-09
+Challenge 238 Task 01 Running Sum ( Perl )
+=cut
+
+use v5.38;
+
+my @myints = ([1, 2, 3, 4, 5],[1, 1, 1, 1, 1],[0, -1, 1, 2]);
+
+for my $mints (@myints) {
+ my $sum = 0;
+ my @out = ();
+ say "Input: \@int = [@$mints]";
+ for my $i (@$mints) {
+ push(@out, $i + $sum);
+ $sum += $i;
+ }
+ say "Output: [@out]\n";
+}
+
+=begin comment
+SAMPLE OUTPUT
+perl .\RunningSum.pl
+
+Input: @int = [1 2 3 4 5]
+Output: [1 3 6 10 15]
+
+Input: @int = [1 1 1 1 1]
+Output: [1 2 3 4 5]
+
+Input: @int = [0 -1 1 2]
+Output: [0 -1 0 2]
+=cut
+
+
diff --git a/challenge-238/robert-dicicco/raku/ch-1.raku b/challenge-238/robert-dicicco/raku/ch-1.raku
new file mode 100644
index 0000000000..4e9944e28d
--- /dev/null
+++ b/challenge-238/robert-dicicco/raku/ch-1.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env raku
+
+=begin comment
+---------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-10-09
+Challenge 238 Task 01 Running Sum ( Raku )
+---------------------------------
+=end comment
+
+my @myints = ([1, 2, 3, 4, 5],[1, 1, 1, 1, 1],[0, -1, 1, 2]);
+
+for (@myints) -> @mints {
+ my $sum = 0;
+ my @out = ();
+ say "Input: \@int = ", @mints;
+ for (@mints) -> $i {
+ @out.push($i + $sum);
+ $sum += $i;
+ }
+ say "Output: ",@out,"\n";
+}
+
+=begin comment
+---------------------------------
+SAMPLE OUTPUT
+raku .\RunningSum.rk
+
+Input: @int = [1 2 3 4 5]
+Output: [1 3 6 10 15]
+
+Input: @int = [1 1 1 1 1]
+Output: [1 2 3 4 5]
+
+Input: @int = [0 -1 1 2]
+Output: [0 -1 0 2]
+---------------------------------
+=end comment
+
+
diff --git a/challenge-238/ulrich-rieke/cpp/ch-1.cpp b/challenge-238/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..255523fb1f
--- /dev/null
+++ b/challenge-238/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,38 @@
+#include <vector>
+#include <string>
+#include <iostream>
+
+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 integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings( split( line , " " ) ) ;
+ std::vector<int> numbers ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::vector<int> result ;
+ int current_sum = 0 ;
+ int size = numbers.size( ) ;
+ for ( int i = 0 ; i < size ; i++ ) {
+ current_sum += numbers[ i ] ;
+ result.push_back( current_sum ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : result )
+ std::cout << i << " " ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-238/ulrich-rieke/cpp/ch-2.cpp b/challenge-238/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..695c38c7c1
--- /dev/null
+++ b/challenge-238/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,73 @@
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <iostream>
+#include <numeric>
+
+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 ;
+}
+
+std::vector<int> decompose( int number ) {
+ std::vector<int> digits ;
+ while ( number != 0 ) {
+ int remainder = number % 10 ;
+ digits.push_back( remainder ) ;
+ number /= 10 ;
+ }
+ return digits ;
+}
+
+int findSteps( int number ) {
+ if ( number < 10 )
+ return 0 ;
+ else {
+ int steps = 1 ;
+ std::vector<int> digits ( decompose( number ) ) ;
+ int product = std::accumulate( digits.begin( ) , digits.end( ) ,
+ 1 , std::multiplies<int>( ) ) ;
+ while ( product > 9 ) {
+ steps++ ;
+ digits = decompose( product ) ;
+ product = std::accumulate( digits.begin( ) , digits.end( ) ,
+ 1 , std::multiplies<int>( ) ) ;
+ }
+ return steps ;
+ }
+}
+
+bool mySorter( int a , int b ) {
+ int steps1 = findSteps( a ) ;
+ int steps2 = findSteps( b ) ;
+ if ( steps1 != steps2 )
+ return steps1 < steps2 ;
+ else
+ return a < b ;
+}
+
+int main( ) {
+ std::cout << "Enter some positive integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings( split( line , " " ) ) ;
+ std::vector<int> numbers ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::sort( numbers.begin( ) , numbers.end( ) , []( int a , int b ) {
+ return mySorter( a , b ) ; } ) ;
+ std::cout << "(" ;
+ for ( int i : numbers ) {
+ std::cout << i << " " ;
+ }
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-238/ulrich-rieke/haskell/ch-1.hs b/challenge-238/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..35d2766786
--- /dev/null
+++ b/challenge-238/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,5 @@
+module Challenge238
+ where
+
+solution :: [Int] -> [Int]
+solution list = map (\n -> sum $ take n list ) [1..length list]
diff --git a/challenge-238/ulrich-rieke/haskell/ch-2.hs b/challenge-238/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..7c9b38a720
--- /dev/null
+++ b/challenge-238/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,33 @@
+module Challenge238_2
+ where
+import Data.Char ( digitToInt )
+import Data.List ( sortBy )
+
+decompose :: Int -> [Int]
+decompose = map digitToInt . show
+
+findSteps :: Int -> Int
+findSteps n
+ |n < 10 = 0
+ |otherwise = fst $ until ( ( < 10 ) . snd ) action ( 0 , n )
+ where
+ action :: (Int , Int ) -> (Int , Int )
+ action ( count , d ) = ( count + 1 , product $ decompose d )
+
+mySorter :: Int -> Int -> Ordering
+mySorter d1 d2
+ |st1 /= st2 = compare st1 st2
+ |otherwise = compare d1 d2
+ where
+ st1 = findSteps d1
+ st2 = findSteps d2
+
+solution :: [Int] -> [Int]
+solution = sortBy mySorter
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some positive integers , separated by blanks!"
+ numberstrings <- getLine
+ let numbers = map read $ words numberstrings
+ print $ solution numbers
diff --git a/challenge-238/ulrich-rieke/perl/ch-1.pl b/challenge-238/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..e53dec077d
--- /dev/null
+++ b/challenge-238/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $len = scalar( @numbers ) ;
+my $current_sum = 0 ;
+my @result ;
+for my $i (0..$len - 1 ) {
+ $current_sum += $numbers[ $i ] ;
+ push @result , $current_sum ;
+}
+say "(" . join( ',' , @result ) . ")" ;
diff --git a/challenge-238/ulrich-rieke/perl/ch-2.pl b/challenge-238/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..2e11c42b1d
--- /dev/null
+++ b/challenge-238/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( product ) ;
+
+sub decompose {
+ my $number = shift ;
+ my @digits ;
+ while ( $number != 0 ) {
+ my $remainder = $number % 10 ;
+ push @digits , $remainder ;
+ $number = int( $number / 10 ) ;
+ }
+ return @digits ;
+}
+
+sub findSteps {
+ my $number = shift ;
+ if ( $number < 10 ) {
+ return 0 ;
+ }
+ else {
+ my @digits = decompose( $number ) ;
+ my $steps = 1 ;
+ my $prod = product( @digits ) ;
+ while ( $prod > 9 ) {
+ $steps++ ;
+ @digits = decompose( $prod ) ;
+ $prod = product( @digits ) ;
+ }
+ return $steps ;
+ }
+}
+
+say "Enter some positive integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @sorted = sort { findSteps( $a ) <=> findSteps( $b ) || $a <=> $b }
+ @numbers ;
+say "(" . join( ',' , @sorted ) . ")" ;
diff --git a/challenge-238/ulrich-rieke/raku/ch-1.raku b/challenge-238/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..f120033055
--- /dev/null
+++ b/challenge-238/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,13 @@
+use v6 ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $len = @numbers.elems ;
+my @result ;
+my $current_sum = 0 ;
+for (0..$len - 1 ) -> $pos {
+ $current_sum += @numbers[ $pos ] ;
+ @result.push( $current_sum ) ;
+}
+say "(" ~ @result.join( ',' ) ~ ")" ;
diff --git a/challenge-238/ulrich-rieke/raku/ch-2.raku b/challenge-238/ulrich-rieke/raku/ch-2.raku
new file mode 100755
index 0000000000..b033d029d0
--- /dev/null
+++ b/challenge-238/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,55 @@
+use v6 ;
+
+sub decompose( $number is copy ) {
+ my @digits ;
+ while ( $number != 0 ) {
+ my $remainder = $number % 10 ;
+ @digits.push( $remainder ) ;
+ $number div= 10 ;
+ }
+ return @digits ;
+}
+
+sub find_steps( $number is copy ) {
+ if ( $number < 10 ) {
+ return 0 ;
+ }
+ else {
+ my $steps = 1 ;
+ my @digits = decompose( $number ) ;
+ my $product = [*] @digits ;
+ while ( $product > 9 ) {
+ $steps++ ;
+ @digits = decompose( $product ) ;
+ $product = [*] @digits ;
+ }
+ return $steps ;
+ }
+}
+
+sub my_sorter( $firstNum , $secondNum ) {
+ my $steps1 = find_steps( $firstNum ) ;
+ my $steps2 = find_steps( $secondNum ) ;
+ if ( $steps1 != $steps2 ) {
+ if ( $steps1 < $steps2 ) {
+ return Order::Less ;
+ }
+ else {
+ return Order::More ;
+ }
+ }
+ else {
+ if ( $firstNum < $secondNum ) {
+ return Order::Less ;
+ }
+ else {
+ return Order::More ;
+ }
+ }
+}
+
+say "Enter some positive integers, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $sorted = @numbers.sort: &my_sorter ;
+say "(" ~ $sorted.join( ',' ) ~ ")" ;
diff --git a/challenge-238/ulrich-rieke/rust/ch-1.rs b/challenge-238/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..17daff00de
--- /dev/null
+++ b/challenge-238/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,18 @@
+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<i32> = Vec::new( ) ;
+ let mut current_sum : i32 = 0 ;
+ let len = numbers.len( ) ;
+ for i in 0..len {
+ current_sum += numbers[ i ] ;
+ result.push( current_sum ) ;
+ }
+ println!("{:?}" , result ) ;
+}
diff --git a/challenge-238/ulrich-rieke/rust/ch-2.rs b/challenge-238/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..491503d5fc
--- /dev/null
+++ b/challenge-238/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,52 @@
+use std::io ;
+use std::cmp::Ordering ;
+
+fn decompose( number : u32 ) -> Vec<u32> {
+ let mut digits : Vec<u32> = Vec::new( ) ;
+ let mut num : u32 = number ;
+ while num != 0 {
+ let remainder = num % 10 ;
+ digits.push( remainder ) ;
+ num /= 10 ;
+ }
+ digits
+}
+
+fn find_steps( number : u32 ) -> usize {
+ if number < 10 {
+ 0
+ }
+ else {
+ let mut digits : Vec<u32> = decompose( number ) ;
+ let mut steps : usize = 1 ;
+ let mut prod : u32 = digits.iter( ).product::<u32>( ) ;
+ while prod > 9 {
+ steps += 1 ;
+ digits = decompose( prod ) ;
+ prod = digits.iter( ).product::<u32>() ;
+ }
+ steps
+ }
+}
+
+fn main() {
+ println!("Enter some positive integers , separated by blanks!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = &*inline ;
+ let mut numbers : Vec<u32> = entered_line.split_whitespace( ).map( | s |
+ s.trim( ).parse::<u32>().unwrap( )).collect( ) ;
+ let nums = numbers.as_mut_slice( ) ;
+ nums.sort_by( |a , b| {
+ let s1 : usize = find_steps( *a ) ;
+ let s2 : usize = find_steps( *b ) ;
+ let my_ord : Ordering = s1.cmp(&s2) ;
+ if my_ord != Ordering::Equal {
+ my_ord
+ }
+ else {
+ a.cmp( b )
+ }
+ }) ;
+ println!("{:?}" , nums ) ;
+}