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 | |
| 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
| -rw-r--r-- | challenge-013/cheok-yin-fung/README.md | 6 | ||||
| -rw-r--r-- | challenge-013/cheok-yin-fung/java/LastFriday.java | 31 | ||||
| -rw-r--r-- | challenge-013/cheok-yin-fung/perl/ch-1.pl | 27 | ||||
| -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 | ||||
| -rw-r--r-- | challenge-038/cheok-yin-fung/README.md | 6 | ||||
| -rw-r--r-- | challenge-038/cheok-yin-fung/java/DateFinder.java | 65 | ||||
| -rw-r--r-- | challenge-038/cheok-yin-fung/perl/ch-1.pl | 32 |
9 files changed, 230 insertions, 0 deletions
diff --git a/challenge-013/cheok-yin-fung/README.md b/challenge-013/cheok-yin-fung/README.md new file mode 100644 index 0000000000..743a767c7d --- /dev/null +++ b/challenge-013/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-013/cheok-yin-fung/java/LastFriday.java b/challenge-013/cheok-yin-fung/java/LastFriday.java new file mode 100644 index 0000000000..4ab9ba6355 --- /dev/null +++ b/challenge-013/cheok-yin-fung/java/LastFriday.java @@ -0,0 +1,31 @@ +// The Weekly Challenge - 013 +// Task 1 Last Friday +// attempt: May 09th, 2021 +// from: C.-Y. Fung +// Usage: java LastFriday [year] +import java.time.LocalDate; +import java.time.YearMonth; +import java.time.DayOfWeek; +public class LastFriday +{ + public static void main(String[] args) + { + int year = 2008; // default value + if (args.length != 0) + year = Integer.parseInt(args[0]); + if (year < 1900) { + System.out.println("Sorry, it's too long ago."); + System.exit(0); + } + System.out.println("Year: " + year + "\n"); + for (int month = 1; month <= 12; month++) { + YearMonth temp = YearMonth.of(year, month); + LocalDate target = + LocalDate.of(year , month , temp.lengthOfMonth()); + while ( target.getDayOfWeek() != DayOfWeek.FRIDAY ) { + target = target.minusDays(1); + } + System.out.println(target); + } + } +} diff --git a/challenge-013/cheok-yin-fung/perl/ch-1.pl b/challenge-013/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..d0b747ffb7 --- /dev/null +++ b/challenge-013/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +# The Weekly Challenge - 013 +# Task 1 Last Friday +# attempt: May 09th, 2021 +# from: C.-Y. Fung +# Usage: ch-1.pl [year] +# Java correspondence: LastFriday.java +use Time::Piece; +use Time::Seconds; +use strict; +use warnings; + +my $year = $ARGV[0] || 2008; +die "Sorry, it's too long ago." if $year < 1900; +print "Year: $year\n\n"; + +for my $month (1..12) { + my $first_day_of_nmonth = ($month != 12) ? + Time::Piece->strptime($year."-".($month+1)."-01", "%Y-%m-%d") + : Time::Piece->strptime(($year+1)."-01-01", "%Y-%m-%d"); + + my $target_day = ($first_day_of_nmonth -= ONE_DAY); + while ($target_day->day_of_week != 5) { + $target_day -= ONE_DAY; + } + print($target_day->strftime("%Y-%m-%d"), "\n"); +} 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); + } +} + diff --git a/challenge-038/cheok-yin-fung/README.md b/challenge-038/cheok-yin-fung/README.md new file mode 100644 index 0000000000..743a767c7d --- /dev/null +++ b/challenge-038/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-038/cheok-yin-fung/java/DateFinder.java b/challenge-038/cheok-yin-fung/java/DateFinder.java new file mode 100644 index 0000000000..5945b73f53 --- /dev/null +++ b/challenge-038/cheok-yin-fung/java/DateFinder.java @@ -0,0 +1,65 @@ +// The Weekly Challenge - 038 +// Task 1 Date Finder +// attempt: May 09th, 2021 +// from: C.-Y. Fung +// Usage: java DateFinder [7-digit string] + +import java.time.LocalDate; +import java.time.YearMonth; + +public class DateFinder +{ + public static void main(String[] args) + { + String myYear = "", in_str = ""; + try { + in_str = args[0]; + for (char numChar : in_str.toCharArray()) { + if (numChar < '0' || numChar> '9') { + System.out.println("Non-numeric character found."); + throw new IllegalArgumentException(); + } + } + switch(in_str.charAt(0)) { + case '1': myYear = "20"+in_str.substring(1,3); break; + case '2': myYear = "19"+in_str.substring(1,3); break; + default: throw new IllegalArgumentException(); + } + if (in_str.length() != 7) { + System.out.println("Input digits is not equal to 7."); + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println("Ilegal argument."); + System.exit(0); + } catch (IndexOutOfBoundsException e) { + System.out.println("Usage: java DateFinder [7-digit string]"); + System.exit(0); + } + + String myMonth = in_str.substring(3,5); + int myMonth_i = Integer.parseInt(myMonth); + try { + if (myMonth_i > 12 || myMonth_i < 1) + throw new IllegalArgumentException(); + } catch (IllegalArgumentException e) { + System.out.println("Problem in Month (within 01-12) " + myMonth); + System.exit(0); + } + String myDay = in_str.substring(5,7); + int myDay_i = Integer.parseInt(myDay); + int myYear_i = Integer.parseInt(myYear); + try { + YearMonth temp = YearMonth.of(myYear_i, myMonth_i); + int daysInMonth = temp.lengthOfMonth(); + if (myDay_i > daysInMonth || myDay_i < 1) + throw new IllegalArgumentException(); + } catch (IllegalArgumentException e) { + System.out.println("Problem in date (last two digits) " + myDay); + System.exit(0); + } finally { + System.out.println(myYear + "-" + myMonth + "-" + myDay); + } + } + +} diff --git a/challenge-038/cheok-yin-fung/perl/ch-1.pl b/challenge-038/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..88f5e781f0 --- /dev/null +++ b/challenge-038/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +# The Weekly Challenge - 038 +# Task 1 Date Finder +# attempt: May 09th, 2021 +# from: C.-Y. Fung +# Usage: ch-1.pl [7-digit string] +# Java correspondence: DateFinder.java +use Time::Piece; +use strict; +use warnings; + +my $str = $ARGV[0] || die "A 7-digit string is expected."; +die "Format Error: A 7-digit string is expected." if length $str != 7; + +$str =~ s/^1/20/; +$str =~ s/^2/19/; +die "Error: first digit must be 1 or 2! " if $str =~ /[^12]/; + +my $Y = substr($str, 0, 4); +my $Month = substr($str, 4, 2); +my $d = substr($str, 6, 2); + +die "Problem in month ($Month)! " + if $Month > 12 || $Month < 1; +my $check = Time::Piece->strptime(substr($str,0,6)."01" , "%Y%m%d"); +die "Problem in date (last two digits: $d)! " + if $d > $check->month_last_day || $d < 1; + +my $t = Time::Piece->strptime($str, "%Y%m%d"); + + +print $t->strftime("%Y-%m-%d"), "\n"; |
