aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorntovar <tovar.nelo@gmail.com>2024-05-25 14:29:05 -0500
committerntovar <tovar.nelo@gmail.com>2024-05-25 14:29:05 -0500
commited0ffb69d47a48a03f44c20b385e46142331e7bc (patch)
tree2f12f3c6f5f7cdcea4ea834191c96af3d4024dbc
parented462bf99ed6fda013ab14d58855951ef13b05fa (diff)
downloadperlweeklychallenge-club-ed0ffb69d47a48a03f44c20b385e46142331e7bc.tar.gz
perlweeklychallenge-club-ed0ffb69d47a48a03f44c20b385e46142331e7bc.tar.bz2
perlweeklychallenge-club-ed0ffb69d47a48a03f44c20b385e46142331e7bc.zip
Challenge 270. Add Perl and Bash(ch-2.sh) solutions. By Nelo Tovar
-rwxr-xr-xchallenge-270/nelo-tovar/bash/ch-2.sh77
-rw-r--r--challenge-270/nelo-tovar/perl/ch-1.pl54
-rw-r--r--challenge-270/nelo-tovar/perl/ch-2.pl54
3 files changed, 185 insertions, 0 deletions
diff --git a/challenge-270/nelo-tovar/bash/ch-2.sh b/challenge-270/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..933ab5daf1
--- /dev/null
+++ b/challenge-270/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 270 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+#
+# Task 2 : Equalize Array
+
+function max() {
+ local array=("$@")
+ local max=${array[0]}
+
+ for n in ${array[@]}; do
+ if [[ n -gt $max ]]; then
+ max=$n
+ fi
+ done
+
+ echo $max
+}
+
+function get_less_than() {
+ local number=$1
+ shift 1
+ local array=("$@")
+ local temp=()
+
+ for i in ${array[@]}; do
+ if [[ $i < $number ]]; then
+ temp+=($i)
+ fi
+ done
+
+ echo "${temp[@]}"
+}
+
+function equalize_array() {
+ local x=$1
+ local y=$2
+ shift 2
+ local ints=("$@")
+ local max=$(max "${ints[@]}")
+ local to_process=($(get_less_than $max "${ints[@]}"))
+ local len=${#to_process[@]}
+ local cost=0
+
+ while [[ $len > 0 ]]; do
+ if [[ $len -eq 1 ]]; then
+ ((to_process[0]++))
+ ((cost+=$x))
+ else
+ ((to_process[0]++))
+ ((to_process[1]++))
+ ((cost+=$y))
+
+ fi
+ to_process=($(get_less_than $max "${to_process[@]}")) >&2
+ len=${#to_process[@]}
+
+ done
+
+ echo $cost
+}
+
+examples_ints=('4 1' '2 3 3 3 5')
+examples_x=(3 2)
+examples_y=(2 1)
+
+for e in ${!examples_ints[@]}; do
+ array=(${examples_ints[$e]})
+ x=${examples_x[$e]}
+ y=${examples_y[$e]}
+ ea=$(equalize_array $x $y "${array[@]}")
+ echo "Input : ints = (${array[@]}), x = $x and y = $y"
+ echo -e "Output : $ea\n"
+done
+
diff --git a/challenge-270/nelo-tovar/perl/ch-1.pl b/challenge-270/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..231313ec58
--- /dev/null
+++ b/challenge-270/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 270 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+#
+# Task 1 - Special Positions
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::MoreUtils qw(one_u);
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ [1, 0, 0],
+ [0, 0, 1],
+ [1, 0, 0], ],
+ [ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1], ],
+);
+
+sub special_positions {
+ my $matrix = shift;
+ my $len_rows = scalar @$matrix;
+ my $len_cols = scalar @{$matrix->[0]};
+ my $count = 0;
+
+ for (my $i = 0; $i < $len_rows; $i++) {
+ my @list_row = @{$matrix->[$i]};
+ next unless one_u { $_ == 1 } @list_row;
+
+ my @list_col;
+ for (my $j = 0; $j < $len_cols; $j++) {
+ push @list_col, @{$matrix->[$j]}[$i];
+ }
+ next unless one_u { $_ == 1 } @list_col;
+
+ $count++;
+
+ }
+
+ return $count;
+}
+
+for my $elements (@examples) {
+ my $sp = special_positions $elements;
+
+ say 'Input : @matrix = ', dump(@$elements);
+ say 'Output : ', $sp;
+ say ' ';
+}
diff --git a/challenge-270/nelo-tovar/perl/ch-2.pl b/challenge-270/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..b1c2006122
--- /dev/null
+++ b/challenge-270/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 270 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+#
+# Task 2 - Equalize Array
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw (min max sum);
+use Data::Dump qw(dump);
+
+my @examples = (
+ { ints => [ 4, 1 ], x =>3, y => 2},
+ { ints => [ 2, 3, 3, 3, 5 ], x=> 2, y => 1},
+);
+
+sub equalize_array {
+ my $params = shift;
+ my $x = $params->{x};
+ my $y = $params->{y};
+ my @ints = @{$params->{ints}};
+ my $max = max(@ints);
+ my @to_process = grep { $_ < $max} @ints;
+ my $length = scalar @to_process;
+ my $cost = 0;
+
+ while ($length > 0) {
+ if ($length == 1) {
+ $to_process[0]++;
+ $cost += $x
+ }else{
+ $to_process[0]++;
+ $to_process[1]++;
+ $cost += $y
+ }
+
+ @to_process = grep { $_ < $max} @to_process;
+ $length = scalar @to_process;
+ }
+
+ return $cost;
+}
+
+for my $elements (@examples) {
+ my $ea = equalize_array $elements;
+
+ say 'Input : @ints = ', dump($elements->{ints}), " x = $elements->{x} and y = $elements->{y}";
+ say 'Output : ', $ea;
+ say ' ';
+}