diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-01-16 20:21:31 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-01-16 20:21:31 +0000 |
| commit | 857d573334deaa24866cb695f1e3758a85c0b9d9 (patch) | |
| tree | 357858672feabff853babfbb96035d61b4b688e0 | |
| parent | 47becd4d92af268e02cd61bb2b77816a4c8ed06b (diff) | |
| download | perlweeklychallenge-club-857d573334deaa24866cb695f1e3758a85c0b9d9.tar.gz perlweeklychallenge-club-857d573334deaa24866cb695f1e3758a85c0b9d9.tar.bz2 perlweeklychallenge-club-857d573334deaa24866cb695f1e3758a85c0b9d9.zip | |
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by Laurent Rosenfeld.
- Added solutions by PokGoPun.
- Added solutions by Simon Proctor.
- Added solutions by Niels van Dijke.
- Added solutions by Mark Anderson.
- Added solutions by E. Choroba.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Peter Campbell Smith.
- Added solutions by W. Luis Mochan.
- Added solutions by Peter Meszaros.
- Added solutions by Thomas Kohler.
- Added solutions by David Ferrone.
- Added solutions by Luca Ferrari.
- Added solutions by Roger Bell_West.
- Added solutions by Steven Wilson.
- Added solutions by Matthew Neleigh.
- Added solutions by Paulo Custodio.
40 files changed, 4357 insertions, 3694 deletions
diff --git a/challenge-252/eric-cheung/python/ch-1.py b/challenge-252/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..73003ac315 --- /dev/null +++ b/challenge-252/eric-cheung/python/ch-1.py @@ -0,0 +1,6 @@ +
+## arrInt = [1, 2, 3, 4] ## Example 1
+arrInt = [2, 7, 1, 19, 18, 3] ## Example 2
+
+arrOutput = [arrInt[nIndx] * arrInt[nIndx] for nIndx in range(len(arrInt)) if len(arrInt) % (nIndx + 1) == 0]
+print (sum(arrOutput))
diff --git a/challenge-252/eric-cheung/python/ch-2.py b/challenge-252/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..e943434336 --- /dev/null +++ b/challenge-252/eric-cheung/python/ch-2.py @@ -0,0 +1,10 @@ +
+## nInput = 5 ## Example 1
+## nInput = 3 ## Example 2
+## nInput = 1 ## Example 3
+nInput = 4 ## Example 4
+
+nMax = int(nInput / 2 if nInput % 2 == 0 else (nInput - 1) / 2)
+arrOutput = [nLoop for nLoop in range(-nMax, nMax + 1) if nLoop != 0 or nInput % 2 == 1]
+
+print (arrOutput)
diff --git a/challenge-252/laurent-rosenfeld/blog.txt b/challenge-252/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..30145fad3c --- /dev/null +++ b/challenge-252/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/01/perl-weekly-challenge-252-special-numbers.html diff --git a/challenge-252/laurent-rosenfeld/blog1.txt b/challenge-252/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..f123a9e23d --- /dev/null +++ b/challenge-252/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/01/perl-weekly-challenge-252-unique-sum-zero.html diff --git a/challenge-252/laurent-rosenfeld/perl/ch-1.pl b/challenge-252/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..1de3842ba8 --- /dev/null +++ b/challenge-252/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'say'; + +sub special_numbers { + my @in = @_; + my $n = scalar @in; + my $sq_sum = 0; + for my $i (0..$#in) { + $sq_sum += $in[$i] ** 2 unless $n % ($i+1); + } + return $sq_sum; +} + +for my $test ([<1 2 3 4>], [<2 7 1 19 18 3>]) { + printf "%-15s => ", "@$test"; + say special_numbers @$test; +} diff --git a/challenge-252/laurent-rosenfeld/perl/ch-2.pl b/challenge-252/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..fb888e79d1 --- /dev/null +++ b/challenge-252/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,16 @@ +use strict; +use warnings; +use feature 'say'; + +sub zero_sum { + my $n = shift; + my @result; + for my $i (1.. int $n/2) { + push @result, ($i, -$i); + } + push @result, 0 if $n % 2; + return @result; +} +for my $test (3, 4, 5, 1) { + say "$test => ", join " ", zero_sum $test; +} diff --git a/challenge-252/laurent-rosenfeld/raku/ch-1.raku b/challenge-252/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..c44a909359 --- /dev/null +++ b/challenge-252/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub special-numbers (@in) { + my $n = @in.elems; + my $sq-sum = 0; + for 0..@in.end -> $i { + $sq-sum += @in[$i]² if $n %% ($i+1); + } + return $sq-sum; +} + +for <1 2 3 4>, <2 7 1 19 18 3> -> @test { + printf "%-15s => ", "@test[]"; + say special-numbers @test; +} diff --git a/challenge-252/laurent-rosenfeld/raku/ch-2.raku b/challenge-252/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..42d40de711 --- /dev/null +++ b/challenge-252/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,11 @@ +sub zero-sum ($n) { + my @result; + for (1..$n*2).pick(($n/2).Int) -> $i { + append @result, ($i, -$i); + } + append @result, 0 unless $n %% 2; + return @result; +} +for 3, 4, 5, 1 -> $test { + say "$test => ", zero-sum $test; +} diff --git a/challenge-252/perlboy1967/perl/ch1.pl b/challenge-252/perlboy1967/perl/ch-1.pl index e8ae528469..e8ae528469 100755 --- a/challenge-252/perlboy1967/perl/ch1.pl +++ b/challenge-252/perlboy1967/perl/ch-1.pl diff --git a/challenge-252/perlboy1967/perl/ch2.pl b/challenge-252/perlboy1967/perl/ch-2.pl index 225f32ac2a..225f32ac2a 100755 --- a/challenge-252/perlboy1967/perl/ch2.pl +++ b/challenge-252/perlboy1967/perl/ch-2.pl diff --git a/challenge-252/steven-wilson/python/ch-01.py b/challenge-252/steven-wilson/python/ch-1.py index 1beb425462..1beb425462 100644 --- a/challenge-252/steven-wilson/python/ch-01.py +++ b/challenge-252/steven-wilson/python/ch-1.py diff --git a/challenge-252/ulrich-rieke/cpp/ch-1.cpp b/challenge-252/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..a99e8d5227 --- /dev/null +++ b/challenge-252/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,18 @@ +#include <vector>
+#include <iostream>
+#include <algorithm>
+#include <iterator>
+
+int main( ) {
+ std::cout << "Enter some integers , separated by blanks, end to end!\n" ;
+ std::vector<int> numbers { std::istream_iterator<int>{ std::cin } ,
+ std::istream_iterator<int>{} } ;
+ int sum = 0 ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( len % ( i + 1 ) == 0 )
+ sum += numbers[ i ] * numbers[ i ] ;
+ }
+ std::cout << sum << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-252/ulrich-rieke/cpp/ch-2.cpp b/challenge-252/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..5e88c46688 --- /dev/null +++ b/challenge-252/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,33 @@ +#include <vector>
+#include <iostream>
+#include <algorithm>
+#include <iterator>
+#include <numeric>
+
+int main( ) {
+ std::cout << "Enter a positive integer!\n" ;
+ int number ;
+ std::cin >> number ;
+ int limit = number / 2 ;
+ int neg_limit = -limit ;
+ std::vector<int> solution ;
+ if ( number % 2 == 1 ) {
+ solution.reserve( number ) ;
+ std::iota( solution.begin( ) , solution.end( ) , neg_limit ) ;
+ }
+ else {
+ int current = neg_limit ;
+ for ( int i = 0 ; i < number - 1 ; i++ ) {
+ solution.push_back( current ) ;
+ current++ ;
+ }
+ int sum = std::accumulate( solution.begin( ) , solution.end( ) ,
+ 0 ) ;
+ solution.push_back( 0 - sum ) ;
+ }
+ std::cout << '(' ;
+ std::copy( solution.begin( ) , solution.end( ) ,
+ std::ostream_iterator<int>( std::cout , " " )) ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-252/ulrich-rieke/haskell/ch-1.hs b/challenge-252/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..b5b2edb293 --- /dev/null +++ b/challenge-252/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,9 @@ +module Challenge252
+ where
+
+solution :: [Int] -> Int
+solution list = sum $ map (\p -> snd p ^ 2 ) $ filter (\p -> mod l
+ ( fst p + 1 ) == 0 ) $ zip [0 , 1..] list
+ where
+ l = length list
+
diff --git a/challenge-252/ulrich-rieke/haskell/ch-2.hs b/challenge-252/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..ac0ab60890 --- /dev/null +++ b/challenge-252/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,10 @@ +module Challenge252_2
+ where
+
+solution :: Int -> [Int]
+solution n
+ |odd n = [-limit..limit]
+ |even n = first_part ++ [0 - sum first_part]
+ where
+ limit = div n 2
+ first_part = take ( n - 1 ) $ iterate ( + 1 ) (limit * ( -1 ) )
diff --git a/challenge-252/ulrich-rieke/perl/ch-1.pl b/challenge-252/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..6f2b246c6b --- /dev/null +++ b/challenge-252/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 $sum = 0 ;
+for my $i (0..$len - 1 ) {
+ if ( $len % ( $i + 1 ) == 0 ) {
+ $sum += $numbers[ $i ] ** 2 ;
+ }
+}
+say $sum ;
diff --git a/challenge-252/ulrich-rieke/perl/ch-2.pl b/challenge-252/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..f6e669fb85 --- /dev/null +++ b/challenge-252/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( sum ) ;
+
+say "Enter a positive integer!" ;
+my $number = <STDIN> ;
+chomp $number ;
+my @solution ;
+my $limit = int( $number / 2 ) ;
+my $neg_limit = -$limit ;
+if ( $number % 2 == 1 ) {
+ for my $num ( $neg_limit..$limit ) {
+ push @solution, $num ;
+ }
+}
+else {
+ my $current = $neg_limit ;
+ for (1..$number - 1 ) {
+ push @solution, $current ;
+ $current++ ;
+ }
+ my $sum = sum( @solution ) ;
+ push @solution, 0 - $sum ;
+}
+say "(" . join( ',' , @solution ) . ")" ;
+
+
diff --git a/challenge-252/ulrich-rieke/raku/ch-1.raku b/challenge-252/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..899b12d639 --- /dev/null +++ b/challenge-252/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 $sum = 0 ;
+my $len = @numbers.elems ;
+for ( 0..$len - 1 ) -> $i {
+ if ( $len %% ( $i + 1 )) {
+ $sum += @numbers[$i]** 2 ;
+ }
+}
+say $sum ;
diff --git a/challenge-252/ulrich-rieke/raku/ch-2.raku b/challenge-252/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..25cff2e1e5 --- /dev/null +++ b/challenge-252/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,24 @@ +use v6 ;
+
+say "Enter a positive integer!" ;
+my $line = $*IN.get ;
+my $number = $line.Int ;
+my @solution ;
+my $limit = $number div 2 ;
+my $neg_limit = $limit * (-1) ;
+if ( not $number %% 2 ) {
+ for ( $neg_limit..$limit ) -> $num {
+ @solution.push( $num ) ;
+ }
+}
+else {
+ my $current = $neg_limit ;
+ for (1..$number - 1 ) {
+ @solution.push( $current ) ;
+ $current++ ;
+ }
+ my $sum = [+] @solution ;
+ @solution.push( 0 - $sum ) ;
+}
+say "(" ~ @solution.join( ',' ) ~ ")" ;
+
diff --git a/challenge-252/ulrich-rieke/rust/ch-1.rs b/challenge-252/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..95f3f39b32 --- /dev/null +++ b/challenge-252/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 sum : i32 = 0 ; + let len = numbers.len( ) ; + for i in 0..len { + if len % ( i + 1 ) == 0 { + sum += numbers[i].pow( 2 ) ; + } + } + println!("{}" , sum ) ; +} diff --git a/challenge-252/ulrich-rieke/rust/ch-2.rs b/challenge-252/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..449799be7e --- /dev/null +++ b/challenge-252/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,28 @@ +use std::io ; + +fn main() { + println!("Enter a positive integer!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let num : u32 = entered_line.trim( ).parse::<u32>( ).unwrap( ) ; + let mut solution : Vec<i32> = Vec::new( ) ; + let limit : u32 = num.div_euclid( 2 ) ; + let neg_limit : i32 = (limit as i32) * ( -1 ) ; + if num % 2 == 1 { + let pos_limit : i32 = limit as i32 ; + for i in neg_limit..=pos_limit { + solution.push( i ) ; + } + } + else { + let mut current : i32 = neg_limit ; + for _ in 0..num - 1 { + solution.push( current ) ; + current += 1 ; + } + let sum : i32 = solution.iter( ).sum::<i32>( ) ; + solution.push( 0 - sum ) ; + } + println!("{:?}" , solution ) ; +} diff --git a/stats/pwc-challenge-246.json b/stats/pwc-challenge-246.json index 83d8ebd515..51b7b4e097 100644 --- a/stats/pwc-challenge-246.json +++ b/stats/pwc-challenge-246.json @@ -1,35 +1,209 @@ { + "title" : { + "text" : "The Weekly Challenge - 246" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, "chart" : { "type" : "column" }, + "series" : [ + { + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 246", + "data" : [ + { + "drilldown" : "Adam Russell", + "name" : "Adam Russell", + "y" : 1 + }, + { + "drilldown" : "Arne Sommer", + "name" : "Arne Sommer", + "y" : 4 + }, + { + "y" : 4, + "name" : "Athanasius", + "drilldown" : "Athanasius" + }, + { + "drilldown" : "BarrOff", + "y" : 2, + "name" : "BarrOff" + }, + { + "name" : "Bob Lied", + "y" : 3, + "drilldown" : "Bob Lied" + }, + { + "name" : "Bruce Gray", + "y" : 2, + "drilldown" : "Bruce Gray" + }, + { + "drilldown" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung", + "y" : 3 + }, + { + "drilldown" : "Dave Jacoby", + "y" : 3, + "name" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "name" : "David Ferrone", + "y" : 2 + }, + { + "y" : 2, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { + "y" : 2, + "name" : "Humberto Massa", + "drilldown" : "Humberto Massa" + }, + { + "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek", + "y" : 2 + }, + { + "y" : 3, + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey" + }, + { + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "y" : 6 + }, + { + "name" : "Lubos Kolouch", + "y" : 5, + "drilldown" : "Lubos Kolouch" + }, + { + "y" : 10, + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari" + }, + { + "name" : "Mark Anderson", + "y" : 2, + "drilldown" : "Mark Anderson" + }, + { + "y" : 1, + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh" + }, + { + "name" : "Nelo Tovar", + "y" : 2, + "drilldown" : "Nelo Tovar" + }, + { + "y" : 2, + "name" : "Niels van Dijke", + "drilldown" : "Niels van Dijke" + }, + { + "drilldown" : "Oliver Oviedo", + "name" : "Oliver Oviedo", + "y" : 1 + }, + { + "name" : "Packy Anderson", + "y" : 5, + "drilldown" : "Packy Anderson" + }, + { + "name" : "Paulo Custodio", + "y" : 1, + "drilldown" : "Paulo Custodio" + }, + { + "y" : 3, + "name" : "Peter Campbell Smith", + "drilldown" : "Peter Campbell Smith" + }, + { + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros", + "y" : 2 + }, + { + "name" : "Robert DiCicco", + "y" : 2, + "drilldown" : "Robert DiCicco" + }, + { + "name" : "Robert Ransbottom", + "y" : 2, + "drilldown" : "Robert Ransbottom" + }, + { + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West", + "y" : 5 + }, + { + "drilldown" : "Stephen G. Lynn", + "name" : "Stephen G. Lynn", + "y" : 3 + }, + { + "name" : "Thomas Kohler", + "y" : 4, + "drilldown" : "Thomas Kohler" + }, + { + "y" : 3, + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" + } + ] + } + ], + "subtitle" : { + "text" : "[Champions: 31] Last updated at 2024-01-16 20:14:14 GMT" + }, "xAxis" : { "type" : "category" }, - "tooltip" : { - "followPointer" : 1, - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" - }, - "subtitle" : { - "text" : "[Champions: 30] Last updated at 2023-12-13 08:26:17 GMT" + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } }, - "title" : { - "text" : "The Weekly Challenge - 246" + "legend" : { + "enabled" : 0 }, "drilldown" : { "series" : [ { + "id" : "Adam Russell", "data" : [ [ "Perl", 1 ] ], - "name" : "Adam Russell", - "id" : "Adam Russell" + "name" : "Adam Russell" }, { - "name" : "Arne Sommer", "data" : [ [ "Perl", @@ -44,11 +218,10 @@ 1 ] ], + "name" : "Arne Sommer", "id" : "Arne Sommer" }, { - "id" : "Athanasius", - "name" : "Athanasius", "data" : [ [ "Perl", @@ -58,10 +231,11 @@ "Raku", 2 ] - ] + ], + "name" : "Athanasius", + "id" : "Athanasius" }, { - "id" : "BarrOff", "name" : "BarrOff", "data" : [ [ @@ -72,7 +246,8 @@ "Raku", 1 ] - ] + ], + "id" : "BarrOff" }, { "id" : "Bob Lied", @@ -89,16 +264,17 @@ ] }, { - "name" : "Bruce Gray", + "id" : "Bruce Gray", "data" : [ [ "Raku", 2 ] ], - "id" : "Bruce Gray" + "name" : "Bruce Gray" }, { + "id" : "Cheok-Yin Fung", "name" : "Cheok-Yin Fung", "data" : [ [ @@ -109,12 +285,10 @@ "Blog", 1 ] - ], - "id" : "Cheok-Yin Fung" + ] }, { "id" : "Dave Jacoby", - "name" : "Dave Jacoby", "data" : [ [ "Perl", @@ -124,17 +298,18 @@ "Blog", 1 ] - ] + ], + "name" : "Dave Jacoby" }, { - "id" : "David Ferrone", "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "David Ferrone" }, { "id" : "E. Choroba", @@ -147,14 +322,14 @@ ] }, { - "id" : "Humberto Massa", "name" : "Humberto Massa", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Humberto Massa" }, { "id" : "Jan Krnavek", @@ -167,6 +342,7 @@ ] }, { + "id" : "Jorg Sommrey", "name" : "Jorg Sommrey", "data" : [ [ @@ -177,11 +353,9 @@ "Blog", 1 ] - ], - "id" : "Jorg Sommrey" + ] }, {< |
