diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-26 18:18:35 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-26 18:18:35 +0100 |
| commit | 2fe7c8e127dfebfa29c4670dc1cf5d17adab38fd (patch) | |
| tree | c64b211982e586607dfb0d9c9829bd1a32ce9398 /challenge-175 | |
| parent | 307796cabb6ed674c38f634fd7d786b6a1acbfc4 (diff) | |
| download | perlweeklychallenge-club-2fe7c8e127dfebfa29c4670dc1cf5d17adab38fd.tar.gz perlweeklychallenge-club-2fe7c8e127dfebfa29c4670dc1cf5d17adab38fd.tar.bz2 perlweeklychallenge-club-2fe7c8e127dfebfa29c4670dc1cf5d17adab38fd.zip | |
- Added Raku solution to the task "Last Sunday".
Diffstat (limited to 'challenge-175')
| -rw-r--r-- | challenge-175/mohammad-anwar/raku/ch-1.raku | 45 | ||||
| -rw-r--r-- | challenge-175/mohammad-anwar/swift/ch-1.swift | 86 |
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-175/mohammad-anwar/raku/ch-1.raku b/challenge-175/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..2b95da30db --- /dev/null +++ b/challenge-175/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,45 @@ +#!/usr/bin/env raku + +=begin pod + +Week 175: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-175 + +Task #1: Last Sunday + + Write a script to list Last Sunday of every month in the given year. + +=end pod + +use Test; + +my $got = all-last-sunday-of(2022); +my $exp = [ + '2022-01-30', '2022-02-27', '2022-03-27', '2022-04-24', + '2022-05-29', '2022-06-26', '2022-07-31', '2022-08-28', + '2022-09-25', '2022-10-30', '2022-11-27', '2022-12-25', +]; + +is $exp, $got; + +done-testing; + +# +# +# METHODS + +sub all-last-sunday-of(Int $year where $year >= 1900) { + my $ls = []; + $ls.push: last-sunday-of($_, $year) for (1..12); + return $ls; +} + +sub last-sunday-of(Int $month where $month >= 1 && $month <= 12, + Int $year where $year >= 1900 --> Date) { + my $dt = Date.new($year, $month, 1).last-date-in-month; + my $dow = $dt.day-of-week; + $dt -= $dow if $dow != 7; + + return $dt; +} diff --git a/challenge-175/mohammad-anwar/swift/ch-1.swift b/challenge-175/mohammad-anwar/swift/ch-1.swift new file mode 100644 index 0000000000..f81cbcc619 --- /dev/null +++ b/challenge-175/mohammad-anwar/swift/ch-1.swift @@ -0,0 +1,86 @@ +import Foundation + +/* + +Week 175: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-175 + +Task #1: Last Sunday + + Write a script to list Last Sunday of every month in the given year. + +*/ + +enum ParamError: Error { + case missingYear + case invalidYear +} + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingYear + } + + let year:Int = Int(CommandLine.arguments[1])! + if year >= 1900 { + for month in 1...12 { + print(lastSundayOf(month, 2022)) + } + } + else { + throw ParamError.invalidYear + } +} +catch ParamError.missingYear { + print("Missing year.") +} +catch ParamError.invalidYear { + print("Invalid year.") +} +catch let error { + print(error) +} + +// +// +// Functions + +func lastSundayOf(_ month:Int, _ year:Int) -> Date { + var dcomp = DateComponents() + dcomp.year = year + dcomp.month = month + dcomp.day = 1 + + let date = Calendar.current.date(from: dcomp) ?? Date() + let nm = addMonths(date, 1) + let pd = addDays(nm, -1) + let dow = dayOfWeek(pd) + + if dow == 7 { + return pd + } + + return addDays(pd, -dow) +} + +func dayOfWeek(_ d:Date) -> Int { + let index = Calendar(identifier: .gregorian).component(.weekday, from: d) + return index - 1 +} + +func addMonths(_ date:Date, _ m:Int) -> Date { + var dc = DateComponents() + dc.month = m + + return Calendar.current.date(byAdding: dc, to:date) ?? Date() +} + +func addDays(_ date:Date, _ d:Int) -> Date { + var dc = DateComponents() + dc.day = d + + return Calendar.current.date(byAdding: dc, to:date) ?? Date() +} |
