aboutsummaryrefslogtreecommitdiff
path: root/challenge-320
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-05-06 15:45:31 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-05-06 15:45:31 +0100
commit7d67833e2e7de682d2d418145812d575218e0fa9 (patch)
tree5941416b346b211dfe559083da3d92a708dd4a29 /challenge-320
parentfd1e9787a80a72ee53be0a313a27c56ecd9d62ee (diff)
downloadperlweeklychallenge-club-7d67833e2e7de682d2d418145812d575218e0fa9.tar.gz
perlweeklychallenge-club-7d67833e2e7de682d2d418145812d575218e0fa9.tar.bz2
perlweeklychallenge-club-7d67833e2e7de682d2d418145812d575218e0fa9.zip
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke. - Added solutions by David Ferrone. - Added solutions by E. Choroba. - Added solutions by Mark Anderson. - Added solutions by Jaldhar H. Vyas. - Added solutions by Ali Moradi. - Added solutions by Feng Chang. - Added solutions by Vinod Kumar K. - Added solutions by Peter Campbell Smith. - Added solutions by Ali Moradi. - Added solutions by Feng Chang. - Added solutions by Conor Hoekstra. - Added solutions by Bob Lied. - Added solutions by Athanasius. - Added solutions by W. Luis Mochan. - Added solutions by Andrew Shitov. - Added solutions by PokGoPun. - Added solutions by Roger Bell_West.
Diffstat (limited to 'challenge-320')
-rw-r--r--challenge-320/conor-hoekstra/bqn/ch-1.bqn (renamed from challenge-320/conor-hoekstra/ch-1.bqn)0
-rw-r--r--challenge-320/conor-hoekstra/bqn/ch-2.bqn (renamed from challenge-320/conor-hoekstra/ch-2.bqn)0
-rwxr-xr-xchallenge-320/eric-cheung/python/ch-1.py9
-rwxr-xr-xchallenge-320/eric-cheung/python/ch-2.py12
-rwxr-xr-xchallenge-320/ulrich-rieke/cpp/ch-1.cpp36
-rwxr-xr-xchallenge-320/ulrich-rieke/cpp/ch-2.cpp44
-rwxr-xr-xchallenge-320/ulrich-rieke/haskell/ch-1.hs16
-rwxr-xr-xchallenge-320/ulrich-rieke/haskell/ch-2.hs18
-rwxr-xr-xchallenge-320/ulrich-rieke/perl/ch-1.pl18
-rwxr-xr-xchallenge-320/ulrich-rieke/perl/ch-2.pl14
-rwxr-xr-xchallenge-320/ulrich-rieke/raku/ch-1.raku13
-rwxr-xr-xchallenge-320/ulrich-rieke/raku/ch-2.raku17
-rwxr-xr-xchallenge-320/ulrich-rieke/rust/ch-1.rs17
-rwxr-xr-xchallenge-320/ulrich-rieke/rust/ch-2.rs22
14 files changed, 236 insertions, 0 deletions
diff --git a/challenge-320/conor-hoekstra/ch-1.bqn b/challenge-320/conor-hoekstra/bqn/ch-1.bqn
index 332ee5a755..332ee5a755 100644
--- a/challenge-320/conor-hoekstra/ch-1.bqn
+++ b/challenge-320/conor-hoekstra/bqn/ch-1.bqn
diff --git a/challenge-320/conor-hoekstra/ch-2.bqn b/challenge-320/conor-hoekstra/bqn/ch-2.bqn
index 20d234a4ba..20d234a4ba 100644
--- a/challenge-320/conor-hoekstra/ch-2.bqn
+++ b/challenge-320/conor-hoekstra/bqn/ch-2.bqn
diff --git a/challenge-320/eric-cheung/python/ch-1.py b/challenge-320/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..57f640454f
--- /dev/null
+++ b/challenge-320/eric-cheung/python/ch-1.py
@@ -0,0 +1,9 @@
+
+## arrInts = [-3, -2, -1, 1, 2, 3] ## Example 1
+## arrInts = [-2, -1, 0, 0, 1] ## Example 2
+arrInts = [1, 2, 3, 4] ## Example 3
+
+nPosCount = len([nLoop for nLoop in arrInts if nLoop > 0])
+nNegCount = len([nLoop for nLoop in arrInts if nLoop < 0])
+
+print (max(nPosCount, nNegCount))
diff --git a/challenge-320/eric-cheung/python/ch-2.py b/challenge-320/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..5e2c3d812c
--- /dev/null
+++ b/challenge-320/eric-cheung/python/ch-2.py
@@ -0,0 +1,12 @@
+
+## arrInts = [1, 23, 4, 5] ## Example 1
+## arrInts = [1, 2, 3, 4, 5] ## Example 2
+arrInts = [1, 2, 34] ## Example 3
+
+nElemSum = sum(arrInts)
+nDigitSum = sum([sum([int(charLoop) for charLoop in list(str(nLoop))]) for nLoop in arrInts])
+
+## print (nElemSum)
+## print (nDigitSum)
+
+print (abs(nElemSum - nDigitSum))
diff --git a/challenge-320/ulrich-rieke/cpp/ch-1.cpp b/challenge-320/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..158bba2cb8
--- /dev/null
+++ b/challenge-320/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,36 @@
+#include <vector>
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <cstdlib>
+#include <algorithm>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> 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 blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ int positives = std::count_if( numbers.begin( ) , numbers.end( ) , [](int i) {
+ return i > 0 ; } ) ;
+ int negatives = std::count_if( numbers.begin( ) , numbers.end( ) , [](int i) {
+ return i < 0 ; } ) ;
+ if ( positives != 0 || negatives != 0 ) {
+ std::cout << std::max( positives , negatives ) << '\n' ;
+ }
+ else {
+ std::cout << 0 << '\n' ;
+ }
+ return 0 ;
+}
diff --git a/challenge-320/ulrich-rieke/cpp/ch-2.cpp b/challenge-320/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..9950e4e891
--- /dev/null
+++ b/challenge-320/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,44 @@
+#include <iostream>
+#include <sstream>
+#include <numeric>
+#include <string>
+#include <vector>
+#include <cstdlib>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::istringstream istr { text } ;
+ std::string word ;
+ while ( std::getline( istr , word , delimiter ) )
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int digitsum( int number ) {
+ int sum = 0 ;
+ while ( number != 0 ) {
+ sum += number % 10 ;
+ number /= 10 ;
+ }
+ return sum ;
+}
+
+int main( ) {
+ std::cout << "Enter some positive integers separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ int numbersum = std::accumulate( numbers.begin( ) , numbers.end( ) , 0 ) ;
+ std::vector<int> digitsums ;
+ for ( int n : numbers ) {
+ digitsums.push_back( digitsum( n ) ) ;
+ }
+ int all_digit_sum = std::accumulate( digitsums.begin( ) , digitsums.end( ) ,
+ 0 ) ;
+ std::cout << std::abs( all_digit_sum - numbersum ) << '\n' ;
+ return 0 ;
+}
+
diff --git a/challenge-320/ulrich-rieke/haskell/ch-1.hs b/challenge-320/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..eb3137b388
--- /dev/null
+++ b/challenge-320/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,16 @@
+module Challenge320
+ where
+
+solution :: [Int] -> Int
+solution list = if all (== 0) list then 0 else max positives negatives
+ where
+ positives :: Int
+ positives = length $ filter ( > 0 ) list
+ negatives :: Int
+ negatives = length $ filter ( < 0 ) list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-320/ulrich-rieke/haskell/ch-2.hs b/challenge-320/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..becfa51647
--- /dev/null
+++ b/challenge-320/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,18 @@
+module Challenge320_2
+ where
+import Data.Char ( digitToInt )
+
+solution :: [Int] -> Int
+solution list = abs ( numbersum - allDigitSum )
+ where
+ numbersum :: Int
+ numbersum = sum list
+ allDigitSum :: Int
+ allDigitSum = sum $ map ( sum . map digitToInt . show ) list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some positive integers separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
+
diff --git a/challenge-320/ulrich-rieke/perl/ch-1.pl b/challenge-320/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..00dd5c6869
--- /dev/null
+++ b/challenge-320/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( max ) ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $positives = scalar( grep { $_ > 0 } @numbers ) ;
+my $negatives = scalar( grep { $_ < 0 } @numbers ) ;
+if ( $positives != 0 || $negatives != 0 ) {
+ say max( $positives , $negatives ) ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-320/ulrich-rieke/perl/ch-2.pl b/challenge-320/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..1caa708a93
--- /dev/null
+++ b/challenge-320/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( sum ) ;
+
+say "Enter some positive integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $numbersum = sum( @numbers ) ;
+my @digits = map { [split(// , $_)] } @numbers ;
+my $all_digit_sum = sum( map { sum( @$_ ) } @digits ) ;
+say abs( $numbersum - $all_digit_sum ) ;
diff --git a/challenge-320/ulrich-rieke/raku/ch-1.raku b/challenge-320/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..b0eca84c46
--- /dev/null
+++ b/challenge-320/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 $positives = @numbers.grep( {$_ > 0} ).elems ;
+my $negatives = @numbers.grep( {$_ < 0} ).elems ;
+if ( $positives != 0 || $negatives != 0 ) {
+ say ($positives , $negatives).max ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-320/ulrich-rieke/raku/ch-2.raku b/challenge-320/ulrich-rieke/raku/ch-2.raku
new file mode 100755
index 0000000000..a55f945b74
--- /dev/null
+++ b/challenge-320/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,17 @@
+use v6 ;
+
+sub digitsum( $number is copy ) {
+ my $sum = 0 ;
+ while ( $number != 0 ) {
+ $sum += $number % 10 ;
+ $number div= 10 ;
+ }
+ return $sum ;
+}
+
+say "Enter some positive integers separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $numbersum = [+] @numbers ;
+my $all_digit_sum = [+] @numbers.map( {digitsum( $_ )} ) ;
+say ($numbersum - $all_digit_sum).abs ;
diff --git a/challenge-320/ulrich-rieke/rust/ch-1.rs b/challenge-320/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..ae42871e6b
--- /dev/null
+++ b/challenge-320/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,17 @@
+use std::{io, cmp} ;
+
+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<i32> = inline.trim( ).split_whitespace( ).map( |s|
+ s.parse::<i32>( ).unwrap( ) ).collect( ) ;
+ let positives : usize = numbers.iter( ).filter( |&d| *d > 0 ).count( ) ;
+ let negatives : usize = numbers.iter( ).filter( |&d| *d < 0 ).count( ) ;
+ if positives != 0 || negatives != 0 {
+ println!("{}" , cmp::max( positives , negatives) ) ;
+ }
+ else {
+ println!("0") ;
+ }
+}
diff --git a/challenge-320/ulrich-rieke/rust/ch-2.rs b/challenge-320/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..4593e933a6
--- /dev/null
+++ b/challenge-320/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,22 @@
+use std::io ;
+
+fn digit_sum( mut number : u32 ) -> u32 {
+ let mut sum : u32 = 0 ;
+ while number != 0 {
+ sum += number % 10 ;
+ number /= 10 ;
+ }
+ sum
+}
+
+fn main() {
+ println!("Enter some positive integers separated by blanks!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let mut numbers : Vec<u32> = inline.trim( ).split_whitespace( ).map( |s|
+ s.parse::<u32>( ).unwrap( ) ).collect( ) ;
+ let numbersum : u32 = numbers.iter( ).sum( ) ;
+ let all_digit_sum : u32 = numbers.iter_mut( ).map( |s| digit_sum( *s ) )
+ .sum( ) ;
+ println!("{}" , (numbersum as i32).abs_diff( all_digit_sum as i32 ) ) ;
+}