diff options
| author | Pok <pok@goyangi> | 2025-11-17 18:03:19 +0700 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-11-18 12:24:41 +0700 |
| commit | d7aa3e67c378f18a778610cb6c2d347a06dc58a4 (patch) | |
| tree | 942b5373e1b008ea6ba9a1092168816a36feb61e | |
| parent | e80a2ae531aa79507271b69576177ff61ef6c0df (diff) | |
| download | perlweeklychallenge-club-d7aa3e67c378f18a778610cb6c2d347a06dc58a4.tar.gz perlweeklychallenge-club-d7aa3e67c378f18a778610cb6c2d347a06dc58a4.tar.bz2 perlweeklychallenge-club-d7aa3e67c378f18a778610cb6c2d347a06dc58a4.zip | |
pwc248 solution in lua
| -rw-r--r-- | challenge-348/pokgopun/lua/ch-1.lua | 90 | ||||
| -rw-r--r-- | challenge-348/pokgopun/lua/ch-2.lua | 114 |
2 files changed, 204 insertions, 0 deletions
diff --git a/challenge-348/pokgopun/lua/ch-1.lua b/challenge-348/pokgopun/lua/ch-1.lua new file mode 100644 index 0000000000..da2e830838 --- /dev/null +++ b/challenge-348/pokgopun/lua/ch-1.lua @@ -0,0 +1,90 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +--[[ + +Task 1: String Alike + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string of even length. + + Write a script to find if the given string split into two halves of + equal lengths and they both have same number of vowels. + +Example 1 + +Input: $str = "textbook" +Output: false + +1st half: "text" (1 vowel) +2nd half: "book" (2 vowels) + +Example 2 + +Input: $str = "book" +Output: true + +1st half: "bo" (1 vowel) +2nd half: "ok" (1 vowel) + +Example 3 + +Input: $str = "AbCdEfGh" +Output: true + +1st half: "AbCd" (1 vowel) +2nd half: "EfGh" (1 vowel) + +Example 4 + +Input: $str = "rhythmmyth" +Output: false + +1st half: "rhyth" (0 vowel) +2nd half: "mmyth" (0 vowel) + +Example 5 + +Input: $str = "UmpireeAudio" +Output: false + +1st half: "Umpire" (3 vowels) +2nd half: "eAudio" (5 vowels) + +Task 2: Covert Time +--]] +--# solution by pokgopun@gmail.com + +--@params string +local function sa(str) --@return bool + str = string.lower(str) + local vwl = "aeiou" + local cnt1 = 0 + for i=1,#str/2 do + if string.match(vwl,string.sub(str,i,i)) then + cnt1 = cnt1 + 1 + end + end + local cnt2 = 0 + for i=1+#str/2,#str do + if string.match(vwl,string.sub(str,i,i)) then + cnt2 = cnt2 + 1 + end + end + return cnt1 > 0 and cnt1 == cnt2 +end + +local lu = require("luaunit") + +function TestSa() + for inpt, otpt in pairs({ + ["textbook"]= false, + ["book"]= true, + ["AbCdEfGh"]= true, + ["rhythmmyth"]= false, + ["UmpireeAudio"]= false}) do + lu.assertEquals(sa(inpt),otpt) + end +end + +lu.run() diff --git a/challenge-348/pokgopun/lua/ch-2.lua b/challenge-348/pokgopun/lua/ch-2.lua new file mode 100644 index 0000000000..036bd3fb6e --- /dev/null +++ b/challenge-348/pokgopun/lua/ch-2.lua @@ -0,0 +1,114 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/ +--[[ + +Task 2: Covert Time + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $source and $target, containing time in + 24-hour time form. + + Write a script to convert the source into target by performing one of + the following operations: +1. Add 1 minute +2. Add 5 minutes +3. Add 15 minutes +4. Add 60 minutes + + Find the total operations needed to get to the target. + +Example 1 + +Input: $source = "02:30" + $target = "02:45" +Output: 1 + +Just one operation i.e. "Add 15 minutes". + +Example 2 + +Input: $source = "11:55" + $target = "12:15" +Output: 2 + +Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". + +Example 3 + +Input: $source = "09:00" + $target = "13:00" +Output: 4 + +Four operations of "Add 60 minutes". + +Example 4 + +Input: $source = "23:45" + $target = "00:30" +Output: 3 + +Three operations of "Add 15 minutes". + +Example 5 + +Input: $source = "14:20" + $target = "15:25" +Output: 2 + +Two operations, one "Add 60 minutes" and one "Add 5 minutes" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 23rd November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +--]] +--# solution by pokgopun@gmail.com + +local function helper(timestr) + local m = 0 + local h2m = true + for t in string.gmatch(timestr,"%d+") do + t = tonumber(t) + if h2m then + t = t * 60 + h2m = false + end + m = m + t + end + return m +end + +--@params string, string +local function ct(source, target) --@return integer + local m1 = helper(source) + local m2 = helper(target) + if m2 < m1 then + m2 = m2 + 24*60 + end + m = m2 - m1 + c = 0 + for _, n in ipairs({60, 15, 5}) do + c = c + m // n + m = m % n + end + --print(source, target, m1, m2, m + c) + return c + m +end + +local lu = require("luaunit") + +function TestCt() + for _, data in ipairs({ + {source = "02:30",target = "02:45", Output= 1}, + {source = "11:55",target = "12:15",Output= 2}, + {source = "09:00",target = "13:00",Output= 4}, + {source = "23:45",target = "00:30",Output= 3}, + {source = "14:20",target = "15:25",Output= 2}}) do + lu.assertEquals(ct(data.source,data.target), data.Output) + end +end + +lu.run() |
