aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-05-27 00:45:11 +0100
committerGitHub <noreply@github.com>2023-05-27 00:45:11 +0100
commit611749025bdbbf6cdd1e0de893453d3d1058c1c0 (patch)
treec28be41de47fadd6d86a0a785c60b23767bcd43a
parent2c96bed9f5aa2da0fa926cc9be1b64468542ed48 (diff)
parentd0ff7d70015252ad56c4f03dbf0290aa610cc765 (diff)
downloadperlweeklychallenge-club-611749025bdbbf6cdd1e0de893453d3d1058c1c0.tar.gz
perlweeklychallenge-club-611749025bdbbf6cdd1e0de893453d3d1058c1c0.tar.bz2
perlweeklychallenge-club-611749025bdbbf6cdd1e0de893453d3d1058c1c0.zip
Merge pull request #8136 from pauloscustodio/master
Add Perl solution
-rw-r--r--challenge-026/paulo-custodio/c/ch-1.c33
-rw-r--r--challenge-026/paulo-custodio/c/ch-2.c45
-rw-r--r--challenge-218/paulo-custodio/Makefile2
-rw-r--r--challenge-218/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-218/paulo-custodio/perl/ch-2.pl94
-rw-r--r--challenge-218/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-218/paulo-custodio/t/test-2.yaml10
7 files changed, 260 insertions, 0 deletions
diff --git a/challenge-026/paulo-custodio/c/ch-1.c b/challenge-026/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..26e85e147a
--- /dev/null
+++ b/challenge-026/paulo-custodio/c/ch-1.c
@@ -0,0 +1,33 @@
+/*
+Challenge 026
+
+Task #1
+Create a script that accepts two strings, let us call it, "stones" and
+"jewels". It should print the count of "alphabet" from the string "stones"
+found in the string "jewels". For example, if your stones is "chancellor" and
+"jewels" is "chocolate", then the script should print "8". To keep it simple,
+only A-Z,a-z characters are acceptable. Also make the comparison case
+sensitive.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int count_alphabet(const char* stones, const char* jewels) {
+ int count = 0;
+ for (const char* p = stones; *p; p++) {
+ if (strchr(jewels, *p))
+ count++;
+ }
+ return count;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 3) {
+ fputs("Usage: ch-1 stones jewels\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ printf("%d\n", count_alphabet(argv[1], argv[2]));
+}
diff --git a/challenge-026/paulo-custodio/c/ch-2.c b/challenge-026/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..565926e544
--- /dev/null
+++ b/challenge-026/paulo-custodio/c/ch-2.c
@@ -0,0 +1,45 @@
+/*
+Challenge 026
+
+Task #2
+Create a script that prints mean angles of the given list of angles in degrees.
+Please read wiki page that explains the formula in details with an example.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+double mean_angle(double *angles, size_t size) {
+ double y = 0.0, x = 0.0;
+ for (size_t i = 0; i < size; i++) {
+ x += cos(angles[i] * M_PI / 180.0);
+ y += sin(angles[i] * M_PI / 180.0);
+ }
+
+ return atan2(y / (double)size, x / (double)size) * 180.0 / M_PI;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ fputs("usage: ch-2 angles...\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ size_t size = argc-1;
+ double* angles = check_mem(malloc(size * sizeof(double)));
+ for (size_t i = 0; i < size; i++)
+ angles[i] = atof(argv[i+1]);
+
+ printf("%.1f", mean_angle(angles, size));
+
+ free(angles);
+}
diff --git a/challenge-218/paulo-custodio/Makefile b/challenge-218/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-218/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-218/paulo-custodio/perl/ch-1.pl b/challenge-218/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..2172a1675a
--- /dev/null
+++ b/challenge-218/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+# Challenge 218
+#
+# Task 1: Maximum Product
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of 3 or more integers.
+#
+# Write a script to find the 3 integers whose product is the maximum and return it.
+# Example 1
+#
+# Input: @list = (3, 1, 2)
+# Output: 6
+#
+# 1 x 2 x 3 => 6
+#
+# Example 2
+#
+# Input: @list = (4, 1, 3, 2)
+# Output: 24
+#
+# 2 x 3 x 4 => 24
+#
+# Example 3
+#
+# Input: @list = (-1, 0, 1, 3, 1)
+# Output: 3
+#
+# 1 x 1 x 3 => 3
+#
+# Example 4
+#
+# Input: @list = (-8, 2, -9, 0, -4, 3)
+# Output: 216
+#
+# -9 × -8 × 3 => 216
+
+use Modern::Perl;
+
+sub max_product {
+ my(@N) = @_;
+ return 0 if @N < 3;
+ my $max = 0;
+ for my $i (0 .. $#N-2) {
+ for my $j ($i+1 .. $#N-1) {
+ for my $k ($j+1 .. $#N) {
+ my $prod = $N[$i]*$N[$j]*$N[$k];
+ $max = $prod if $prod > $max;
+ }
+ }
+ }
+ return $max;
+}
+
+say max_product(@ARGV);
diff --git a/challenge-218/paulo-custodio/perl/ch-2.pl b/challenge-218/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..15cdefc7b0
--- /dev/null
+++ b/challenge-218/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/perl
+
+# Challenge 218
+#
+# Task 2: Matrix Score
+# Submitted by: Mohammad S Anwar
+#
+# You are given a m x n binary matrix i.e. having only 1 and 0.
+#
+# You are allowed to make as many moves as you want to get the highest score.
+#
+# A move can be either toggling each value in a row or column.
+#
+# To get the score, convert the each row binary to dec and return the sum.
+# Example 1:
+#
+# Input: @matrix = [ [0,0,1,1],
+# [1,0,1,0],
+# [1,1,0,0], ]
+# Output: 39
+#
+# Move #1: convert row #1 => 1100
+# [ [1,1,0,0],
+# [1,0,1,0],
+# [1,1,0,0], ]
+#
+# Move #2: convert col #3 => 101
+# [ [1,1,1,0],
+# [1,0,0,0],
+# [1,1,1,0], ]
+#
+# Move #3: convert col #4 => 111
+# [ [1,1,1,1],
+# [1,0,0,1],
+# [1,1,1,1], ]
+#
+# Score: 0b1111 + 0b1001 + 0b1111 => 15 + 9 + 15 => 39
+#
+# Example 2:
+#
+# Input: @matrix = [ [0] ]
+# Output: 1
+
+use Modern::Perl;
+use List::Util 'sum';
+
+sub compute_score {
+ my(@game) = @_;
+ return sum(map {oct("0b$_")} @game);
+}
+
+sub invert_row {
+ my($row, @game) = @_;
+ $game[$row] =~ tr/01/10/;
+ return @game;
+}
+
+sub invert_col {
+ my($col, @game) = @_;
+ for (@game) {
+ $_ = substr($_,0,$col).(1-substr($_,$col,1)).substr($_,$col+1);
+ }
+ return @game;
+}
+
+sub maximize_score {
+ my(@game) = @_;
+ my $max = compute_score(@game);
+ my $changed;
+ do {
+ $changed = 0;
+ for my $row (0..$#game) {
+ my @new_game = invert_row($row, @game);
+ my $new_max = compute_score(@new_game);
+ if ($new_max > $max) {
+ @game = @new_game;
+ $max = $new_max;
+ $changed = 1;
+ }
+ }
+ for my $col (0..length($game[0])-1) {
+ my @new_game = invert_col($col, @game);
+ my $new_max = compute_score(@new_game);
+ if ($new_max > $max) {
+ @game = @new_game;
+ $max = $new_max;
+ $changed = 1;
+ }
+ }
+ } while ($changed);
+ return @game;
+}
+
+say compute_score(maximize_score(@ARGV));
diff --git a/challenge-218/paulo-custodio/t/test-1.yaml b/challenge-218/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..538bbd6a57
--- /dev/null
+++ b/challenge-218/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 3 1 2
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 4 1 3 2
+ input:
+ output: 24
+- setup:
+ cleanup:
+ args: -1 0 1 3 1
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: -8 2 -9 0 -4 3
+ input:
+ output: 216
diff --git a/challenge-218/paulo-custodio/t/test-2.yaml b/challenge-218/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..d22f9cb2fb
--- /dev/null
+++ b/challenge-218/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 0011 1010 1100
+ input:
+ output: 39
+- setup:
+ cleanup:
+ args: 0
+ input:
+ output: 1