aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-04-01 23:29:33 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2023-04-01 23:29:33 +0100
commit634867957bd351d613e82e4cc75beb409c1fd96a (patch)
treed2b36a9edc3d689c2704ef18b71211ff8e53efa7
parent0834b3095c35d37388c439897edad20ec166b34f (diff)
downloadperlweeklychallenge-club-634867957bd351d613e82e4cc75beb409c1fd96a.tar.gz
perlweeklychallenge-club-634867957bd351d613e82e4cc75beb409c1fd96a.tar.bz2
perlweeklychallenge-club-634867957bd351d613e82e4cc75beb409c1fd96a.zip
Add C solution
-rw-r--r--challenge-048/paulo-custodio/c/ch-2.c57
-rw-r--r--challenge-048/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-048/paulo-custodio/t/test-2.yaml24
3 files changed, 82 insertions, 1 deletions
diff --git a/challenge-048/paulo-custodio/c/ch-2.c b/challenge-048/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..32ed45df01
--- /dev/null
+++ b/challenge-048/paulo-custodio/c/ch-2.c
@@ -0,0 +1,57 @@
+/*
+Challenge 048
+
+TASK #2
+Palindrome Dates
+Write a script to print all Palindrome Dates between 2000 and 2999. The format
+of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is
+represented as 10022001.
+*/
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+bool is_leap_year(int y) {
+ return y % 400 == 0 ? true : y % 100 == 0 ? false : y % 4 == 0 ? true : false;
+}
+
+int days_of_month(int y, int m) {
+ switch (m) {
+ case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
+ case 4: case 6: case 9: case 11: return 30;
+ case 2: return is_leap_year(y) ? 29 : 28;
+ default: assert(0); return -1;
+ }
+}
+
+bool is_palindrome(const char* str) {
+ int i = 0, j = strlen(str)-1;
+ while (i < strlen(str) && j >= 0) {
+ if (str[i] != str[j])
+ return false;
+ i++; j--;
+ }
+ return true;
+}
+
+void print_palindrome_dates(int start_year, int end_year) {
+ char date[BUFSIZ];
+
+ for (int y = start_year; y <= end_year; y++) {
+ for (int m = 1; m <= 12; m++) {
+ int eom = days_of_month(y, m);
+ for (int d = 1; d <= eom; d++) {
+ sprintf(date, "%02d%02d%04d", m, d, y);
+ if (is_palindrome(date))
+ printf("%s\n", date);
+ }
+ }
+ }
+}
+
+int main() {
+ print_palindrome_dates(2000, 2999);
+}
diff --git a/challenge-048/paulo-custodio/perl/ch-2.pl b/challenge-048/paulo-custodio/perl/ch-2.pl
index 27e9fe5bc8..be0dfde09e 100644
--- a/challenge-048/paulo-custodio/perl/ch-2.pl
+++ b/challenge-048/paulo-custodio/perl/ch-2.pl
@@ -12,7 +12,7 @@ use Modern::Perl;
use DateTime;
my $dt = DateTime->new(year=>2000, month=>1, day=>1);
-while ($dt->year < 2100) {
+while ($dt->year <= 2999) {
my $txt = $dt->strftime("%m%d%Y");
say $txt if reverse($txt) eq $txt; # is palindrome
$dt->add(days=>1);
diff --git a/challenge-048/paulo-custodio/t/test-2.yaml b/challenge-048/paulo-custodio/t/test-2.yaml
index fdb31f1c58..01f6c61b09 100644
--- a/challenge-048/paulo-custodio/t/test-2.yaml
+++ b/challenge-048/paulo-custodio/t/test-2.yaml
@@ -15,3 +15,27 @@
|07022070
|08022080
|09022090
+ |10122101
+ |01122110
+ |11122111
+ |02122120
+ |12122121
+ |03122130
+ |04122140
+ |05122150
+ |06122160
+ |07122170
+ |08122180
+ |09122190
+ |10222201
+ |01222210
+ |11222211
+ |02222220
+ |12222221
+ |03222230
+ |04222240
+ |05222250
+ |06222260
+ |07222270
+ |08222280
+ |09222290