aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-17 22:24:55 +0000
committerGitHub <noreply@github.com>2021-02-17 22:24:55 +0000
commit648d7e5c0df6c4f6c844baae70dec84e33f716e4 (patch)
treeddba38352fc3857545acb096b4b5708a28bad352
parent833512d983db4860442b52c48e0edcc10c0dce92 (diff)
parenta4af5299879128e7a16f4d81aa96c5b7f18f6744 (diff)
downloadperlweeklychallenge-club-648d7e5c0df6c4f6c844baae70dec84e33f716e4.tar.gz
perlweeklychallenge-club-648d7e5c0df6c4f6c844baae70dec84e33f716e4.tar.bz2
perlweeklychallenge-club-648d7e5c0df6c4f6c844baae70dec84e33f716e4.zip
Merge pull request #3561 from pauloscustodio/paulo-custodio
Add Awk, C, C++ and Python solutions to challenge 100
-rw-r--r--challenge-100/paulo-custodio/awk/ch-1.awk55
-rw-r--r--challenge-100/paulo-custodio/awk/ch-2.awk78
-rw-r--r--challenge-100/paulo-custodio/c/ch-1.c87
-rw-r--r--challenge-100/paulo-custodio/c/ch-2.c142
-rw-r--r--challenge-100/paulo-custodio/cpp/ch-1.cpp111
-rw-r--r--challenge-100/paulo-custodio/cpp/ch-2.cpp119
-rw-r--r--challenge-100/paulo-custodio/perl/ch-1.pl3
-rw-r--r--challenge-100/paulo-custodio/python/ch-1.py28
-rw-r--r--challenge-100/paulo-custodio/python/ch-2.py74
9 files changed, 696 insertions, 1 deletions
diff --git a/challenge-100/paulo-custodio/awk/ch-1.awk b/challenge-100/paulo-custodio/awk/ch-1.awk
new file mode 100644
index 0000000000..03d750b366
--- /dev/null
+++ b/challenge-100/paulo-custodio/awk/ch-1.awk
@@ -0,0 +1,55 @@
+#!/usr/bin/gawk
+
+# TASK #1 > Fun Time
+# Submitted by: Mohammad S Anwar
+# You are given a time (12 hour / 24 hour).
+#
+# Write a script to convert the given time from 12 hour format to 24 hour format
+# and vice versa.
+#
+# Ideally we expect a one-liner.
+#
+# Example 1:
+# Input: 05:15 pm or 05:15pm
+# Output: 17:15
+# Example 2:
+# Input: 19:15
+# Output: 07:15 pm or 07:15pm
+
+function convert_time(text, hour, arr, pm) {
+ text = tolower(text)
+
+ # get hour
+ match(text, /^([0-9]+)/, arr)
+ hour = arr[1]
+ if (text ~ /am|pm/) {
+ # 12->24
+ if (text ~ /pm/) {
+ if (hour < 12) hour += 12;
+ }
+ else { /am/
+ if (hour == 12) hour = 0;
+ }
+ gsub(/^[0-9]+/, "", text);
+ gsub(/ *(am|pm)/, "", text);
+ printf("%02d", hour); print text;
+ }
+ else {
+ # 24->12
+ pm = 0;
+ if (hour == 0) hour = 12;
+ else if (hour == 12) pm = 1;
+ else if (hour > 12) { hour -= 12; pm = 1; }
+ gsub(/^[0-9]+/, "", text);
+ printf("%02d%s", hour, text);
+ if (pm)
+ print "pm";
+ else
+ print "am";
+ }
+}
+
+BEGIN {
+ convert_time(ARGV[1]);
+ exit 0;
+} \ No newline at end of file
diff --git a/challenge-100/paulo-custodio/awk/ch-2.awk b/challenge-100/paulo-custodio/awk/ch-2.awk
new file mode 100644
index 0000000000..d315475853
--- /dev/null
+++ b/challenge-100/paulo-custodio/awk/ch-2.awk
@@ -0,0 +1,78 @@
+#!/usr/bin/gawk
+
+# TASK #2 > Triangle Sum
+# Submitted by: Mohammad S Anwar
+# You are given triangle array.
+#
+# Write a script to find the minimum path sum from top to bottom.
+#
+# When you are on index i on the current row then you may move to either
+# index i or index i + 1 on the next row.
+#
+# Example 1:
+# Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ]
+# Output: 8
+#
+# Explanation: The given triangle
+#
+# 1
+# 2 4
+# 6 4 9
+# 5 1 7 2
+#
+# The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8
+#
+# [1]
+# [2] 4
+# 6 [4] 9
+# 5 [1] 7 2
+# Example 2:
+# Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ]
+# Output: 7
+#
+# Explanation: The given triangle
+#
+# 3
+# 3 1
+# 5 2 3
+# 4 3 1 3
+#
+# The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7
+#
+# [3]
+# 3 [1]
+# 5 [2] 3
+# 4 3 [1] 3
+
+function add_row(row, text, i, arr) {
+ rows = row;
+ for (i = 1; i <= row; i++) {
+ gsub(/^[^0-9]*/, "", text);
+ match(text, /^([0-9]+)/, arr);
+ triangle[row][i] = arr[1];
+ gsub(/^[0-9]*/, "", text);
+ }
+}
+
+function min_sum_1(sum, row, col, sum1, sum2) {
+ sum += triangle[row][col];
+ if (row == rows)
+ return sum;
+ else {
+ sum1 = min_sum_1(sum, row+1, col);
+ sum2 = min_sum_1(sum, row+1, col+1);
+ return (sum1 < sum2) ? sum1 : sum2;
+ }
+}
+
+function min_sum() {
+ return min_sum_1(0, 1, 1);
+}
+
+BEGIN {
+ rows = 0;
+ for (i = 1; i < ARGC; i++)
+ add_row(i, ARGV[i]);
+ print min_sum();
+ exit 0;
+}
diff --git a/challenge-100/paulo-custodio/c/ch-1.c b/challenge-100/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..d0a33a1f36
--- /dev/null
+++ b/challenge-100/paulo-custodio/c/ch-1.c
@@ -0,0 +1,87 @@
+/*
+TASK #1 > Fun Time
+Submitted by: Mohammad S Anwar
+You are given a time (12 hour / 24 hour).
+
+Write a script to convert the given time from 12 hour format to 24 hour format
+and vice versa.
+
+Ideally we expect a one-liner.
+
+Example 1:
+Input: 05:15 pm or 05:15pm
+Output: 17:15
+Example 2:
+Input: 19:15
+Output: 07:15 pm or 07:15pm
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <ctype.h>
+
+typedef struct Time {
+ int hours, minutes;
+ bool is12;
+} Time;
+
+bool parse_time(Time* time, const char* text) {
+ char buffer[BUFSIZ];
+ time->hours = time->minutes = 0;
+ time->is12 = false;
+ buffer[0] = '\0';
+
+ if (!sscanf(text, "%d:%d %s", &time->hours, &time->minutes, buffer))
+ return false;
+
+ switch (tolower(buffer[0])) {
+ case 'a':
+ if (time->hours == 12) time->hours = 0;
+ time->is12 = true;
+ break;
+ case 'p':
+ if (time->hours != 12) time->hours += 12;
+ time->is12 = true;
+ break;
+ case '\0':
+ break;
+ default:
+ return false;
+ }
+
+ return true;
+}
+
+void print_time24(int hours, int minutes) {
+ printf("%02d:%02d", hours, minutes);
+}
+
+void print_time12(int hours, int minutes) {
+ bool ispm = false;
+ if (hours == 0)
+ hours = 12;
+ else if (hours == 12)
+ ispm = true;
+ else if (hours > 12) {
+ ispm = true;
+ hours -= 12;
+ }
+ print_time24(hours, minutes);
+ if (ispm)
+ printf("pm");
+ else
+ printf("am");
+}
+
+int main(int argc, char* argv[]) {
+ Time time;
+ if (argc != 2 || !parse_time(&time, argv[1])) {
+ fputs("Usage: ch-1 time", stderr);
+ return EXIT_FAILURE;
+ }
+ if (time.is12)
+ print_time24(time.hours, time.minutes);
+ else
+ print_time12(time.hours, time.minutes);
+}
diff --git a/challenge-100/paulo-custodio/c/ch-2.c b/challenge-100/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..1d68e1d2d6
--- /dev/null
+++ b/challenge-100/paulo-custodio/c/ch-2.c
@@ -0,0 +1,142 @@
+/*
+TASK #2 > Triangle Sum
+Submitted by: Mohammad S Anwar
+You are given triangle array.
+
+Write a script to find the minimum path sum from top to bottom.
+
+When you are on index i on the current row then you may move to either
+index i or index i + 1 on the next row.
+
+Example 1:
+Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ]
+Output: 8
+
+Explanation: The given triangle
+
+ 1
+ 2 4
+ 6 4 9
+ 5 1 7 2
+
+The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8
+
+ [1]
+ [2] 4
+ 6 [4] 9
+ 5 [1] 7 2
+Example 2:
+Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ]
+Output: 7
+
+Explanation: The given triangle
+
+ 3
+ 3 1
+ 5 2 3
+ 4 3 1 3
+
+The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7
+
+ [3]
+ 3 [1]
+ 5 [2] 3
+ 4 3 [1] 3
+*/
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <ctype.h>
+
+#define MIN(a,b) ((a)<(b)?(a):(b))
+
+// memory allocation
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+// triangle (stored as a square)
+typedef struct {
+ int rows;
+ int** data;
+} Triangle;
+
+Triangle* triangle_new(void) {
+ Triangle* triangle = check_mem(calloc(1, sizeof(Triangle)));
+ return triangle;
+}
+
+void triangle_free(Triangle* triangle) {
+ for (int i = 0; i < triangle->rows; i++)
+ free(triangle->data[i]);
+ free(triangle->data);
+ free(triangle);
+}
+
+void triangle_add_row(Triangle* triangle) {
+ triangle->rows++;
+ triangle->data = check_mem(realloc(triangle->data,
+ triangle->rows * sizeof(int*)));
+ triangle->data[triangle->rows - 1] = NULL;
+ for (int i = 0; i < triangle->rows; i++)
+ triangle->data[i] = check_mem(realloc(triangle->data[i],
+ triangle->rows * sizeof(int)));
+}
+
+bool triangle_parse_row(Triangle* triangle, int row, const char* text) {
+ assert(row >= 0 && row < triangle->rows);
+ for (int i = 0; i < triangle->rows; i++) {
+ while (*text && !isdigit(*text)) text++;
+ if (!isdigit(*text))
+ return false;
+ triangle->data[row][i] = atoi(text);
+ while (*text && isdigit(*text)) text++;
+ }
+ return true;
+}
+
+bool triangle_parse(Triangle* triangle, int argc, char* argv[]) {
+ for (int row = 0; row < argc; row++) {
+ triangle_add_row(triangle);
+ if (!triangle_parse_row(triangle, row,argv[row]))
+ return false;
+ }
+ return true;
+}
+
+int triangle_min_sum_1(Triangle* triangle, int sum, int row, int col) {
+ sum += triangle->data[row][col];
+ if (row + 1 == triangle->rows)
+ return sum;
+ else {
+ int sum1 = triangle_min_sum_1(triangle, sum, row + 1, col);
+ int sum2 = triangle_min_sum_1(triangle, sum, row + 1, col + 1);
+ return MIN(sum1, sum2);
+ }
+}
+
+int triangle_min_sum(Triangle* triangle) {
+ return triangle_min_sum_1(triangle, 0, 0, 0);
+}
+
+int main(int argc, char* argv[]) {
+ if (argc == 1) {
+ fputs("Usage: ch-2 row row ...", stderr);
+ return EXIT_FAILURE;
+ }
+ argc--; argv++;
+ Triangle* triangle = triangle_new();
+ if (!triangle_parse(triangle, argc, argv)) {
+ fputs("Malformed triangle", stderr);
+ return EXIT_FAILURE;
+ }
+ int sum = triangle_min_sum(triangle);
+ printf("%d\n", sum);
+ triangle_free(triangle);
+}
diff --git a/challenge-100/paulo-custodio/cpp/ch-1.cpp b/challenge-100/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..389d36f9c9
--- /dev/null
+++ b/challenge-100/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,111 @@
+/*
+TASK #1 > Fun Time
+Submitted by: Mohammad S Anwar
+You are given a time (12 hour / 24 hour).
+
+Write a script to convert the given time from 12 hour format to 24 hour format
+and vice versa.
+
+Ideally we expect a one-liner.
+
+Example 1:
+Input: 05:15 pm or 05:15pm
+Output: 17:15
+Example 2:
+Input: 19:15
+Output: 07:15 pm or 07:15pm
+*/
+
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <cctype>
+
+class Time {
+public:
+ Time(int hours = 0, int minutes = 0)
+ : m_hours(hours), m_minutes(minutes), m_is12(false) {}
+
+ int hours() const { return m_hours; }
+ int minutes() const { return m_minutes; }
+ bool is12() const { return m_is12; }
+ void set12() { m_is12 = true; }
+ void set24() { m_is12 = false; }
+
+ friend std::ostream& operator<<(std::ostream& os, const Time& time);
+ friend std::istream& operator>>(std::istream& is, Time& time);
+
+private:
+ int m_hours, m_minutes; // always in 24 hour format
+ bool m_is12;
+};
+
+std::ostream& operator<<(std::ostream& os, const Time& time) {
+ if (time.m_is12) {
+ int hours = time.m_hours;
+ bool ispm = false;
+ if (hours == 0)
+ hours = 12;
+ else if (hours == 12)
+ ispm = true;
+ else if (hours > 12) {
+ ispm = true;
+ hours -= 12;
+ }
+ os << std::setw(2) << std::setfill('0') << hours << ":"
+ << std::setw(2) << std::setfill('0') << time.m_minutes
+ << (ispm ? "pm" : "am");
+ }
+ else
+ os << std::setw(2) << std::setfill('0') << time.m_hours << ":"
+ << std::setw(2) << std::setfill('0') << time.m_minutes;
+ return os;
+}
+
+std::istream& operator>>(std::istream& is, Time& time) {
+ // get hh:mm
+ char colon = '\0';
+ if (!(is >> time.m_hours >> colon >> time.m_minutes) || colon != ':') {
+ std::cerr << "error parsing time" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ // get optional am|pm
+ std::string ampm;
+ is >> ampm;
+
+ // convert from 12 to 24 format
+ if (!ampm.empty()) {
+ switch (tolower(ampm[0])) {
+ case 'a':
+ if (time.m_hours == 12) time.m_hours = 0;
+ time.m_is12 = true;
+ break;
+ case 'p':
+ if (time.m_hours != 12) time.m_hours += 12;
+ time.m_is12 = true;
+ break;
+ default:
+ std::cerr << "error parsing time" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+ }
+ return is;
+}
+
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) {
+ fputs("Usage: ch-1 time", stderr);
+ return EXIT_FAILURE;
+ }
+
+ Time time;
+ std::stringstream ss(argv[1]);
+ ss >> time;
+ if (time.is12())
+ time.set24();
+ else
+ time.set12();
+ std::cout << time << std::endl;
+}
diff --git a/challenge-100/paulo-custodio/cpp/ch-2.cpp b/challenge-100/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..b672a5cffe
--- /dev/null
+++ b/challenge-100/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,119 @@
+/*
+TASK #2 > Triangle Sum
+Submitted by: Mohammad S Anwar
+You are given triangle array.
+
+Write a script to find the minimum path sum from top to bottom.
+
+When you are on index i on the current row then you may move to either
+index i or index i + 1 on the next row.
+
+Example 1:
+Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ]
+Output: 8
+
+Explanation: The given triangle
+
+ 1
+ 2 4
+ 6 4 9
+ 5 1 7 2
+
+The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8
+
+ [1]
+ [2] 4
+ 6 [4] 9
+ 5 [1] 7 2
+Example 2:
+Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ]
+Output: 7
+
+Explanation: The given triangle
+
+ 3
+ 3 1
+ 5 2 3
+ 4 3 1 3
+
+The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7
+
+ [3]
+ 3 [1]
+ 5 [2] 3
+ 4 3 [1] 3
+*/
+
+#include <iostream>
+#include <vector>
+#include <cassert>
+#include <cctype>
+
+#define MIN(a,b) ((a)<(b)?(a):(b))
+
+// triangle (stored as a square)
+class Triangle {
+public:
+ bool parse(int argc, char* argv[]) {
+ for (int row = 0; row < argc; row++) {
+ add_row();
+ if (!parse_row(row, argv[row]))
+ return false;
+ }
+ return true;
+ }
+
+ int min_sum() {
+ return min_sum_1(0, 0, 0);
+ }
+
+private:
+ std::vector<std::vector<int>> m_data;
+
+ void add_row() {
+ int rows = static_cast<int>(m_data.size()) + 1;
+ m_data.resize(rows);
+ for (int i = 0; i < rows; i++)
+ m_data[i].resize(rows);
+ }
+
+ bool parse_row(int row, const char* text) {
+ int rows = static_cast<int>(m_data.size());
+ assert(row >= 0 && row < rows);
+ for (int i = 0; i < rows; i++) {
+ while (*text && !isdigit(*text)) text++;
+ if (!isdigit(*text))
+ return false;
+ m_data[row][i] = atoi(text);
+ while (*text && isdigit(*text)) text++;
+ }
+ return true;
+ }
+
+ int min_sum_1(int sum, int row, int col) {
+ sum += m_data[row][col];
+ int rows = static_cast<int>(m_data.size());
+ if (row + 1 == rows)
+ return sum;
+ else {
+ int sum1 = min_sum_1(sum, row + 1, col);
+ int sum2 = min_sum_1(sum, row + 1, col + 1);
+ return MIN(sum1, sum2);
+ }
+ }
+};
+
+int main(int argc, char* argv[]) {
+ if (argc == 1) {
+ fputs("Usage: ch-2 row row ...", stderr);
+ return EXIT_FAILURE;
+ }
+ argc--; argv++;
+ Triangle triangle;
+ if (!triangle.parse(argc, argv)) {
+ fputs("Malformed triangle", stderr);
+ return EXIT_FAILURE;
+ }
+ int sum = triangle.min_sum();
+ std::cout << sum << std::endl;
+}
diff --git a/challenge-100/paulo-custodio/perl/ch-1.pl b/challenge-100/paulo-custodio/perl/ch-1.pl
index 88e5a10357..58ffb22647 100644
--- a/challenge-100/paulo-custodio/perl/ch-1.pl
+++ b/challenge-100/paulo-custodio/perl/ch-1.pl
@@ -4,7 +4,8 @@
# Submitted by: Mohammad S Anwar
# You are given a time (12 hour / 24 hour).
#
-# Write a script to convert the given time from 12 hour format to 24 hour format and vice versa.
+# Write a script to convert the given time from 12 hour format to 24 hour format
+# and vice versa.
#
# Ideally we expect a one-liner.
#
diff --git a/challenge-100/paulo-custodio/python/ch-1.py b/challenge-100/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..863b39f2ba
--- /dev/null
+++ b/challenge-100/paulo-custodio/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+# TASK #1 > Fun Time
+# Submitted by: Mohammad S Anwar
+# You are given a time (12 hour / 24 hour).
+#
+# Write a script to convert the given time from 12 hour format to 24 hour format
+# and vice versa.
+#
+# Ideally we expect a one-liner.
+#
+# Example 1:
+# Input: 05:15 pm or 05:15pm
+# Output: 17:15
+# Example 2:
+# Input: 19:15
+# Output: 07:15 pm or 07:15pm
+
+import re;
+import sys;
+import datetime;
+
+if re.search(r'am|pm', sys.argv[1], re.I):
+ t = datetime.datetime.strptime(sys.argv[1], "%I:%M%p")
+ print(t.strftime("%H:%M"))
+else:
+ t = datetime.datetime.strptime(sys.argv[1], "%H:%M")
+ print(t.strftime("%I:%M%p").lower())
diff --git a/challenge-100/paulo-custodio/python/ch-2.py b/challenge-100/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..d75c2540c9
--- /dev/null
+++ b/challenge-100/paulo-custodio/python/ch-2.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python
+
+# TASK #2 > Triangle Sum
+# Submitted by: Mohammad S Anwar
+# You are given triangle array.
+#
+# Write a script to find the minimum path sum from top to bottom.
+#
+# When you are on index i on the current row then you may move to either
+# index i or index i + 1 on the next row.
+#
+# Example 1:
+# Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ]
+# Output: 8
+#
+# Explanation: The given triangle
+#
+# 1
+# 2 4
+# 6 4 9
+# 5 1 7 2
+#
+# The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8
+#
+# [1]
+# [2] 4
+# 6 [4] 9
+# 5 [1] 7 2
+# Example 2:
+# Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ]
+# Output: 7
+#
+# Explanation: The given triangle
+#
+# 3
+# 3 1
+# 5 2 3
+# 4 3 1 3
+#
+# The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7
+#
+# [3]
+# 3 [1]
+# 5 [2] 3
+# 4 3 [1] 3
+
+import sys;
+
+triangle = []
+
+def add_row(row, items):
+ triangle.append(items)
+
+def parse(args):
+ for i in range(0, len(args)):
+ items = [int(x) for x in args[i].split(",")]
+ add_row(i, items)
+
+def min_sum():
+ def min_sum_1(sum, row, col):
+ sum += triangle[row][col]
+ if row+1 == len(triangle):
+ return sum
+ else:
+ sum1 = min_sum_1(sum, row+1, col)
+ sum2 = min_sum_1(sum, row+1, col+1)
+ return min(sum1, sum2)
+ return min_sum_1(0, 0, 0)
+
+parse(sys.argv[1:])
+print(min_sum())
+
+
+