aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-02 21:32:04 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-02 21:32:04 +0000
commit5c33806bdd7d70d9179fec0594ffec63afa23ab2 (patch)
treeafe2a9eefdfb1f490e82385f611679dbde2d7742
parent1c86d96be6cff3051f909f168d2ec69446641626 (diff)
downloadperlweeklychallenge-club-5c33806bdd7d70d9179fec0594ffec63afa23ab2.tar.gz
perlweeklychallenge-club-5c33806bdd7d70d9179fec0594ffec63afa23ab2.tar.bz2
perlweeklychallenge-club-5c33806bdd7d70d9179fec0594ffec63afa23ab2.zip
Add Perl, C and C++ solutions
-rw-r--r--challenge-204/paulo-custodio/Makefile2
-rw-r--r--challenge-204/paulo-custodio/c/ch-1.c62
-rw-r--r--challenge-204/paulo-custodio/c/ch-2.c100
-rw-r--r--challenge-204/paulo-custodio/cpp/ch-1.cpp58
-rw-r--r--challenge-204/paulo-custodio/cpp/ch-2.cpp94
-rw-r--r--challenge-204/paulo-custodio/perl/ch-1.pl49
-rw-r--r--challenge-204/paulo-custodio/perl/ch-2.pl72
-rw-r--r--challenge-204/paulo-custodio/t/test-1.yaml31
-rw-r--r--challenge-204/paulo-custodio/t/test-2.yaml28
9 files changed, 496 insertions, 0 deletions
diff --git a/challenge-204/paulo-custodio/Makefile b/challenge-204/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-204/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-204/paulo-custodio/c/ch-1.c b/challenge-204/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..fb1bac1842
--- /dev/null
+++ b/challenge-204/paulo-custodio/c/ch-1.c
@@ -0,0 +1,62 @@
+/*
+Challenge 204
+
+Task 1: Monotonic Array
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0.
+
+ An array is Monotonic if it is either monotone increasing or decreasing.
+
+Monotone increasing: for i <= j , nums[i] <= nums[j]
+Monotone decreasing: for i <= j , nums[i] >= nums[j]
+
+
+Example 1
+
+Input: @nums = (1,2,2,3)
+Output: 1
+
+Example 2
+
+Input: @nums = (1,3,2)
+Output: 0
+
+Example 3
+
+Input: @nums = (6,5,5,4)
+Output: 1
+*/
+
+#define MAX_DATA 1024
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int is_monotonic(int size, int data[]) {
+ if (size < 2)
+ return 1;
+ else {
+ int climb = 0, descend = 0;
+ for (int i = 0; i < size - 1; i++) {
+ int delta = data[i+1]-data[i];
+ if (delta > 0) climb++;
+ else if (delta < 0) descend++;
+ if (climb && descend) return 0;
+ }
+ return 1;
+ }
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ assert(argc <= MAX_DATA);
+ int data[MAX_DATA];
+
+ for (int i = 0; i < argc; i++)
+ data[i] = atoi(argv[i]);
+ printf("%d\n", is_monotonic(argc, data));
+}
diff --git a/challenge-204/paulo-custodio/c/ch-2.c b/challenge-204/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..d722b21321
--- /dev/null
+++ b/challenge-204/paulo-custodio/c/ch-2.c
@@ -0,0 +1,100 @@
+/*
+Challenge 204
+
+Task 2: Reshape Matrix
+Submitted by: Mohammad S Anwar
+
+You are given a matrix (m x n) and two integers (r) and (c).
+
+Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0.
+
+Example 1
+
+Input: [ 1 2 ]
+ [ 3 4 ]
+
+ $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
+ $r = 1
+ $c = 4
+
+Output: [ 1 2 3 4 ]
+
+Example 2
+
+Input: [ 1 2 3 ]
+ [ 4 5 6 ]
+
+ $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
+ $r = 3
+ $c = 2
+
+Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
+
+ [ 1 2 ]
+ [ 3 4 ]
+ [ 5 6 ]
+
+Example 3
+
+Input: [ 1 2 ]
+
+ $matrix = [ [ 1, 2 ] ]
+ $r = 3
+ $c = 2
+
+Output: 0
+*/
+
+#define MAX_DATA 1024
+#define BLANKS " \t\r\n\v\f"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int data[MAX_DATA], size = 0, rows = 0, cols = 0;
+
+void parse_data() {
+ char line[MAX_DATA], *p;
+ while (fgets(line, sizeof(line), stdin)) {
+ if (strchr(line, '[') != NULL) {
+ while ((p = strchr(line, '[')) != NULL) *p = ' ';
+ while ((p = strchr(line, ']')) != NULL) *p = ' ';
+ char* p = strtok(line, BLANKS);
+ while (p) {
+ assert(size < MAX_DATA);
+ data[size++] = atoi(p);
+ p = strtok(NULL, BLANKS);
+ }
+ }
+ else {
+ char* p = strtok(line, BLANKS);
+ assert(p);
+ rows = atoi(p);
+ p = strtok(NULL, BLANKS);
+ assert(p);
+ cols = atoi(p);
+ break;
+ }
+ }
+}
+
+void output_data() {
+ if (size != rows*cols)
+ printf("0\n");
+ else {
+ for (int i = 0; i < rows; i++) {
+ printf("[ ");
+ for (int j = 0; j < cols; j++) {
+ printf("%d ", data[i*cols+j]);
+ }
+ printf("]\n");
+ }
+ }
+}
+
+int main() {
+ parse_data();
+ output_data();
+}
diff --git a/challenge-204/paulo-custodio/cpp/ch-1.cpp b/challenge-204/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..e877007577
--- /dev/null
+++ b/challenge-204/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,58 @@
+/*
+Challenge 204
+
+Task 1: Monotonic Array
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0.
+
+ An array is Monotonic if it is either monotone increasing or decreasing.
+
+Monotone increasing: for i <= j , nums[i] <= nums[j]
+Monotone decreasing: for i <= j , nums[i] >= nums[j]
+
+
+Example 1
+
+Input: @nums = (1,2,2,3)
+Output: 1
+
+Example 2
+
+Input: @nums = (1,3,2)
+Output: 0
+
+Example 3
+
+Input: @nums = (6,5,5,4)
+Output: 1
+*/
+
+#include <iostream>
+#include <vector>
+
+int is_monotonic(const std::vector<int>& data) {
+ if (data.size() < 2)
+ return 1;
+ else {
+ int climb = 0, descend = 0;
+ for (size_t i = 0; i < data.size() - 1; i++) {
+ int delta = data[i+1]-data[i];
+ if (delta > 0) climb++;
+ else if (delta < 0) descend++;
+ if (climb && descend) return 0;
+ }
+ return 1;
+ }
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ std::vector<int> data;
+
+ for (int i = 0; i < argc; i++)
+ data.push_back(atoi(argv[i]));
+ std::cout << is_monotonic(data) << std::endl;
+}
diff --git a/challenge-204/paulo-custodio/cpp/ch-2.cpp b/challenge-204/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..331715e1c7
--- /dev/null
+++ b/challenge-204/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,94 @@
+/*
+Challenge 204
+
+Task 2: Reshape Matrix
+Submitted by: Mohammad S Anwar
+
+You are given a matrix (m x n) and two integers (r) and (c).
+
+Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0.
+
+Example 1
+
+Input: [ 1 2 ]
+ [ 3 4 ]
+
+ $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
+ $r = 1
+ $c = 4
+
+Output: [ 1 2 3 4 ]
+
+Example 2
+
+Input: [ 1 2 3 ]
+ [ 4 5 6 ]
+
+ $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
+ $r = 3
+ $c = 2
+
+Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
+
+ [ 1 2 ]
+ [ 3 4 ]
+ [ 5 6 ]
+
+Example 3
+
+Input: [ 1 2 ]
+
+ $matrix = [ [ 1, 2 ] ]
+ $r = 3
+ $c = 2
+
+Output: 0
+*/
+
+#include <iostream>
+#include <sstream>
+#include <vector>
+#include <string>
+
+std::vector<int> data;
+size_t rows = 0, cols = 0;
+
+void parse_data() {
+ std::string line;
+ while (std::getline(std::cin, line)) {
+ if (line.find('[') != std::string::npos) {
+ size_t pos;
+ while ((pos = line.find('[')) != std::string::npos) line[pos] = ' ';
+ while ((pos = line.find(']')) != std::string::npos) line[pos] = ' ';
+ std::istringstream iss{ line };
+ int n;
+ while (iss >> n) {
+ data.push_back(n);
+ }
+ }
+ else {
+ std::istringstream iss{ line };
+ iss >> rows >> cols;
+ break;
+ }
+ }
+}
+
+void output_data() {
+ if (data.size() != rows*cols)
+ std::cout << "0" << std::endl;
+ else {
+ for (int i = 0; i < rows; i++) {
+ std::cout << "[ ";
+ for (int j = 0; j < cols; j++) {
+ std::cout << data[i*cols+j] << " ";
+ }
+ std::cout << "]" << std::endl;
+ }
+ }
+}
+
+int main() {
+ parse_data();
+ output_data();
+}
diff --git a/challenge-204/paulo-custodio/perl/ch-1.pl b/challenge-204/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..9dbd93f7a4
--- /dev/null
+++ b/challenge-204/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+# Challenge 204
+#
+# Task 1: Monotonic Array
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers.
+#
+# Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0.
+#
+# An array is Monotonic if it is either monotone increasing or decreasing.
+#
+# Monotone increasing: for i <= j , nums[i] <= nums[j]
+# Monotone decreasing: for i <= j , nums[i] >= nums[j]
+#
+#
+# Example 1
+#
+# Input: @nums = (1,2,2,3)
+# Output: 1
+#
+# Example 2
+#
+# Input: @nums = (1,3,2)
+# Output: 0
+#
+# Example 3
+#
+# Input: @nums = (6,5,5,4)
+# Output: 1
+
+use Modern::Perl;
+
+sub is_monotonic {
+ my(@list) = @_;
+ return 1 if @list < 2;
+ my $climb = 0;
+ my $descend = 0;
+ for my $i (0..$#list-1) {
+ my $delta = $list[$i+1]-$list[$i];
+ if ($delta > 0) { $climb++; }
+ elsif ($delta < 0) { $descend++; }
+ if ($climb && $descend) { return 0; }
+ }
+ return 1;
+}
+
+say is_monotonic(@ARGV);
diff --git a/challenge-204/paulo-custodio/perl/ch-2.pl b/challenge-204/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..dcd0fadaab
--- /dev/null
+++ b/challenge-204/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# Challenge 204
+#
+# Task 2: Reshape Matrix
+# Submitted by: Mohammad S Anwar
+#
+# You are given a matrix (m x n) and two integers (r) and (c).
+#
+# Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0.
+#
+# Example 1
+#
+# Input: [ 1 2 ]
+# [ 3 4 ]
+#
+# $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
+# $r = 1
+# $c = 4
+#
+# Output: [ 1 2 3 4 ]
+#
+# Example 2
+#
+# Input: [ 1 2 3 ]
+# [ 4 5 6 ]
+#
+# $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
+# $r = 3
+# $c = 2
+#
+# Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
+#
+# [ 1 2 ]
+# [ 3 4 ]
+# [ 5 6 ]
+#
+# Example 3
+#
+# Input: [ 1 2 ]
+#
+# $matrix = [ [ 1, 2 ] ]
+# $r = 3
+# $c = 2
+#
+# Output: 0
+
+use Modern::Perl;
+
+my @list;
+my($r, $c);
+while (<>) {
+ if (s/[\[\]]//g) {
+ push @list, split(' ', $_);
+ }
+ else {
+ ($r, $c) = split(' ', $_);
+ last;
+ }
+}
+if ($r*$c == @list) {
+ for (1..$r) {
+ print "[ ";
+ for (1..$c) {
+ print shift(@list), " ";
+ }
+ print "]\n";
+ }
+}
+else {
+ print 0;
+}
diff --git a/challenge-204/paulo-custodio/t/test-1.yaml b/challenge-204/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1ffd89f985
--- /dev/null
+++ b/challenge-204/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,31 @@
+- setup:
+ cleanup:
+ args: 1 2 2 3
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1 3 2
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 6 5 5 4
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1 1 1 1
+ input:
+ output: 1
+
diff --git a/challenge-204/paulo-custodio/t/test-2.yaml b/challenge-204/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..9ca698d882
--- /dev/null
+++ b/challenge-204/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,28 @@
+- setup:
+ cleanup:
+ args:
+ input: |
+ |[ 1 2 ]
+ |[ 3 4 ]
+ |1 4
+ output: |
+ |[ 1 2 3 4 ]
+- setup:
+ cleanup:
+ args:
+ input: |
+ |[ 1 2 3 ]
+ |[ 4 5 6 ]
+ |3 2
+ output: |
+ |[ 1 2 ]
+ |[ 3 4 ]
+ |[ 5 6 ]
+- setup:
+ cleanup:
+ args:
+ input: |
+ |[ 1 2 ]
+ |3 2
+ output: |
+ |0