diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-04-03 02:26:32 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-04-03 02:26:32 +0100 |
| commit | 2ab30f5a76565588dfe64ff9efc2abeafed0e813 (patch) | |
| tree | bc0fe454df07f039cece23dc0286de3c0ecfb7b1 | |
| parent | 2ee6aa405be90f379f95597ad94e97d1a7985f1d (diff) | |
| download | perlweeklychallenge-club-2ab30f5a76565588dfe64ff9efc2abeafed0e813.tar.gz perlweeklychallenge-club-2ab30f5a76565588dfe64ff9efc2abeafed0e813.tar.bz2 perlweeklychallenge-club-2ab30f5a76565588dfe64ff9efc2abeafed0e813.zip | |
- Added solutions by Simon Green.
- Added solutions by W. Luis Mochan.
- Added solutions by E. Choroba.
- Added solutions by David Ferrone.
- Added solutions by Mark Anderson.
- Added solutions by Lubos Kolouch.
- Added solutions by Paulo Custodio.
- Added solutions by Roger Bell_West.
- Added solutions by Thomas Kohler.
- Added solutions by Peter Campbell Smith.
- Added solutions by Athanasius.
- Added solutions by Robbie Hatley.
- Added solutions by Luca Ferrari.
- Added solutions by Avery Adams.
- Added solutions by Bob Lied.
- Added solutions by Mariano Spadaccini.
- Added solutions by Jorg Sommrey.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Flavio Poletti.
- Added solutions by Solathian.
- Added solutions by Matthias Muth.
- Added solutions by Carlos Oliveira.
- Added solutions by Robert Ransbottom.
- Added solutions by Matthew Neleigh.
- Added solutions by Arne Sommer.
- Added solutions by Jan Krnavek.
- Added solutions by Pip Stuart.
- Added solutions by James Smith.
- Added solutions by Shimon Bollinger.
- Added solutions by Peter Meszaros.
- Added solutions by Ulrich Rieke.
- Added solutions by Robert DiCicco.
67 files changed, 11672 insertions, 9794 deletions
diff --git a/challenge-210/eric-cheung/python/ch-1.py b/challenge-210/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..53345fd3d4 --- /dev/null +++ b/challenge-210/eric-cheung/python/ch-1.py @@ -0,0 +1,7 @@ +
+## arrList = [2, 3, 1] ## Example 1
+arrList = [1, 1, 2, 2, 2, 3] ## Example 2
+
+nTotalPoints = sum(arrList)
+
+print (nTotalPoints)
diff --git a/challenge-210/eric-cheung/python/ch-2.py b/challenge-210/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..864545c9e8 --- /dev/null +++ b/challenge-210/eric-cheung/python/ch-2.py @@ -0,0 +1,28 @@ +
+## arrInputList = [2, 3, -1] ## Example 1:
+## arrInputList = [3, 2, -4] ## Example 2:
+arrInputList = [1, -1] ## Example 3:
+
+arrTempList = arrInputList[:]
+
+while len(arrTempList) > 0:
+
+ arrPosIndx = [nIndx for nIndx, nElemLoop in enumerate(arrTempList) if nElemLoop >= 0]
+ if len(arrPosIndx) == 0 or len(arrPosIndx) == len(arrTempList):
+ break
+
+ arrNegIndx = [nIndx for nIndx, nElemLoop in enumerate(arrTempList) if nElemLoop < 0]
+ nFirstNegIndx = arrNegIndx[0]
+
+ if nFirstNegIndx == 0:
+ break
+
+ if arrTempList[nFirstNegIndx - 1] == abs(arrTempList[nFirstNegIndx]):
+ del arrTempList[nFirstNegIndx]
+ del arrTempList[nFirstNegIndx - 1]
+ elif arrTempList[nFirstNegIndx - 1] < abs(arrTempList[nFirstNegIndx]):
+ del arrTempList[nFirstNegIndx - 1]
+ else:
+ del arrTempList[nFirstNegIndx]
+
+print (arrTempList)
diff --git a/challenge-210/robert-dicicco/julia/ch-1.jl b/challenge-210/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..a7e7a8d3f9 --- /dev/null +++ b/challenge-210/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,64 @@ +#!/usr/bin/env julia +#= +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-28 +Challenge 210 Kill&Win ( Julia ) +-------------------------------------- +=# +using Printf + +myint =[[2,3,1],[1,1,2,2,2,3]] +total_score = 0; +max_score = 0; +max_possible = 0; + +function Score(arr) + global total_score,max_possible,max_score + for testx in arr + println("trying ",testx) + for val in arr + if ((val == testx) || (val == testx-1) || (val == testx+1)) + total_score += val + end + end + if total_score == max_possible + println("Output: ",total_score) + println("") + return + end + if total_score > max_score + max_score = total_score + end + total_score = 0 + end + println("Output: ",max_score) +end + +for a in myint + global max_possible + println("Input: @int = ",a) + max_possible = sum(a) + println("max possible = ",max_possible) + Score(a) +end + +#= +-------------------------------------- +SAMPLE OUTPUT +julia .\KillWin.jl +Input: @int = [2, 3, 1] +max possible = 6 +trying 2 +Output: 6 + +Input: @int = [1, 1, 2, 2, 2, 3] +max possible = 11 +trying 1 +trying 1 +trying 2 +Output: 11 +-------------------------------------- +=# + + diff --git a/challenge-210/robert-dicicco/perl/ch-1.pl b/challenge-210/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..a52d55be49 --- /dev/null +++ b/challenge-210/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +=begin comment +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-28 +Challenge 210 Kill&Win ( Perl ) +-------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; +use List::Util 'sum'; + +my @int = ([2,3,1],[1,1,2,2,2,3]); +my $total_score = 0; +my $max_score = 0; +my $max_possible = 0; + +sub Score { + my $a = shift; + + for my $testx (@$a) { + say "trying $testx..."; + + for my $val (@$a) { + $total_score += $val if (($val == $testx) || ($val == $testx-1) || ($val == $testx+1)); + } + + if ($total_score == $max_possible) { + say "Output: $total_score\n"; + return; + } + if ($total_score > $max_score) { + $max_score = $total_score; + } + + $total_score = 0; + } + say "Output: $max_score\n"; +} + +for my $i (@int) { + my @arr = @$i; + say "Input: \@int = (@arr)"; + $max_possible = sum(@arr[0..$#arr]); + say "max_possible = $max_possible"; + Score(\@arr); +} +=begin comment +-------------------------------------- +SAMPLE OUTPUT +perl .\KillWin.pl +Input: @int = (2 3 1) +max_possible = 6 +trying 2... +Output: 6 + +Input: @int = (1 1 2 2 2 3) +max_possible = 11 +trying 1... +trying 1... +trying 2... +Output: 11 +-------------------------------------- +=cut + + diff --git a/challenge-210/robert-dicicco/python/ch-1.py b/challenge-210/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..886bd689e5 --- /dev/null +++ b/challenge-210/robert-dicicco/python/ch-1.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +''' +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-28 +Challenge 210 Kill&Win ( Python ) +-------------------------------------- +''' +myint =[[2,3,1],[1,1,2,2,2,3]] + +def Score(arr): + global total_score,max_score; + total_score = 0 + max_score = 0 + + for testx in arr: + print(f"trying {testx}") + for val in arr: + if ((val == testx) or (val == testx-1) or (val == testx+1)) : + total_score += val + if total_score == max_possible: + print(f"Output: {total_score}") + print("") + return + if total_score > max_score: + max_score = total_score + total_score = 0 + +for a in myint: + global max_possible; + print(f"Input: @int = {a}") + max_possible = sum(a) + print(f"max_possible = {max_possible}") + Score(a) +''' +-------------------------------------- +SAMPLE OUTPUT +python .\KillWin.py +Input: @int = [2, 3, 1] +max_possible = 6 +trying 2 +Output: 6 + +Input: @int = [1, 1, 2, 2, 2, 3] +max_possible = 11 +trying 1 +trying 1 +trying 2 +Output: 11 +-------------------------------------- +''' + + diff --git a/challenge-210/robert-dicicco/raku/ch-1.raku b/challenge-210/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..fab7235ae5 --- /dev/null +++ b/challenge-210/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,59 @@ +#!/usr/bin/env raku +use v6; +#`{ +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-28 +Challenge 210 Kill&Win ( Raku ) +-------------------------------------- +} +my @int = ([2,3,1],[1,1,2,2,2,3]); +my $total_score = 0; +my $max_score = 0; +my $max_possible = 0; + +sub Score(@a) { + for (@a) -> $testx { + say "trying $testx"; + for (@a) -> $val { + $total_score += $val if (($val == $testx) || ($val == $testx-1) || ($val == $testx+1)); + } + if ($total_score == $max_possible) { + say "Output: $total_score\n"; + return; + } + if ($total_score > $max_score) { + $max_score = $total_score; + } + $total_score = 0; + } + say "Output: $max_score\n"; +} + + +for (@int) -> @arr { + #my @arr = @$i; + say "Input: \@int = ("~@arr~")"; + $max_possible = sum(@arr[0..@arr.end]); + say "max_possible = $max_possible"; + Score(@arr); +} + +#`{ +-------------------------------------- +raku .\KillWin.rk +Input: @int = (2 3 1) +max_possible = 6 +trying 2 +Output: 6 + +Input: @int = (1 1 2 2 2 3) +max_possible = 11 +trying 1 +trying 1 +trying 2 +Output: 11 +-------------------------------------- +} + + diff --git a/challenge-210/robert-dicicco/ruby/ch-1.rb b/challenge-210/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..54007d4a9d --- /dev/null +++ b/challenge-210/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,62 @@ +#!/usr/bin/env ruby +=begin +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-28 +Challenge 210 Kill&Win ( Ruby ) +-------------------------------------- +=end + +myint =[[2,3,1],[1,1,2,2,2,3]] +$total_score = 0; +$max_score = 0; +$max_possible = 0; + +def Score(arr) + arr.each do |testx| + testx = testx.to_i + puts("trying #{testx}") + arr.each do |val| + val = val.to_i + $total_score += val if ((val == testx) || (val == testx-1) || (val == testx+1)) + end + if ($total_score == $max_possible) + puts("Output: #{$total_score}") + puts("") + return + end + if $total_score > $max_score + $max_score = $total_score + end + $total_score = 0 + end + puts("Output: #{$max_score}\n") + puts("") +end + +myint.each do |a| + puts("Input: @int = #{a}") + $max_possible = a.sum + puts("max possible = #{$max_possible}") + Score(a) +end + +=begin +-------------------------------------- +SAMPLE OUTPUT +ruby .\KillWin.rb +Input: @int = [2, 3, 1] +max possible = 6 +trying 2 +Output: 6 + +Input: @int = [1, 1, 2, 2, 2, 3] +max possible = 11 +trying 1 +trying 1 +trying 2 +Output: 11 +-------------------------------------- +=end + + diff --git a/challenge-210/ulrich-rieke/cpp/ch-1.cpp b/challenge-210/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..013b52b5c7 --- /dev/null +++ b/challenge-210/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> +#include <list> + +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 << "Please enter some integers, separated by a blank!\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( ) ) ; + std::list<int> theNumbers ; + for ( int i : numbers ) + theNumbers.push_back( i ) ; + theNumbers.sort( ) ; + theNumbers.unique( ) ; + std::vector<int> allSums ; + for ( auto it = theNumbers.begin( ) ; it != theNumbers.end( ) ; it++ ) { + int sum = 0 ; + int ct = std::count( numbers.begin( ) , numbers.end( ) , *it - 1) ; + sum += ct * (*it - 1) ; + ct = std::count( numbers.begin( ) , numbers.end( ) , *it ) ; + sum += ct * *it ; + ct = std::count( numbers.begin( ) , numbers.end( ) , *it + 1 ) ; + sum += ct * (*it + 1 ) ; + allSums.push_back( sum ) ; + } + std::cout<< *max_element( allSums.begin( ) , allSums.end( ) ) << std::endl ; + return 0 ; +} diff --git a/challenge-210/ulrich-rieke/cpp/ch-2.cpp b/challenge-210/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..52f7112565 --- /dev/null +++ b/challenge-210/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,84 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> +#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 ; +} + +bool isValid( const std::vector<int> & numbers ) { + int len = numbers.size( ) ; + if ( len == 0 || len == 1 ) + return true ; + else { + if ( len == 2 ) { + return ( ! ( (numbers[ 0 ] > 0 ) && ( numbers[ 1 ] < 0 ) ) ) ; + } + else { + std::vector<int> indices ( numbers.size( ) - 2 ) ; + std::iota( indices.begin( ) , indices.end( ) , 0 ) ; + return std::none_of( indices.begin( ) , indices.end( ) , [numbers]( int i ) { + return (numbers[i] > 0 && numbers[ i + 1 ] < 0 ) ; } ) ; + } + } +} + +std::vector<int> eliminate( const std::vector<int> & numbers ) { + std::vector<int> afterElimination ; + int len = numbers.size( ) ; + int pos = 0 ; + while ( ! ( numbers[ pos ] > 0 && numbers[ pos + 1 ] < 0 ) ) { + afterElimination.push_back( numbers[ pos ] ) ; + pos++ ; + } + int diff = abs( numbers[ pos ] ) - abs( numbers[ pos + 1 ] ) ; + if ( diff > 0 ) + afterElimination.push_back( numbers[ pos ] ) ; + if ( diff == 0 ) { + } + if ( diff < 0 ) { + afterElimination.push_back( numbers[ pos + 1 ] ) ; + } + pos += 2 ; + if ( pos < len ) { + while ( pos < len ) { + afterElimination.push_back( numbers[ pos ] ) ; + pos++ ; + } + } + return afterElimination ; +} + +int main( ) { + std::cout << "Enter some positive and negative 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> afterEliminate = eliminate( numbers ) ; + while ( ! isValid( afterEliminate ) ) { + afterEliminate = eliminate( afterEliminate ) ; + } + if ( afterEliminate.size( ) > 0 ) { + std::cout << '(' ; + for ( int i : afterEliminate ) + std::cout << i << " " ; + std::cout << ")\n" ; + } + else + std::cout << "()\n" ; + return 0 ; +} diff --git a/challenge-210/ulrich-rieke/haskell/ch-1.hs b/challenge-210/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..527ebf8131 --- /dev/null +++ b/challenge-210/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,13 @@ +module Challenge210 + where +import Data.List ( sort ) +import qualified Data.Set as S + +solution :: [Int] -> Int +solution list = maximum $ map (\[a , b , c] -> a * count a list + b * count b list + + c * count c list ) $ map (\i -> [i - 1 , i , i + 1] ) uniques + where + uniques :: [Int] + uniques = sort $ S.toList $ S.fromList list + count :: Int -> [Int] -> Int + count n l = length $ filter ( == n ) list diff --git a/challenge-210/ulrich-rieke/haskell/ch-2.hs b/challenge-210/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..444f063a25 --- /dev/null +++ b/challenge-210/ |
