diff options
| author | Roger Bell_West <roger@firedrake.org> | 2023-10-03 12:39:33 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2023-10-03 12:39:33 +0100 |
| commit | b2b1f26f1b5c68b7e374a6372ba45fd319df0dc2 (patch) | |
| tree | ed32448261772dcbd253ff633d9cfb0aff0ea562 /challenge-237 | |
| parent | 348adb792a21a38fdafbd4977e18ca4b29881ae5 (diff) | |
| download | perlweeklychallenge-club-b2b1f26f1b5c68b7e374a6372ba45fd319df0dc2.tar.gz perlweeklychallenge-club-b2b1f26f1b5c68b7e374a6372ba45fd319df0dc2.tar.bz2 perlweeklychallenge-club-b2b1f26f1b5c68b7e374a6372ba45fd319df0dc2.zip | |
RogerBW solutions for challenge no. 237
Diffstat (limited to 'challenge-237')
21 files changed, 893 insertions, 0 deletions
diff --git a/challenge-237/roger-bell-west/javascript/ch-1.js b/challenge-237/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..6fca0cc0ba --- /dev/null +++ b/challenge-237/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,40 @@ +#! /usr/bin/node + +"use strict" + +function seizetheday(year, month, weekcount, dayofweek) { + let dt = new Date(year, month-1, 1, 12, 0, 0); + let wd = dt.getDay(); + if (wd == 0) { + wd = 7; + } + if (wd != dayofweek) { + dt.setDate(dt.getDate() + (dayofweek - wd + 7) % 7); + } + if (weekcount > 1) { + dt.setDate(dt.getDate() + 7 * (weekcount - 1)); + } + if (dt.getMonth() != month-1 || dt.getYear() != year-1900) { + return 0; + } + return dt.getDate(); +} + +if (seizetheday(2024, 4, 3, 2) == 16) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (seizetheday(2025, 10, 2, 4) == 9) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (seizetheday(2026, 8, 5, 3) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-237/roger-bell-west/javascript/ch-2.js b/challenge-237/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..a52e4c717b --- /dev/null +++ b/challenge-237/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,30 @@ +#! /usr/bin/node + +"use strict" + +function maximisegreatness(a) { + let b = a; + b.sort(function(a, b) { + return a - b; + }); + let g = 0; + for (let c of b) { + if (c > b[g]) { + g++; + } + } + return g; +} + +if (maximisegreatness([1, 3, 5, 2, 1, 3, 1]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximisegreatness([1, 2, 3, 4]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-237/roger-bell-west/kotlin/ch-1.kt b/challenge-237/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..3b64e67cff --- /dev/null +++ b/challenge-237/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,43 @@ +import java.time.LocalDate +import java.time.temporal.ChronoUnit + +fun seizetheday(year: Int, month: Int, weekcount: Int, dayofweek: Int): Int { + var dt = LocalDate.of(year, month, 1) + var wd = dt.getDayOfWeek().getValue() + if (wd == 0) { + wd = 7 + } + if (wd != dayofweek) { + dt = dt.plusDays(((dayofweek - wd + 7) % 7).toLong()) + } + if (weekcount > 1) { + dt = dt.plusWeeks((weekcount - 1).toLong()) + } + if (dt.getMonthValue() != month || dt.getYear() != year) { + return 0 + } + return dt.getDayOfMonth() +} + +fun main() { + + if (seizetheday(2024, 4, 3, 2) == 16) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (seizetheday(2025, 10, 2, 4) == 9) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (seizetheday(2026, 8, 5, 3) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-237/roger-bell-west/kotlin/ch-2.kt b/challenge-237/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..d58c523adf --- /dev/null +++ b/challenge-237/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,27 @@ +fun maximisegreatness(a: List<Int>): Int { + val b = a.sorted() + var g = 0 + for (c in b) { + if (c > b[g]) { + g += 1 + } + } + return g +} + +fun main() { + + if (maximisegreatness(listOf(1, 3, 5, 2, 1, 3, 1)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximisegreatness(listOf(1, 2, 3, 4)) == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-237/roger-bell-west/lua/ch-1.lua b/challenge-237/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..6de67ba37d --- /dev/null +++ b/challenge-237/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,82 @@ +#! /usr/bin/lua + +function idiv(a0, b) + local a = math.abs(a0) + local q = a // b + if a0 < 0 then + q = -q + end + return q +end +function ymd2jd(y, m, d) + local mn = idiv(m - 14, 12) + return idiv((y + 4800 + mn) * 1461, 4) + idiv((m - 2 - 12 * mn) * 367, 12) - idiv(idiv(y + 4900 + mn, 100) * 3, 4) + d - 32075 +end + +function jd2dow(jd) + return (jd + 1) % 7 +end + +function jd2ymd(jd) + local y = 4716 + local v = 3 + local j = 1401 + local u = 5 + local m = 2 + local s = 153 + local n = 12 + local w = 2 + local r = 4 + local B = 274277 + local p = 1461 + local C = -38 + local f = jd + j + idiv((idiv(4 * jd + B, 146097)*3), 4) + C + local e = r * f + v + local g = idiv(e % p, r) + local h = u * g + w + local D = idiv(h % s, u) + 1 + local M = (idiv(h, s) + m) % n + 1 + local Y = idiv(e, p) - y + idiv(n + m - M, n) + return {Y, M, D} +end + +function seizetheday(year, month, weekcount, dayofweek) + local dt = ymd2jd(year, month, 1) + local wd = jd2dow(dt) + if wd == 0 then + wd = 7 + end + if wd ~= dayofweek then + dt = dt + (dayofweek - wd + 7) % 7 + end + if weekcount > 1 then + dt = dt + 7 * (weekcount - 1) + end + nd = jd2ymd(dt) + if nd[1] ~= year or nd[2] ~= month then + return 0 + end + return nd[3] +end + +if seizetheday(2024, 4, 3, 2) == 16 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if seizetheday(2025, 10, 2, 4) == 9 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if seizetheday(2026, 8, 5, 3) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-237/roger-bell-west/lua/ch-2.lua b/challenge-237/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..e67594f5d1 --- /dev/null +++ b/challenge-237/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,28 @@ +#! /usr/bin/lua + +function maximisegreatness(a) + local b = a + table.sort(b) + local g = 0 + for _, c in ipairs(b) do + if c > b[g + 1] then + g = g + 1 + end + end + return g +end + +if maximisegreatness({1, 3, 5, 2, 1, 3, 1}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximisegreatness({1, 2, 3, 4}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-237/roger-bell-west/perl/ch-1.pl b/challenge-237/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..37bd3940fa --- /dev/null +++ b/challenge-237/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,33 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(seizetheday(2024, 4, 3, 2), 16, 'example 1'); +is(seizetheday(2025, 10, 2, 4), 9, 'example 2'); +is(seizetheday(2026, 8, 5, 3), 0, 'example 3'); + +use DateTime; + +sub seizetheday($year, $month, $weekcount, $dayofweek) { + my $dt = DateTime->new(year => $year, + month => $month, + day => 1, + hour => 12, + minute => 0, + second => 0); + my $wd = $dt->day_of_week(); + if ($wd != $dayofweek) { + $dt = $dt->add(days => ($dayofweek - $wd + 7) % 7); + } + if ($weekcount > 1) { + $dt = $dt->add(days => 7 * ($weekcount - 1)); + } + if ($dt->month != $month || $dt->year != $year) { + return 0; + } + return $dt->day; +} diff --git a/challenge-237/roger-bell-west/perl/ch-2.pl b/challenge-237/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..6c2d2b8a9e --- /dev/null +++ b/challenge-237/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(maximisegreatness([1, 3, 5, 2, 1, 3, 1]), 4, 'example 1'); +is(maximisegreatness([1, 2, 3, 4]), 3, 'example 2'); + +sub maximisegreatness($a) { + my @b = sort {$::a <=> $::b} @{$a}; + my $g = 0; + foreach my $c (@b) { + if ($c > $b[$g]) { + $g++; + } + } + return $g; +} diff --git a/challenge-237/roger-bell-west/postscript/ch-1.ps b/challenge-237/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..9fad6a90b0 --- /dev/null +++ b/challenge-237/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,116 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/jd2dow { + 1 add 7 mod +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/ymd2jd { + 4 dict begin + aload pop + /d exch def + /m exch def + /y exch def + /mn m 14 sub 12 idiv def + y 4800 add mn add 1461 mul 4 idiv + mn 12 mul neg 2 sub m add 367 mul 12 idiv add + y 4900 add mn add 100 idiv 3 mul 4 idiv sub + d add + 32075 sub + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/jd2ymd { + 15 dict begin + /y 4716 def + /v 3 def + /j 1401 def + /u 5 def + /m 2 def + /s 153 def + /n 12 def + /w 2 def + /r 4 def + /B 274277 def + /p 1461 def + /C -38 def + dup + 4 mul B add 146097 idiv 3 mul 4 idiv C add j add add /f exch def + r f mul v add /e exch def + e p mod r idiv u mul w add /h exch def + /day h s mod u idiv 1 add def + /month h s idiv m add n mod 1 add def + /year e p idiv y sub n m add month sub n idiv add def + [ year month day ] + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/seizetheday { + 0 dict begin + /dayofweek exch def + /weekcount exch def + /month exch def + /year exch def + /dt [ year month 1 ] ymd2jd def + /wd dt jd2dow def + wd 0 eq { + /wd 7 def + } if + wd dayofweek ne { + /dt dayofweek 7 add wd sub 7 mod dt add def + } if + weekcount 1 gt { + /dt dt weekcount 1 sub 7 mul add def + } if + dt jd2ymd aload pop + exch + month ne { + pop 0 + } if + exch + year ne { + pop 0 + } if + end +} bind def + +(seizetheday) test.start +2024 4 3 2 seizetheday 16 eq test +2025 10 2 4 seizetheday 9 eq test +2026 8 5 3 seizetheday 0 eq test +test.end diff --git a/challenge-237/roger-bell-west/postscript/ch-2.ps b/challenge-237/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..d8bd66d6ff --- /dev/null +++ b/challenge-237/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,143 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/maximisegreatness { + 0 dict begin + quicksort /b exch def + /g 0 def + b { + b g get gt { + /g g 1 add def + } if + } forall + g + end +} bind def + +(maximisegreatness) test.start +[1 3 5 2 1 3 1] maximisegreatness 4 eq test +[1 2 3 4] maximisegreatness 3 eq test +test.end diff --git a/challenge-237/roger-bell-west/python/ch-1.py b/challenge-237/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..a6470bf1c5 --- /dev/null +++ b/challenge-237/roger-bell-west/python/ch-1.py @@ -0,0 +1,29 @@ +#! /usr/bin/python3 + +from datetime import date, timedelta + +def seizetheday(year, month, weekcount, dayofweek): + dt = date(year, month, 1) + wd = dt.weekday() + 1 + if wd != dayofweek: + dt += timedelta(days = (dayofweek - wd + 7) % 7) + if weekcount > 1: + dt += timedelta(weeks = (weekcount - 1)) + if dt.month != month or dt.year != year: + return 0 + return dt.day + +import unittest + +class TestSeizetheday(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(seizetheday(2024, 4, 3, 2), 16, 'example 1') + + def test_ex2(self): + self.assertEqual(seizetheday(2025, 10, 2, 4), 9, 'example 2') + + def test_ex3(self): + self.assertEqual(seizetheday(2026, 8, 5, 3), 0, 'example 3') + +unittest.main() diff --git a/challenge-237/roger-bell-west/python/ch-2.py b/challenge-237/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..c220bcea0d --- /dev/null +++ b/challenge-237/roger-bell-west/python/ch-2.py @@ -0,0 +1,22 @@ +#! /usr/bin/python3 + +def maximisegreatness(a): + b = a + b.sort() + g = 0 + for c in b: + if c > b[g]: + g += 1 + return g + +import unittest + +class TestMaximisegreatness(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maximisegreatness([1, 3, 5, 2, 1, 3, 1]), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(maximisegreatness([1, 2, 3, 4]), 3, 'example 2') + +unittest.main() diff --git a/challenge-237/roger-bell-west/raku/ch-1.p6 b/challenge-237/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..a78e65e341 --- /dev/null +++ b/challenge-237/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(seizetheday(2024, 4, 3, 2), 16, 'example 1'); +is(seizetheday(2025, 10, 2, 4), 9, 'example 2'); +is(seizetheday(2026, 8, 5, 3), 0, 'example 3'); + +sub seizetheday($year, $month, $weekcount, $dayofweek) { + my $dt = Date.new(year => $year, + month => $month, + day => 1); + my $wd = $dt.day-of-week; + if ($wd != $dayofweek) { + $dt = $dt.later(days => ($dayofweek - $wd + 7) % 7); + } + if ($weekcount > 1) { + $dt = $dt.later(weeks => ($weekcount - 1)); + } + if ($dt.month != $month || $dt.year != $year) { + return 0; + } + return $dt.day; +} diff --git a/challenge-237/roger-bell-west/raku/ch-2.p6 b/challenge-237/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..5d51ea2c8e --- /dev/null +++ b/challenge-237/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(maximisegreatness([1, 3, 5, 2, 1, 3, 1]), 4, 'example 1'); +is(maximisegreatness([1, 2, 3, 4]), 3, 'example 2'); + +sub maximisegreatness(@a) { + my @b = @a.sort({$^a <=> $^b}); + my $g = 0; + for @b -> $c { + if ($c > @b[$g]) { + $g++; + } + } + return $g; +} diff --git a/challenge-237/roger-bell-west/ruby/ch-1.rb b/challenge-237/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..3192f82479 --- /dev/null +++ b/challenge-237/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,39 @@ +#! /usr/bin/ruby + +require 'date' + +def seizetheday(year, month, weekcount, dayofweek) + dt = Date.new(year, month, 1) + wd = dt.wday + if wd == 0 then + wd = 7 + end + if wd != dayofweek then + dt += (dayofweek - wd + 7) % 7 + end + if weekcount > 1 then + dt += 7 * (weekcount - 1) + end + if dt.month != month || dt.year != year then + return 0 + end + return dt.day +end + +require 'test/unit' + +class TestSeizetheday < Test::Unit::TestCase + + def test_ex1 + assert_equal(16, seizetheday(2024, 4, 3, 2)) + end + + def test_ex2 + assert_equal(9, seizetheday(2025, 10, 2, 4)) + end + + def test_ex3 + assert_equal(0, seizetheday(2026, 8, 5, 3)) + end + +end diff --git a/challenge-237/roger-bell-west/ruby/ch-2.rb b/challenge-237/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..f180310ad3 --- /dev/null +++ b/challenge-237/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,26 @@ +#! /usr/bin/ruby + +def maximisegreatness(a) + b = a.sort + g = 0 + b.each do |c| + if c > b[g] then + g += 1 + end + end + return g +end + +require 'test/unit' + +class TestMaximisegreatness < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, maximisegreatness([1, 3, 5, 2, 1, 3, 1])) + end + + def test_ex2 + assert_equal(3, maximisegreatness([1, 2, 3, 4])) + end + +end diff --git a/challenge-237/roger-bell-west/rust/ch-1.rs b/challenge-237/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..b6a9c5b8f1 --- /dev/null +++ b/challenge-237/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,31 @@ +// [dependencies] +// chrono = "0.4.19" + +use chrono::{Datelike, Weekday}; + +#[test] +fn test_ex1() { + assert_eq!(seizetheday(2024, 4, 3, 2), 16); +} + +#[test] +fn test_ex2() { + assert_eq!(seizetheday(2025, 10, 2, 4), 9); +} + +#[test] +fn test_ex3() { + assert_eq!(seizetheday(2026, 8, 5, 3), 0); +} + +fn seizetheday(year: i32, month: u32, weekcount: u8, dayofweek: u8) -> u32 { + match chrono::NaiveDate::from_weekday_of_month_opt( + year, + month, + Weekday::try_from(dayofweek - 1).unwrap(), + weekcount, + ) { + None => 0, + Some(x) => x.day(), + } +} diff --git a/challenge-237/roger-bell-west/rust/ch-2.rs b/challenge-237/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..9a1b306493 --- /dev/null +++ b/challenge-237/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,24 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(maximisegreatness(vec![1, 3, 5, 2, 1, 3, 1]), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(maximisegreatness(vec![1, 2, 3, 4]), 3); +} + +fn maximisegreatness(a: Vec<u32>) -> u32 { + let mut b = a; + b.sort(); + let mut g = 0; + for c in &b { + if c > &b[g] { + g += 1; + } + } + g as u32 +} diff --git a/challenge-237/roger-bell-west/scala/ch-1.scala b/challenge-237/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..0b97987762 --- /dev/null +++ b/challenge-237/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,44 @@ +import java.time.LocalDate +import java.time.temporal.ChronoUnit + +object Seizetheday { + def seizetheday(year: Int, month: Int, weekcount: Int, dayofweek: Int): Int = { + var dt = LocalDate.of(year, month, 1) + var wd = dt.getDayOfWeek().getValue() + if (wd == 0) { + wd = 7 + } + if (wd != dayofweek) { + dt = dt.plusDays((dayofweek - wd + 7) % 7) + } + if (weekcount > 1) { + dt = dt.plusWeeks(weekcount - 1) + } + if (dt.getMonthValue() != month || dt.getYear() != year) { + return 0 + } + return dt.getDayOfMonth() + } + + def main(args: Array[String]) { + if (seizetheday(2024, 4, 3, 2) == 16) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (seizetheday(2025, 10, 2, 4) == 9) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (seizetheday(2026, 8, 5, 3) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-237/roger-bell-west/scala/ch-2.scala b/challenge-237/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..5db3999209 --- /dev/null +++ b/challenge-237/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,29 @@ + +object Maximisegreatness { + def maximisegreatness(a: List[Int]): Int = { + val b = a.sortWith(_ < _) + var g = 0 + for (c <- b) { + if (c > b(g)) { + g += 1 + } + } + return g + } + + def main(args: Array[String]) { + if (maximisegreatness(List(1, 3, 5, 2, 1, 3, 1)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximisegreatness(List(1, 2, 3, 4)) == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-237/roger-bell-west/tests.yaml b/challenge-237/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..822c51f3ae --- /dev/null +++ b/challenge-237/roger-bell-west/tests.yaml @@ -0,0 +1,41 @@ +--- +ch-1: + - function: seizetheday + multiarg: true + arguments: + - 2024 + - 4 + - 3 + - 2 + result: 16 + - multiarg: true + arguments: + - 2025 + - 10 + - 2 + - 4 + result: 9 + - multiarg: true + arguments: + - 2026 + - 8 + - 5 + - 3 + result: 0 +ch-2: + - function: maximisegreatness + arguments: + - 1 + - 3 + - 5 + - 2 + - 1 + - 3 + - 1 + result: 4 + - arguments: + - 1 + - 2 + - 3 + - 4 + result: 3 |
