diff options
| author | rir <rirans@comcast.net> | 2024-03-30 08:53:54 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-30 08:53:54 -0400 |
| commit | 28af4e6c7bc7a561f74a6aea4ebefb89612b04ef (patch) | |
| tree | bc42c63e6e7b9e77b40872e6226356c75e7e2bbd /challenge-262 | |
| parent | 293ea7e2fde6cf20c1e0f1140fe5e5574487c544 (diff) | |
| parent | 286052e238cd08089167db24b9bc2cb5c2f434d2 (diff) | |
| download | perlweeklychallenge-club-28af4e6c7bc7a561f74a6aea4ebefb89612b04ef.tar.gz perlweeklychallenge-club-28af4e6c7bc7a561f74a6aea4ebefb89612b04ef.tar.bz2 perlweeklychallenge-club-28af4e6c7bc7a561f74a6aea4ebefb89612b04ef.zip | |
Merge branch 'manwar:master' into work
Diffstat (limited to 'challenge-262')
25 files changed, 854 insertions, 0 deletions
diff --git a/challenge-262/dave-jacoby/perl/ch-1.pl b/challenge-262/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2caa4c1b3c --- /dev/null +++ b/challenge-262/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ -3, 1, 2, -1, 3, -2, 4 ], + [ -1, -2, -3, 1 ], + [ 1, 2 ], + +); + +for my $example (@examples) { + my $output = max_pos_neg( $example->@* ); + my @ints = $example->@*; + my $ints = join ',', @ints; + say <<"END"; + Input: \@ints = ($ints) + Output: $output +END +} + +sub max_pos_neg (@ints) { + my $pos = scalar grep { $_ > 0 } @ints; + my $neg = scalar grep { $_ < 0 } @ints; + return $pos > $neg ? $pos : $neg; +} diff --git a/challenge-262/dave-jacoby/perl/ch-2.pl b/challenge-262/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..3be159219a --- /dev/null +++ b/challenge-262/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + { ints => [ 3, 1, 2, 2, 2, 1, 3 ], k => 2 }, + { ints => [ 1, 2, 3 ], k => 1 }, +); + +for my $example (@examples) { + my $k = $example->{k}; + my @ints = $example->{ints}->@*; + my $output = count_equal_divisible( $k, @ints ); + my $ints = join ',', @ints; + say <<"END"; + Input: \@ints = ($ints) and \$k = $k + Output: $output +END +} + +sub count_equal_divisible ( $k, @ints ) { + my $output = 0; + for my $i ( 0 .. $#ints ) { + for my $j ( $i + 1 .. $#ints ) { + next unless $ints[$i] == $ints[$j]; + next unless ( $i * $j ) % $k == 0; + $output++; + } + } + return $output; +} diff --git a/challenge-262/jo-37/blog.txt b/challenge-262/jo-37/blog.txt new file mode 100644 index 0000000000..a343be35a4 --- /dev/null +++ b/challenge-262/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/03/28/ch-262.html diff --git a/challenge-262/jo-37/perl/ch-1.pl b/challenge-262/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..bff884ad9f --- /dev/null +++ b/challenge-262/jo-37/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util qw(max reduce); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [--] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of numbers + +EOS + + +### Input and Output + +say mpn(@ARGV); + + +### Implementation + +sub mpn { + max +(reduce {$a->[1 + ($b <=> 0)]++; $a} [0,0,0], @_)->@[0,2]; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is mpn(-3, 1, 2, -1, 3, -2, 4), 4, 'example 1'; + is mpn(-1, -2, -3, 1), 3, 'example 2'; + is mpn(1,2), 2, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-262/jo-37/perl/ch-2.pl b/challenge-262/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..bae511c438 --- /dev/null +++ b/challenge-262/jo-37/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV > 1; +usage: $0 [-examples] [-tests] [K I...] + +-examples + run the examples from the challenge + +-tests + run some tests + +K + (positive) integer + +I... + list of integers + +EOS + + +### Input and Output + +say ced(@ARGV); + + +### Implementation + +sub ced { + my $k = shift; + my $ints = long @_; + my $i = sequence($ints); + my $j = $i->dummy(0); + + which(($j > $i) & ($ints->dummy(0) == $ints) & ! ($i * $j % $k))->nelem; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is ced(2 ,=> 3,1,2,2,2,1,3), 4, 'example 1'; + is ced(1,2,3), 0, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-262/lance-wicks/perl/ch-1.sh b/challenge-262/lance-wicks/perl/ch-1.sh new file mode 100644 index 0000000000..7f15aef3cf --- /dev/null +++ b/challenge-262/lance-wicks/perl/ch-1.sh @@ -0,0 +1 @@ +perl -Ilib/ t/mpn.t diff --git a/challenge-262/lance-wicks/perl/lib/MPN.pm b/challenge-262/lance-wicks/perl/lib/MPN.pm new file mode 100644 index 0000000000..9a2190486b --- /dev/null +++ b/challenge-262/lance-wicks/perl/lib/MPN.pm @@ -0,0 +1,41 @@ +package MPN; + + +sub max_pos_or_neg { + my $self = shift; + my @ints = @_; + + my $positives = $self->positives(@ints); + my $negatives = $self->negatives(@ints); + + if ($positives > $negatives) { return $positives } + if ($positives < $negatives) { return $negatives } + return 0; + +} + +sub positives { + my $self = shift; + my @ints = @_; + + my $count = 0; + for my $i (@ints) { + $count++ if $i > 0; + } + + return $count; +} + +sub negatives { + my $self = shift; + my @ints = @_; + + my $count = 0; + for my $i (@ints) { + $count++ if $i < 0; + } + + return $count; +} + +1;
\ No newline at end of file diff --git a/challenge-262/lance-wicks/perl/t/mpn.t b/challenge-262/lance-wicks/perl/t/mpn.t new file mode 100644 index 0000000000..4cda02cc02 --- /dev/null +++ b/challenge-262/lance-wicks/perl/t/mpn.t @@ -0,0 +1,38 @@ +use Test2::V0 -target => 'MPN'; + +subtest "Example 1" => sub { + my @ints = (-3, 1, 2, -1, 3, -2, 4); + is $CLASS->positives(@ints), 4; + is $CLASS->negatives(@ints), 3; + is $CLASS->max_pos_or_neg(@ints), 4; +}; + +subtest "Example 2" => sub { + my @ints = (-1, -2, -3, 1); + is $CLASS->positives(@ints), 1; + is $CLASS->negatives(@ints), 3; + is $CLASS->max_pos_or_neg(@ints), 3; +}; + +subtest "Example 3" => sub { + my @ints = (1, 2); + is $CLASS->positives(@ints), 2; + is $CLASS->negatives(@ints), 0; + is $CLASS->max_pos_or_neg(@ints), 2; +}; + +subtest "Example 3" => sub { + my @ints = (1, 2); + is $CLASS->positives(@ints), 2; + is $CLASS->negatives(@ints), 0; + is $CLASS->max_pos_or_neg(@ints), 2; +}; + +subtest "Zero is neither positive or negative" => sub { + my @ints = (-0,+0,0); + is $CLASS->positives(@ints), 0; + is $CLASS->negatives(@ints), 0; + is $CLASS->max_pos_or_neg(@ints), 0; +}; + +done_testing;
\ No newline at end of file diff --git a/challenge-262/lance-wicks/roc/main.roc b/challenge-262/lance-wicks/roc/main.roc new file mode 100644 index 0000000000..fe51e6e95e --- /dev/null +++ b/challenge-262/lance-wicks/roc/main.roc @@ -0,0 +1,40 @@ +app "mpn" + packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br" } + imports [pf.Stdout] + provides [main] to pf + +main = + dbg positives [-3, 1, 2, -1, 3, -2, 4] + dbg negatives [-3, 1, 2, -1, 3, -2, 4] + dbg maxNegOrPos [-3, 1, 2, -1, 3, -2, 4] + dbg maxNegOrPos [-1, -2, -3, 1] + Stdout.line "run this with 'roc dev'" + + +maxNegOrPos = \ints -> + if positives ints > negatives ints then + positives ints + else if negatives ints > positives ints then + negatives ints + else + 0 + +positives = \ints -> + List.countIf ints Num.isPositive + +negatives = \ints -> + List.countIf ints Num.isNegative + + +# Tests +expect positives [-3, 1, 2, -1, 3, -2, 4] == 4 +expect negatives [-3, 1, 2, -1, 3, -2, 4] == 3 +expect maxNegOrPos [-3, 1, 2, -1, 3, -2, 4] == 4 + +expect positives [-1, -2, -3, 1] == 1 +expect negatives [-1, -2, -3, 1] == 3 +expect maxNegOrPos [-1, -2, -3, 1] == 3 + +expect positives [1,2] == 2 +expect negatives [1,2] == 0 +expect maxNegOrPos [1,2] == 2
\ No newline at end of file diff --git a/challenge-262/laurent-rosenfeld/blog1.txt b/challenge-262/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..b015a56644 --- /dev/null +++ b/challenge-262/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/03/perl-weekly-challenge-262-count-equal-divisible.html diff --git a/challenge-262/laurent-rosenfeld/perl/ch-2.pl b/challenge-262/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..2fe599238e --- /dev/null +++ b/challenge-262/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +use feature 'say'; + +sub count_equal_div { + my $divisor = shift; + die "$divisor cannot be 0" if $divisor == 0; + my @in = @_; + my $count = 0; + for my $i (0 .. $#in - 1) { + for my $j ($i+1 .. $#in) { + next if $in[$i] != $in[$j]; + $count++ if $i * $j % $divisor == 0; + } + } + return $count; +} + +my @tests = ( [2, [3,1,2,2,2,1,3]], [1, [1,2,3]] ); +for my $test (@tests) { + printf "%d - %-15s => ", $test->[0], "@{$test->[1]}"; + say count_equal_div @$test[0], @{$test->[1]}; +} diff --git a/challenge-262/laurent-rosenfeld/raku/ch-2.raku b/challenge-262/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..c28d78ed68 --- /dev/null +++ b/challenge-262/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,16 @@ +sub count-equal-div ($divisor where * != 0, @in) { + my $count = 0; + for 0..^@in.end -> $i { + for $i^..@in.end -> $j { + next if @in[$i] != @in[$j]; + $count++ if $i * $j %% $divisor; + } + } + return $count; +} + +my @tests = (2, (3,1,2,2,2,1,3)), (1, (1,2,3)); +for @tests -> @test { + printf "%d - %-15s => ", @test[0], "@test[1]"; + say count-equal-div @test[0], @test[1]; +} diff --git a/challenge-262/lubos-kolouch/perl/ch-1.pl b/challenge-262/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ab2df48556 --- /dev/null +++ b/challenge-262/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; + + +package Ch1; +use Carp; + +sub max_positive_negative { + my @ints = @_; + my $positive_count = scalar(grep { $_ > 0 } @ints); + my $negative_count = scalar(grep { $_ < 0 } @ints); + return $positive_count > $negative_count ? $positive_count : $negative_count; +} + +# Assert tests +croak "Test failed!" unless max_positive_negative(-3, 1, 2, -1, 3, -2, 4) == 4; +croak "Test failed!" unless max_positive_negative(-1, -2, -3, 1) == 3; +croak "Test failed!" unless max_positive_negative(1, 2) == 2; + +1;
\ No newline at end of file diff --git a/challenge-262/lubos-kolouch/perl/ch-2.pl b/challenge-262/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..c2eb3354ee --- /dev/null +++ b/challenge-262/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; + + +package Ch2; +use Carp; + +sub count_equal_divisible { + my ($ints_ref, $k) = @_; + my @ints = @$ints_ref; + my $count = 0; + my $n = scalar @ints; + + for my $i (0 .. $n - 1) { + for my $j ($i + 1 .. $n - 1) { + if ($ints[$i] == $ints[$j] && ($i * $j) % $k == 0) { + $count++; + } + } + } + return $count; +} + +# Assert tests +croak "Test failed!" unless count_equal_divisible([3, 1, 2, 2, 2, 1, 3], 2) == 4; +croak "Test failed!" unless count_equal_divisible([1, 2, 3], 1) == 0; + +1;
\ No newline at end of file diff --git a/challenge-262/lubos-kolouch/python/ch-1.py b/challenge-262/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f87605552f --- /dev/null +++ b/challenge-262/lubos-kolouch/python/ch-1.py @@ -0,0 +1,13 @@ +from typing import List + + +def max_positive_negative(ints: List[int]) -> int: + positive_count = sum(1 for x in ints if x > 0) + negative_count = sum(1 for x in ints if x < 0) + return max(positive_count, negative_count) + + +# Assert tests +assert max_positive_negative([-3, 1, 2, -1, 3, -2, 4]) == 4 +assert max_positive_negative([-1, -2, -3, 1]) == 3 +assert max_positive_negative([1, 2]) == 2 diff --git a/challenge-262/lubos-kolouch/python/ch-2.py b/challenge-262/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5d42adf2a0 --- /dev/null +++ b/challenge-262/lubos-kolouch/python/ch-2.py @@ -0,0 +1,16 @@ +from typing import List + + +def count_equal_divisible(ints: List[int], k: int) -> int: + count = 0 + n = len(ints) + for i in range(n): + for j in range(i + 1, n): + if ints[i] == ints[j] and i * j % k == 0: + count += 1 + return count + + +# Assert tests +assert count_equal_divisible([3, 1, 2, 2, 2, 1, 3], 2) == 4 +assert count_equal_divisible([1, 2, 3], 1) == 0 diff --git a/challenge-262/nelo-tovar/bash/ch-1.sh b/challenge-262/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..f0151dc053 --- /dev/null +++ b/challenge-262/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 262 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +# +# Task 1 : Max Positive Negative + +function max_positive_negative() { + local nums=("$@") + local positive=0 + local negative=0 + + for i in "${nums[@]}"; do + if [ $i -ge 0 ]; then + ((positive++)) + else + ((negative++)) + fi + done + + if [ $positive -gt $negative ]; then + echo $positive + else + echo $negative + fi +} + +example1='-3 1 2 -1 3 -2 4' +example2='-1 -2 -3 1' +example3='1 2' + +for e in "$example1" "$example2" "$example3"; do + array=($e) + mpn=($(max_positive_negative "${array[@]}")) + echo "Input : ints = (${array[@]})" + echo "Output : $mpn" + echo "" +done + diff --git a/challenge-262/nelo-tovar/bash/ch-2.sh b/challenge-262/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..1a2ebf4d9b --- /dev/null +++ b/challenge-262/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 262 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +# +# Task 2 : Count Equal Divisible + +function count_equal_divisible() { + local k=$1 + shift + local nums=("$@") + local len=${#nums[@]} + local count=0 + + for (( i = 0; i <= $len-1 ; i++ )); do + for (( j = $i+1; j < $len; j++ )); do + if [[ (${nums[$i]} -eq ${nums[$j]}) && $(($i * $j % $k)) -eq 0 ]]; then + ((count++)) + fi + done + done + + echo $count +} + +example_ints=('3 1 2 2 2 1 3' '1 2 3') +example_k=(2 1) + +for e in 0 1; do + array=(${example_ints[$e]}) + k=${example_k[$e]} + ced=$(count_equal_divisible $k "${array[@]}") + echo "Input : ints = (${array[@]}), and K = $k" + echo -e "Output : $ced\n" +done + diff --git a/challenge-262/nelo-tovar/perl/ch-1.pl b/challenge-262/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..ce3e9bf99c --- /dev/null +++ b/challenge-262/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 262 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +# +# Task 1 - Max Positive Negative +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ -3, 1, 2, -1, 3, -2, 4 ], + [ -1, -2, -3, 1 ], + [ 1, 2 ], +); + +sub max_positive_negative { + my $nums = shift; + my $positive = scalar grep {$_ >= 0} @$nums; + my $negative = scalar grep {$_ < 0 } @$nums; + + return $positive > $negative ? $positive : $negative; +} + +for my $elements (@examples) { + my $mpn = max_positive_negative $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $mpn; + say ' '; +} diff --git a/challenge-262/nelo-tovar/perl/ch-2.pl b/challenge-262/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..9c23b2ca6d --- /dev/null +++ b/challenge-262/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 262 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +# +# Task 2 - Count Equal Divisible +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + { k => 2, ints => [3,1,2,2,2,1,3] }, + { k => 1, ints => [1,2,3] }, +); + +sub count_equal_divisible { + my $input = shift; + my $ints = $input->{ints}; + my $length = scalar @$ints; + my $count = 0; + + for (my $i = 0; $i <= $length - 1; $i++) { + for (my $j = $i + 1; $j < $length; $j++) { + $count++ if (($ints->[$i] eq $ints->[$j]) and (($i * $j % $input->{k}) eq 0) ); + } + } + + return $count; +} + +for my $elements (@examples) { + my $ced = count_equal_divisible $elements; + + say 'Input : @ints = ', dump($elements->{ints}), ' and K = ', $elements->{k}; + say 'Output : ', $ced; + say ' '; +} diff --git a/challenge-262/spadacciniweb/elixir/ch-1.exs b/challenge-262/spadacciniweb/elixir/ch-1.exs new file mode 100644 index 0000000000..a4ebd1dfb9 --- /dev/null +++ b/challenge-262/spadacciniweb/elixir/ch-1.exs @@ -0,0 +1,56 @@ +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +defmodule MaximumOfPositiveAndNegative do + + def positive(ints) do + ints + |> Enum.count(fn x -> x > 0 end) + end + + def negative(ints) do + ints + |> Enum.count(fn x -> x < 0 end) + end + + def out(ints) do + IO.write( "(" <> Enum.join(ints, ",") <> ") -> ") + IO.puts( max(positive(ints), negative(ints)) ) + end +end + +ints = [-3, 1, 2, -1, 3, -2, 4] +MaximumOfPositiveAndNegative.out(ints) + +ints = [-1, -2, -3, 1] +MaximumOfPositiveAndNegative.out(ints) + +ints = [1,2] +MaximumOfPositiveAndNegative.out(ints) diff --git a/challenge-262/spadacciniweb/go/ch-1.go b/challenge-262/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..de672bf423 --- /dev/null +++ b/challenge-262/spadacciniweb/go/ch-1.go @@ -0,0 +1,72 @@ +/* +Task 1: Max Positive Negative +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints. +Write a script to return the maximum number of either positive or negative integers in the given array. + +Example 1 +Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +Output: 4 + +Count of positive integers: 4 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 4 + +Example 2 +Input: @ints = (-1, -2, -3, 1) +Output: 3 + +Count of positive integers: 1 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 3 + +Example 3 +Input: @ints = (1,2) +Output: 2 + +Count of positive integers: 2 +Count of negative integers: 0 +Maximum of count of positive and negative integers: 2 +*/ + +package main + +import ( + "fmt" +) + +func maxInt(x, y int) int { + if x < y { + return y + } + return x +} + +func maximum_of_positive_and_negative(ints []int) { + positiveInt := 0 + negativeInt := 0 + for i := 0; i < len(ints); i++ { + if ints[i] > 0 { + positiveInt++ + } else if ints[i] < 0 { + negativeInt++ + } + } + + s := fmt.Sprint(ints) + fmt.Printf("%s -> %d\n", + s, + maxInt(positiveInt, negativeInt)) +} + +func main() { + ints := []int{-3, 1, 2, -1, 3, -2, 4} + maximum_of_positive_and_negative(ints) + + ints = []int{-1, -2, -3, 1} + maximum_of_positive_and_negative(ints) + + ints = []int{1, 2} + maximum_of_positive_and_negative(ints) +} diff --git a/challenge-262/spadacciniweb/perl/ch-1.pl b/challenge-262/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..00686acb40 --- /dev/null +++ b/challenge-262/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl + +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +use strict; +use warnings; + +my @ints = (-3, 1, 2, -1, 3, -2, 4); +maximum_of_positive_and_negative(\@ints); + +@ints = (-1, -2, -3, 1); +maximum_of_positive_and_negative(\@ints); + +@ints = (1,2); +maximum_of_positive_and_negative(\@ints); + +exit 0; + +sub maximum_of_positive_and_negative { + my $ints = shift; + + my $positive = scalar map { $_ > 0 ? 1 : () } + @$ints; + my $negative = scalar map { $_ < 0 ? 1 : () } + @$ints; + printf "(%s) -> %s\n", + (join ',', @$ints), + $positive > $negative ? $positive : $negative; + + return undef; +} diff --git a/challenge-262/spadacciniweb/python/ch-1.py b/challenge-262/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..d9782dc03a --- /dev/null +++ b/challenge-262/spadacciniweb/python/ch-1.py @@ -0,0 +1,49 @@ +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +def maximum_of_positive_and_negative(ints): + negative = len(list(filter(lambda x: (x < 0), ints))) + positive = len(list(filter(lambda x: (x > 0), ints))) + + print("(%s) -> %d" % + ( ",".join(map(str, ints)), + max(positive, negative) + ) + ) + +if __name__ == "__main__": + ints = [-3, 1, 2, -1, 3, -2, 4] + maximum_of_positive_and_negative(ints) + + ints = [-1, -2, -3, 1] + maximum_of_positive_and_negative(ints) + + ints = [1,2] + maximum_of_positive_and_negative(ints) diff --git a/challenge-262/spadacciniweb/ruby/ch-1.rb b/challen |
