From 26432e1851dfcd8faf8b2b3f5b2d48ee5460302b Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 18 Nov 2025 12:47:57 +0000 Subject: [PATCH] RogerBW solutions for challenge no. 348 --- challenge-348/roger-bell-west/crystal/ch-1.cr | 39 +++++++++ challenge-348/roger-bell-west/crystal/ch-2.cr | 40 ++++++++++ .../roger-bell-west/javascript/ch-1.js | 53 +++++++++++++ .../roger-bell-west/javascript/ch-2.js | 54 +++++++++++++ challenge-348/roger-bell-west/kotlin/ch-1.kt | 60 ++++++++++++++ challenge-348/roger-bell-west/kotlin/ch-2.kt | 55 +++++++++++++ challenge-348/roger-bell-west/lua/ch-1.lua | 75 ++++++++++++++++++ challenge-348/roger-bell-west/lua/ch-2.lua | 57 +++++++++++++ challenge-348/roger-bell-west/perl/ch-1.pl | 33 ++++++++ challenge-348/roger-bell-west/perl/ch-2.pl | 33 ++++++++ .../roger-bell-west/postscript/ch-1.ps | Bin 0 -> 1818 bytes .../roger-bell-west/postscript/ch-2.ps | Bin 0 -> 1851 bytes challenge-348/roger-bell-west/python/ch-1.py | 39 +++++++++ challenge-348/roger-bell-west/python/ch-2.py | 38 +++++++++ challenge-348/roger-bell-west/raku/ch-1.p6 | 30 +++++++ challenge-348/roger-bell-west/raku/ch-2.p6 | 31 ++++++++ challenge-348/roger-bell-west/ruby/ch-1.rb | 46 +++++++++++ challenge-348/roger-bell-west/ruby/ch-2.rb | 47 +++++++++++ challenge-348/roger-bell-west/rust/ch-1.rs | 49 ++++++++++++ challenge-348/roger-bell-west/rust/ch-2.rs | 47 +++++++++++ .../roger-bell-west/scala/ch-1.scala | 63 +++++++++++++++ .../roger-bell-west/scala/ch-2.scala | 57 +++++++++++++ challenge-348/roger-bell-west/tests.json | 68 ++++++++++++++++ challenge-348/roger-bell-west/typst/ch-1.typ | 44 ++++++++++ challenge-348/roger-bell-west/typst/ch-2.typ | 43 ++++++++++ 25 files changed, 1101 insertions(+) create mode 100755 challenge-348/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-348/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-348/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-348/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-348/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-348/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-348/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-348/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-348/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-348/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-348/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-348/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-348/roger-bell-west/python/ch-1.py create mode 100755 challenge-348/roger-bell-west/python/ch-2.py create mode 100755 challenge-348/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-348/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-348/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-348/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-348/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-348/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-348/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-348/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-348/roger-bell-west/tests.json create mode 100644 challenge-348/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-348/roger-bell-west/typst/ch-2.typ diff --git a/challenge-348/roger-bell-west/crystal/ch-1.cr b/challenge-348/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..5e2187bc75 --- /dev/null +++ b/challenge-348/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,39 @@ +#! /usr/bin/crystal + +def stringalike(a) + if a.size % 2 == 1 + return false + end + vt = 0 + mode = 1 + av = false + a.downcase.chars.each_with_index do |c, i| + if i * 2 == a.size + mode = -1 + end + if c.in_set?("aeiou") + vt += mode + av = true + end + end + av && (vt == 0) +end + +require "spec" +describe "stringalike" do + it "test_ex1" do + stringalike("textbook").should eq false + end + it "test_ex2" do + stringalike("book").should eq true + end + it "test_ex3" do + stringalike("AbCdEfGh").should eq true + end + it "test_ex4" do + stringalike("rhythmmyth").should eq false + end + it "test_ex5" do + stringalike("UmpireeAudio").should eq false + end +end diff --git a/challenge-348/roger-bell-west/crystal/ch-2.cr b/challenge-348/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..1596e0e5e7 --- /dev/null +++ b/challenge-348/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,40 @@ +#! /usr/bin/crystal + +def hm2m(a) + p = a.split(":") + return p[0].to_i * 60 + p[1].to_i +end + +def converttime(ssrc, ttgt) + src = hm2m(ssrc) + tgt = hm2m(ttgt) + if tgt < src + tgt += 24 * 60 + end + delta = tgt - src + oc = 0 + [60, 15, 5, 1].each do |op| + q, delta = delta.divmod(op) + oc += q + end + oc +end + +require "spec" +describe "converttime" do + it "test_ex1" do + converttime("02:30", "02:45").should eq 1 + end + it "test_ex2" do + converttime("11:55", "12:15").should eq 2 + end + it "test_ex3" do + converttime("09:00", "13:00").should eq 4 + end + it "test_ex4" do + converttime("23:45", "00:30").should eq 3 + end + it "test_ex5" do + converttime("14:20", "15:25").should eq 2 + end +end diff --git a/challenge-348/roger-bell-west/javascript/ch-1.js b/challenge-348/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..3a4f6ce6cd --- /dev/null +++ b/challenge-348/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,53 @@ +#! /usr/bin/node + +"use strict" + +function stringalike(a) { + if (a.length % 2 == 1) { + return false; + } + let vt = 0; + let mode = 1; + let av = false; + a.toLowerCase().split("").forEach((c, i) => { + if (i * 2 == a.length) { + mode = -1; + } + if (c.match(/[aeiou]/)) { + av = true; + vt += mode; + } + }); + return av && (vt == 0); +} + +if (!stringalike('textbook')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (stringalike('book')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (stringalike('AbCdEfGh')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!stringalike('rhythmmyth')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!stringalike('UmpireeAudio')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-348/roger-bell-west/javascript/ch-2.js b/challenge-348/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..22d55a17c9 --- /dev/null +++ b/challenge-348/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,54 @@ +#! /usr/bin/node + +"use strict" + +function hm2m(a) { + const p = a.match(/(\d+):(\d+)/); + return parseInt(p[1]) * 60 + parseInt(p[2]); +} + +function converttime(ssrc, ttgt) { + const src = hm2m(ssrc); + let tgt = hm2m(ttgt); + if (tgt < src) { + tgt += 24 * 60; + } + let delta = tgt - src; + let oc = 0; + for (let op of [60, 15, 5, 1]) { + oc += Math.floor(delta / op); + delta %= op; + } + return oc; +} + +if (converttime('02:30', '02:45') == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (converttime('11:55', '12:15') == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (converttime('09:00', '13:00') == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (converttime('23:45', '00:30') == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (converttime('14:20', '15:25') == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-348/roger-bell-west/kotlin/ch-1.kt b/challenge-348/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..d496da8f75 --- /dev/null +++ b/challenge-348/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,60 @@ +fun is_vowel(c: Char): Boolean { + return when (c.lowercaseChar()) { + 'a', 'e', 'i', 'o', 'u' -> true + else -> false + } +} + +fun stringalike(a: String): Boolean { + if (a.length % 2 == 1) { + return false + } + var vt = 0 + var mode = 1 + var av = false + a.toList().forEachIndexed { i, c -> + if (i * 2 == a.length) { + mode = -1 + } + if (is_vowel(c)) { + av = true + vt += mode + } + } + return av && (vt == 0) +} + +fun main() { + + if (!stringalike("textbook")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringalike("book")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringalike("AbCdEfGh")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!stringalike("rhythmmyth")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!stringalike("UmpireeAudio")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-348/roger-bell-west/kotlin/ch-2.kt b/challenge-348/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..7bbcd303be --- /dev/null +++ b/challenge-348/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,55 @@ + +fun hm2m(a: String): Int { + val p = a.split(":") + return p[0].toInt() * 60 + p[1].toInt() +} + +fun converttime(ssrc: String, ttgt: String): Int { + val src = hm2m(ssrc) + var tgt = hm2m(ttgt) + if (tgt < src) { + tgt += 24 * 60 + } + var delta: Int = tgt - src + var oc = 0 + for (op in listOf(60, 15, 5, 1)) { + oc += delta / op + delta %= op + } + return oc +} + +fun main() { + + if (converttime("02:30", "02:45") == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("11:55", "12:15") == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("09:00", "13:00") == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("23:45", "00:30") == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("14:20", "15:25") == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-348/roger-bell-west/lua/ch-1.lua b/challenge-348/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..3261a885d6 --- /dev/null +++ b/challenge-348/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,75 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function is_vowel(c) + if string.find(c, "[aeiou]") == 1 then + return true + else + return false + end +end + +function stringalike(a) + if #a % 2 == 1 then + return false + end + local vt = 0 + local mode = 1 + local av = false + for i,c in ipairs(split(string.lower(a))) do + if is_vowel(c) then + av = true + vt = vt + mode + end + if i * 2 == #a then + mode = -1 + end + end + return av and (vt == 0) +end + +if not stringalike("textbook") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if stringalike("book") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if stringalike("AbCdEfGh") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not stringalike("rhythmmyth") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not stringalike("UmpireeAudio") then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-348/roger-bell-west/lua/ch-2.lua b/challenge-348/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c8c13ba2c4 --- /dev/null +++ b/challenge-348/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,57 @@ +#! /usr/bin/lua + +function hm2m(a) + local h, m = string.match(a, "^(%d+):(%d+)$") + return h * 60 + m +end + +function converttime(ssrc, ttgt) + local src = hm2m(ssrc) + local tgt = hm2m(ttgt) + if tgt < src then + tgt = tgt + 24 * 60 + end + local delta = tgt - src + local oc = 0 + for _, op in ipairs({60, 15, 5, 1}) do + oc = oc + delta // op + delta = delta % op + end + return oc +end + +if converttime("02:30", "02:45") == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if converttime("11:55", "12:15") == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if converttime("09:00", "13:00") == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if converttime("23:45", "00:30") == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if converttime("14:20", "15:25") == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-348/roger-bell-west/perl/ch-1.pl b/challenge-348/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..a520a2353e --- /dev/null +++ b/challenge-348/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 => 5; + +is(stringalike('textbook'), 0, 'example 1'); +is(stringalike('book'), 1, 'example 2'); +is(stringalike('AbCdEfGh'), 1, 'example 3'); +is(stringalike('rhythmmyth'), 0, 'example 4'); +is(stringalike('UmpireeAudio'), 0, 'example 5'); + +sub stringalike($a) { + if (length($a) % 2 == 1) { + return 0; + } + my $vt = 0; + my $mode = 1; + my $av = 0; + my @sa = split('', lc($a)); + while (my ($i, $c) = each @sa) { + if ($i * 2 == length($a)) { + $mode = -1; + } + if ($c =~ /[aeiou]/) { + $av = 1; + $vt += $mode; + } + } + ($av && ($vt == 0))?1:0; +} diff --git a/challenge-348/roger-bell-west/perl/ch-2.pl b/challenge-348/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..07311837b9 --- /dev/null +++ b/challenge-348/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,33 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(converttime('02:30', '02:45'), 1, 'example 1'); +is(converttime('11:55', '12:15'), 2, 'example 2'); +is(converttime('09:00', '13:00'), 4, 'example 3'); +is(converttime('23:45', '00:30'), 3, 'example 4'); +is(converttime('14:20', '15:25'), 2, 'example 5'); + +sub hm2m($a) { + $a =~ /(\d+):(\d+)/; + $1 * 60 + $2; +} + +sub converttime($ssrc, $ttgt) { + my $src = hm2m($ssrc); + my $tgt = hm2m($ttgt); + if ($tgt < $src) { + $tgt += 24 * 60; + } + my $delta = $tgt - $src; + my $oc = 0; + foreach my $op (60, 15, 5, 1) { + $oc += int($delta / $op); + $delta %= $op; + } + $oc; +} diff --git a/challenge-348/roger-bell-west/postscript/ch-1.ps b/challenge-348/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000000000000000000000000000000000..5b924f038dccdd6a79ccd32d1decbe0bfe4ce410 GIT binary patch literal 1818 zcmaJ?O>f&U488kTaEAiUp;6Ld>sk~AiecNez_3#@42s1#LLAGJqBG9B%IeOaN3upI3W06EMJqi{rM2=IUJ9UR4oX)(It>CL0iUWhC|rg6LwEzs z2r8k$w)Sf}x_&uWU@HI*1FP={6%5qq;rBq^!^+rjjuLEDv4^E@Rq#^?kIqk>m(~wf zJF8m{<@E%@DR3jIlg?2zV|-CqnV#TL`o{2JGDQY|kwj^!7zXDNtHP(mTR;cDIe4GI zOGray1Bs7vO3wtUolDYTOnJVNx=ERP&?{^lo~RF1ZE%=7OB~Ih9xE8k-&3)uiBbnF ze@%!ZI~kZb$%!R7q;=6s@8Df)XN$?PwQ^w>Z7Sq)<)v~La;VbK zlP?h3Pc{wpW7-@vPF&K{qgDv*{_M;nHiNaB8~FSQyzOylYWRGWT`xc8*IfH0=U!cA z;Q4HRbkE`Q`H_Ae`EYw1q6rhXY0YDsWhk7dIjUu&H#nq0GxGm|KMeT=Y>lE($G-!W zJ7*=iH~Ew{zu1k##~OYFagn#nuttJR?jk>vO5k7YcsnUtB(Jl`Y}pv8pflZAeinkL z@^ux2EZ3*y8YnGl!b6r>b_aHd{yYz#m7(266n|H6)F!D?P{Q0WS-vBHYet{oDcwM$ zK65p8@FDk5aEhHgNLr>$I&2QU9B4Zrf!LV+N(&Lj~8nc-|0)f`}fGWzjI^G^d qdAIzkzO8<&b6(Wh^|N1ZxAe-juU3C;JNm=IyIyJY4kj!q&f^~@1@edh literal 0 HcmV?d00001 diff --git a/challenge-348/roger-bell-west/postscript/ch-2.ps b/challenge-348/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000000000000000000000000000000000..9e3117398770ceac295a3d9c299c3f5edbd132a8 GIT binary patch literal 1851 zcmah}O^@3k5WV|XOb?OpVdIY^mFh!}J+IW`CI=fx!j>2pNVHXz|6Ukk8+#qqN`yVU z`QY(pR$pF!CdmpKeiAK+mdY#R45Vmut^WX3jJsXKIh?KSOqFHc)Np-TD}Bm-2+f(U zfAOqS#u}52AR( z&&S1%3ISl(4kT|+_6#}j-;o?V%K)9~1~~)^{}7`>0Ad9NfKP6CtzG&!Q`m%1*XOW) z`+vpsA(p*ASJLA>6|+3AlE?l7 zQpExZU5^6HhYO8IL9oW%<_3;$wdG-yWamqH>HE;7>h*>-)>74MxORFIq4J)=-tZMW zA$YLasi $i, $c { + if ($i * 2 == $a.chars) { + $mode = -1; + } + if ($c ~~ /<[aeiou]>/) { + $av = True; + $vt += $mode; + } + } + $av && ($vt == 0); +} diff --git a/challenge-348/roger-bell-west/raku/ch-2.p6 b/challenge-348/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..11f38e6b13 --- /dev/null +++ b/challenge-348/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,31 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(converttime('02:30', '02:45'), 1, 'example 1'); +is(converttime('11:55', '12:15'), 2, 'example 2'); +is(converttime('09:00', '13:00'), 4, 'example 3'); +is(converttime('23:45', '00:30'), 3, 'example 4'); +is(converttime('14:20', '15:25'), 2, 'example 5'); + +sub hm2m($a) { + my @p = $a.comb(/\d+/); + @p[0] * 60 + @p[1]; +} + +sub converttime($ssrc, $ttgt) { + my $src = hm2m($ssrc); + my $tgt = hm2m($ttgt); + if ($tgt < $src) { + $tgt += 24 * 60; + } + my $delta = $tgt - $src; + my $oc = 0; + for [60, 15, 5, 1] -> $op { + $oc += $delta div $op; + $delta %= $op; + } + $oc; +} diff --git a/challenge-348/roger-bell-west/ruby/ch-1.rb b/challenge-348/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..3869587766 --- /dev/null +++ b/challenge-348/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,46 @@ +#! /usr/bin/ruby + +def stringalike(a) + if a.size % 2 == 1 + return false + end + vt = 0 + mode = 1 + av = false + a.downcase.chars.each_with_index do |c, i| + if i * 2 == a.size + mode = -1 + end + if c =~ /[aeiou]/ + vt += mode + av = true + end + end + av && (vt == 0) +end + +require 'test/unit' + +class TestStringalike < Test::Unit::TestCase + + def test_ex1 + assert_equal(false, stringalike('textbook')) + end + + def test_ex2 + assert_equal(true, stringalike('book')) + end + + def test_ex3 + assert_equal(true, stringalike('AbCdEfGh')) + end + + def test_ex4 + assert_equal(false, stringalike('rhythmmyth')) + end + + def test_ex5 + assert_equal(false, stringalike('UmpireeAudio')) + end + +end diff --git a/challenge-348/roger-bell-west/ruby/ch-2.rb b/challenge-348/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..b11fa066b6 --- /dev/null +++ b/challenge-348/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,47 @@ +#! /usr/bin/ruby + +def hm2m(a) + p = a.split(":") + return p[0].to_i * 60 + p[1].to_i +end + +def converttime(ssrc, ttgt) + src = hm2m(ssrc) + tgt = hm2m(ttgt) + if tgt < src + tgt += 24 * 60 + end + delta = tgt - src + oc = 0 + [60, 15, 5, 1].each do |op| + q, delta = delta.divmod(op) + oc += q + end + oc +end + +require 'test/unit' + +class TestConverttime < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, converttime('02:30', '02:45')) + end + + def test_ex2 + assert_equal(2, converttime('11:55', '12:15')) + end + + def test_ex3 + assert_equal(4, converttime('09:00', '13:00')) + end + + def test_ex4 + assert_equal(3, converttime('23:45', '00:30')) + end + + def test_ex5 + assert_equal(2, converttime('14:20', '15:25')) + end + +end diff --git a/challenge-348/roger-bell-west/rust/ch-1.rs b/challenge-348/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..36d4205eb9 --- /dev/null +++ b/challenge-348/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,49 @@ +#! /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!(stringalike("textbook"), false); +} + +#[test] +fn test_ex2() { + assert_eq!(stringalike("book"), true); +} + +#[test] +fn test_ex3() { + assert_eq!(stringalike("AbCdEfGh"), true); +} + +#[test] +fn test_ex4() { + assert_eq!(stringalike("rhythmmyth"), false); +} + +#[test] +fn test_ex5() { + assert_eq!(stringalike("UmpireeAudio"), false); + } + +fn stringalike(a: &str) -> bool { + if a.len() % 2 == 1 { + return false; + } + let mut vt = 0i32; + let mut mode = 1i32; + let mut av = false; + for (i, c) in a.to_ascii_lowercase().chars().enumerate() { + if i == a.len() / 2 { + mode = -1i32; + } + vt += match c { + 'a' | 'e' | 'i' | 'o' | 'u' => { + av = true; + mode + }, + _ => 0, + }; + } + av && (vt == 0) +} diff --git a/challenge-348/roger-bell-west/rust/ch-2.rs b/challenge-348/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..2180ae8ecf --- /dev/null +++ b/challenge-348/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,47 @@ +#! /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!(converttime("02:30", "02:45"), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(converttime("11:55", "12:15"), 2); +} + +#[test] +fn test_ex3() { + assert_eq!(converttime("09:00", "13:00"), 4); +} + +#[test] +fn test_ex4() { + assert_eq!(converttime("23:45", "00:30"), 3); +} + +#[test] +fn test_ex5() { + assert_eq!(converttime("14:20", "15:25"), 2); +} + +fn hm2m(a: &str) -> u32 { + let p = a.split(':').map(|x| x.parse::().unwrap()).collect::>(); + p[0] * 60 + p[1] +} + +fn converttime(ssrc: &str, ttgt: &str) -> u32 { + let src = hm2m(ssrc); + let mut tgt = hm2m(ttgt); + if tgt < src { + tgt += 24 * 60; + } + let mut delta = tgt - src; + let mut oc = 0; + for op in [60, 15, 5, 1] { + oc += delta / op; + delta %= op; + } + oc +} diff --git a/challenge-348/roger-bell-west/scala/ch-1.scala b/challenge-348/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..4889818b29 --- /dev/null +++ b/challenge-348/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,63 @@ + +object Stringalike { + def is_vowel(c: Char): Boolean = { + return c.toLower match { + case 'a' | 'e' | 'i' | 'o' | 'u' => true + case _ => false + } + } + + def stringalike(a: String): Boolean = { + if (a.length % 2 == 1) { + false + } else { + var vt = 0 + var mode = 1 + var av = false + for ((c, i) <- a.toList.zipWithIndex) { + if (i * 2 == a.length) { + mode = -1 + } + if (is_vowel(c)) { + av = true + vt += mode + } + } + return av && (vt == 0) + } + } + + def main(args: Array[String]) { + if (!stringalike("textbook")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringalike("book")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (stringalike("AbCdEfGh")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!stringalike("rhythmmyth")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!stringalike("UmpireeAudio")) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-348/roger-bell-west/scala/ch-2.scala b/challenge-348/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..d89592f191 --- /dev/null +++ b/challenge-348/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,57 @@ + +object Converttime { + + def hm2m(a: String): Int = { + val p = a.split(":") + return Integer.parseInt(p(0)) * 60 + Integer.parseInt(p(1)) + } + + def converttime(ssrc: String, ttgt: String): Int = { + val src = hm2m(ssrc) + var tgt = hm2m(ttgt) + if (tgt < src) { + tgt += 24 * 60 + } + var delta = tgt - src + var oc = 0 + for (op <- List(60, 15, 5, 1)) { + oc += delta / op + delta %= op + } + oc + } + + def main(args: Array[String]) { + if (converttime("02:30", "02:45") == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("11:55", "12:15") == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("09:00", "13:00") == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("23:45", "00:30") == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (converttime("14:20", "15:25") == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-348/roger-bell-west/tests.json b/challenge-348/roger-bell-west/tests.json new file mode 100644 index 0000000000..2e438862c9 --- /dev/null +++ b/challenge-348/roger-bell-west/tests.json @@ -0,0 +1,68 @@ +{ + "ch-1" : [ + { + "function" : "stringalike", + "arguments" : "textbook", + "result" : false + }, + { + "arguments" : "book", + "result" : true + }, + { + "arguments" : "AbCdEfGh", + "result" : true + }, + { + "arguments" : "rhythmmyth", + "result" : false + }, + { + "arguments" : "UmpireeAudio", + "result" : false + } + ], + "ch-2" : [ + { + "function" : "converttime", + "multiarg" : true, + "arguments" : [ + "02:30", + "02:45" + ], + "result" : 1 + }, + { + "multiarg" : true, + "arguments" : [ + "11:55", + "12:15" + ], + "result" : 2 + }, + { + "multiarg" : true, + "arguments" : [ + "09:00", + "13:00" + ], + "result" : 4 + }, + { + "multiarg" : true, + "arguments" : [ + "23:45", + "00:30" + ], + "result" : 3 + }, + { + "multiarg" : true, + "arguments" : [ + "14:20", + "15:25" + ], + "result" : 2 + } + ] +} diff --git a/challenge-348/roger-bell-west/typst/ch-1.typ b/challenge-348/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..dbe4e7c88f --- /dev/null +++ b/challenge-348/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,44 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let stringalike(a) = { + if calc.rem-euclid(a.len(), 2) == 1 { + false + } else { + let vt = 0 + let mode = 1 + let av = false + let ret = () + for (i, c) in lower(a).codepoints().enumerate() { + if i * 2 == a.len() { + mode = -1 + } + if c.match(regex("[aeiou]")) != none { + av = true + vt += mode + } + } + av and (vt == 0) + } +} + +Test 1: + #testresult(not stringalike("textbook")) + +Test 2: + #testresult(stringalike("book")) + +Test 3: + #testresult(stringalike("AbCdEfGh")) + +Test 4: + #testresult(not stringalike("rhythmmyth")) + +Test 5: + #testresult(not stringalike("UmpireeAudio")) + diff --git a/challenge-348/roger-bell-west/typst/ch-2.typ b/challenge-348/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..e565ed8aaf --- /dev/null +++ b/challenge-348/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,43 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let hm2m(a) = { + let p = a.split(":") + int(p.at(0)) * 60 + int(p.at(1)) +} + +#let converttime(ssrc, ttgt) = { + let src = hm2m(ssrc) + let tgt = hm2m(ttgt) + if tgt < src { + tgt += 24 * 60 + } + let delta = tgt - src + let oc = 0 + for op in (60, 15, 5, 1) { + oc += calc.div-euclid(delta, op) + delta = calc.rem-euclid(delta, op) + } + oc +} + +Test 1: + #testresult(converttime("02:30", "02:45") == 1) + +Test 2: + #testresult(converttime("11:55", "12:15") == 2) + +Test 3: + #testresult(converttime("09:00", "13:00") == 4) + +Test 4: + #testresult(converttime("23:45", "00:30") == 3) + +Test 5: + #testresult(converttime("14:20", "15:25") == 2) +