diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-09 10:35:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-09 10:35:19 +0100 |
| commit | 03b1587632c5f61d0fc5684aa4448d5c253939d2 (patch) | |
| tree | f1096fda87ae58e4315d11376f873100eef30d99 /challenge-019 | |
| parent | e32de6c08d7fa5c32ed7b6c9ea74992141f855bd (diff) | |
| parent | 7a0fc047ae3e6742da844b472e6acf2e88d20581 (diff) | |
| download | perlweeklychallenge-club-03b1587632c5f61d0fc5684aa4448d5c253939d2.tar.gz perlweeklychallenge-club-03b1587632c5f61d0fc5684aa4448d5c253939d2.tar.bz2 perlweeklychallenge-club-03b1587632c5f61d0fc5684aa4448d5c253939d2.zip | |
Merge pull request #4032 from E7-87-83/newt
Practice date and time stuff
Diffstat (limited to 'challenge-019')
| -rw-r--r-- | challenge-019/cheok-yin-fung/README.md | 6 | ||||
| -rw-r--r-- | challenge-019/cheok-yin-fung/java/FiveWeekends.java | 22 | ||||
| -rw-r--r-- | challenge-019/cheok-yin-fung/perl/ch-1.pl | 35 |
3 files changed, 63 insertions, 0 deletions
diff --git a/challenge-019/cheok-yin-fung/README.md b/challenge-019/cheok-yin-fung/README.md new file mode 100644 index 0000000000..743a767c7d --- /dev/null +++ b/challenge-019/cheok-yin-fung/README.md @@ -0,0 +1,6 @@ +Goals: + +- Java: Get familiar with the Date and Time API, +- Perl: Get familiar with Time::Piece (and Time::Seconds) module, especially the strftime and strptime methods + +attempt on 9th May, 2021 diff --git a/challenge-019/cheok-yin-fung/java/FiveWeekends.java b/challenge-019/cheok-yin-fung/java/FiveWeekends.java new file mode 100644 index 0000000000..bea36cd458 --- /dev/null +++ b/challenge-019/cheok-yin-fung/java/FiveWeekends.java @@ -0,0 +1,22 @@ +// The Weekly Challenge - 019 +// Task 1 Five Weekends +// attempt: May 09th, 2021 +// from: C.-Y. Fung +// Usage: java FiveWeekends [year] +import java.time.LocalDate; +import java.time.DayOfWeek; +import java.time.Month; + +public class FiveWeekends +{ + public static void main(String[] args) + { + int[] months = new int[] {1, 3, 5, 7, 8, 10, 12}; + + for (int i = 1900; i <= 2019; i++) + for (int M: months) + if (LocalDate.of(i,M,1).getDayOfWeek() == DayOfWeek.FRIDAY) + System.out.println(i+" "+ Month.of(M)); + + } +} diff --git a/challenge-019/cheok-yin-fung/perl/ch-1.pl b/challenge-019/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..2dd7290a3a --- /dev/null +++ b/challenge-019/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +# The Weekly Challenge - 019 +# Task 1 Five Weekends +# Task Statement: +# Write a script to display months +# from the year 1900 to 2019 where you find 5 weekends +# i.e. 5 Friday, 5 Saturday and 5 Sunday. +# attempt: May 09th, 2021 +# from: C.-Y. Fung +# Java correspondence: FiveWeekends.java +use Time::Piece; +use strict; +use warnings; + +my @months = (1, 3, 5, 7, 8, 10, 12); + +sub isFridayStart { + my $year = $_[0]; + my $month = $_[1]; + return + Time::Piece->strptime($year."-".$month."-01", "%Y-%m-%d") + ->day_of_week == 5; +} + +for my $i (1900..2019) { + for my $j (@months) { + if (isFridayStart($i, $j)) { + print Time::Piece->strptime($i."-".$j."-01", "%Y-%m-%d") + ->strftime("%Y %B"), + "\n"; + } + # printf("%4d %2d\n", $i, $j) if isFridayStart($i, $j); + } +} + |
