From 8fd1e1114b3c8f2307194d9edb2d52d9c76a833b Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:12:27 +0100 Subject: Solution to task 1 --- challenge-262/jo-37/perl/ch-1.pl | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 challenge-262/jo-37/perl/ch-1.pl (limited to 'challenge-262') 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 <[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; +} -- cgit From 04333790de1b022b4f2b83fc761a0d1005ef6172 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:15:46 +0100 Subject: Solution to task 2 --- challenge-262/jo-37/perl/ch-2.pl | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 challenge-262/jo-37/perl/ch-2.pl (limited to 'challenge-262') 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 < 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; +} -- cgit From 98ccc1a9ddc3ecd17c6c3ff9404e9af110799081 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 25 Mar 2024 18:29:22 +0100 Subject: Blog for challenge 262 --- challenge-262/jo-37/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-262/jo-37/blog.txt (limited to 'challenge-262') 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 -- cgit From 69367c5b88102fabee631384228617b990bf2e60 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 28 Mar 2024 14:02:57 +0000 Subject: - Added solutions by Jorg Sommrey. - Added solutions by Laurent Rosenfeld. --- challenge-262/laurent-rosenfeld/blog1.txt | 1 + challenge-262/laurent-rosenfeld/perl/ch-2.pl | 23 +++++++++++++++++++++++ challenge-262/laurent-rosenfeld/raku/ch-2.raku | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 challenge-262/laurent-rosenfeld/blog1.txt create mode 100644 challenge-262/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-262/laurent-rosenfeld/raku/ch-2.raku (limited to 'challenge-262') 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]; +} -- cgit From af833727a9f4c76635c9b752b03a857f0d26ed2a Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Thu, 28 Mar 2024 12:01:51 -0400 Subject: DAJ 262 --- challenge-262/dave-jacoby/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-262/dave-jacoby/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 challenge-262/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-262/dave-jacoby/perl/ch-2.pl (limited to 'challenge-262') 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; +} -- cgit From 209577d51a7745751b741cf7d4ccd95f66892697 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 28 Mar 2024 17:41:15 +0100 Subject: Add ch-1 in Perl, Python, Ruby, Go, Elixir --- challenge-262/spadacciniweb/elixir/ch-1.exs | 56 ++++++++++++++++++++++ challenge-262/spadacciniweb/go/ch-1.go | 72 +++++++++++++++++++++++++++++ challenge-262/spadacciniweb/perl/ch-1.pl | 59 +++++++++++++++++++++++ challenge-262/spadacciniweb/python/ch-1.py | 49 ++++++++++++++++++++ challenge-262/spadacciniweb/ruby/ch-1.rb | 47 +++++++++++++++++++ 5 files changed, 283 insertions(+) create mode 100644 challenge-262/spadacciniweb/elixir/ch-1.exs create mode 100644 challenge-262/spadacciniweb/go/ch-1.go create mode 100644 challenge-262/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-262/spadacciniweb/python/ch-1.py create mode 100644 challenge-262/spadacciniweb/ruby/ch-1.rb (limited to 'challenge-262') 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/challenge-262/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..d933ea2ec1 --- /dev/null +++ b/challenge-262/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,47 @@ +# 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) + positive = ints.select {|i| i > 0}.size + negative = ints.select {|i| i < 0}.size + + printf "(%s) -> %s\n", + ints.join(","), + [positive,negative].max +end + +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) -- cgit From 86c920c7e4cfa910783b50e3fcbb85e741a6d19c Mon Sep 17 00:00:00 2001 From: lancew Date: Thu, 28 Mar 2024 22:35:38 +0000 Subject: Initial implementation --- challenge-262/lance-wicks/perl/lib/MPN.pm | 41 +++++++++++++++++++++++++++++++ challenge-262/lance-wicks/perl/t/mpn.t | 38 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 challenge-262/lance-wicks/perl/lib/MPN.pm create mode 100644 challenge-262/lance-wicks/perl/t/mpn.t (limited to 'challenge-262') 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 -- cgit From 78cdc97226f16667a32c7df7046bb8fc17c7c3f5 Mon Sep 17 00:00:00 2001 From: ntovar Date: Thu, 28 Mar 2024 17:49:45 -0500 Subject: Challenge 262. Add Perl and Bash solutions. By Nelo Tovar. --- challenge-262/nelo-tovar/bash/ch-1.sh | 34 +++++++++++++++++++++++++++++ challenge-262/nelo-tovar/bash/ch-2.sh | 37 +++++++++++++++++++++++++++++++ challenge-262/nelo-tovar/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++ challenge-262/nelo-tovar/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100755 challenge-262/nelo-tovar/bash/ch-1.sh create mode 100755 challenge-262/nelo-tovar/bash/ch-2.sh create mode 100644 challenge-262/nelo-tovar/perl/ch-1.pl create mode 100644 challenge-262/nelo-tovar/perl/ch-2.pl (limited to 'challenge-262') 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..c88245da06 --- /dev/null +++ b/challenge-262/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,34 @@ +#!/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=("$@") + locali max=0 + + for i in "${nums[@]}"; do + i=${i#-} + if [ $i -gt $max ]; then + max=$i + fi + done + + echo $max +} + +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..204fe9d74c --- /dev/null +++ b/challenge-262/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/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 List::Util qw /max min/; +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 = max(grep {$_ >= 0} @$nums); + my $negative = abs(min(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 ' '; +} -- cgit From d7058de67dcc1d480b7d8cf33fd3e5e4fddf912a Mon Sep 17 00:00:00 2001 From: ntovar Date: Thu, 28 Mar 2024 18:02:19 -0500 Subject: Fix error in ch-1.pl and ch-1.sh --- challenge-262/nelo-tovar/bash/ch-1.sh | 16 +++++++++++----- challenge-262/nelo-tovar/perl/ch-1.pl | 5 ++--- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'challenge-262') diff --git a/challenge-262/nelo-tovar/bash/ch-1.sh b/challenge-262/nelo-tovar/bash/ch-1.sh index c88245da06..f0151dc053 100755 --- a/challenge-262/nelo-tovar/bash/ch-1.sh +++ b/challenge-262/nelo-tovar/bash/ch-1.sh @@ -8,16 +8,22 @@ function max_positive_negative() { local nums=("$@") - locali max=0 + local positive=0 + local negative=0 for i in "${nums[@]}"; do - i=${i#-} - if [ $i -gt $max ]; then - max=$i + if [ $i -ge 0 ]; then + ((positive++)) + else + ((negative++)) fi done - echo $max + if [ $positive -gt $negative ]; then + echo $positive + else + echo $negative + fi } example1='-3 1 2 -1 3 -2 4' diff --git a/challenge-262/nelo-tovar/perl/ch-1.pl b/challenge-262/nelo-tovar/perl/ch-1.pl index 204fe9d74c..ce3e9bf99c 100644 --- a/challenge-262/nelo-tovar/perl/ch-1.pl +++ b/challenge-262/nelo-tovar/perl/ch-1.pl @@ -10,7 +10,6 @@ use strict; use warnings; use v5.28; -use List::Util qw /max min/; use Data::Dump qw(dump); my @examples = ( @@ -21,8 +20,8 @@ my @examples = ( sub max_positive_negative { my $nums = shift; - my $positive = max(grep {$_ >= 0} @$nums); - my $negative = abs(min(grep {$_ < 0 } @$nums)); + my $positive = scalar grep {$_ >= 0} @$nums; + my $negative = scalar grep {$_ < 0 } @$nums; return $positive > $negative ? $positive : $negative; } -- cgit From 9814e9b1e879c8f88b9f4ba8031212faf160fd1b Mon Sep 17 00:00:00 2001 From: lancew Date: Fri, 29 Mar 2024 00:07:21 +0000 Subject: Add roc solution --- challenge-262/lance-wicks/roc/main.roc | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-262/lance-wicks/roc/main.roc (limited to 'challenge-262') 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 -- cgit From 27073c7df5b104dd7c63478003883dfd99fcbb83 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 29 Mar 2024 11:25:22 +0000 Subject: - Added solutions by Nelo Tovar. - Added solutions by Lance Wicks. --- challenge-262/lance-wicks/perl/ch-1.sh | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-262/lance-wicks/perl/ch-1.sh (limited to 'challenge-262') 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 -- cgit From 92a112547b3a615f87e42435788978eade7ff656 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Fri, 29 Mar 2024 12:29:58 +0100 Subject: Challenge 262 LK Perl Python --- challenge-262/lubos-kolouch/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-262/lubos-kolouch/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ challenge-262/lubos-kolouch/python/ch-1.py | 13 +++++++++++++ challenge-262/lubos-kolouch/python/ch-2.py | 16 ++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 challenge-262/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-262/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-262/lubos-kolouch/python/ch-1.py create mode 100644 challenge-262/lubos-kolouch/python/ch-2.py (limited to 'challenge-262') 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 -- cgit