diff options
| author | Pok <pok@goyangi> | 2025-10-21 00:07:23 +0700 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-10-21 00:07:23 +0700 |
| commit | 2a505a3c19f0197d1b4ff717e1163d01f468168f (patch) | |
| tree | a337e4a7aa06a585673941065364503f1677bf15 | |
| parent | bedb01372c255ebec06ae07cbee65e371ee16152 (diff) | |
| download | perlweeklychallenge-club-2a505a3c19f0197d1b4ff717e1163d01f468168f.tar.gz perlweeklychallenge-club-2a505a3c19f0197d1b4ff717e1163d01f468168f.tar.bz2 perlweeklychallenge-club-2a505a3c19f0197d1b4ff717e1163d01f468168f.zip | |
pwc344 solution in lua
| -rw-r--r-- | challenge-344/pokgopun/lua/ch-1.lua | 96 | ||||
| -rw-r--r-- | challenge-344/pokgopun/lua/ch-2.lua | 127 |
2 files changed, 223 insertions, 0 deletions
diff --git a/challenge-344/pokgopun/lua/ch-1.lua b/challenge-344/pokgopun/lua/ch-1.lua new file mode 100644 index 0000000000..4cb4637aea --- /dev/null +++ b/challenge-344/pokgopun/lua/ch-1.lua @@ -0,0 +1,96 @@ +#!/usr/bin/env lua5.4 +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-344/ +--[[ + +Task 1: Array Form Compute + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer, $x. + + Write a script to add $x to the integer in the array-form. + + The array form of an integer is a digit-by-digit representation + stored as an array, where the most significant digit is at the 0th + index. + +Example 1 + +Input: @ints = (1, 2, 3, 4), $x = 12 +Output: (1, 2, 4, 6) + +Example 2 + +Input: @ints = (2, 7, 4), $x = 181 +Output: (4, 5, 5) + +Example 3 + +Input: @ints = (9, 9, 9), $x = 1 +Output: (1, 0, 0, 0) + +Example 4 + +Input: @ints = (1, 0, 0, 0, 0), $x = 9999 +Output: (1, 9, 9, 9, 9) + +Example 5 + +Input: @ints = (0), $x = 1000 +Output: (1, 0, 0, 0) + +Task 2: Array Formation +--]] +--# solution by pokgopun@gmail.com + +--@params {int} +function nums2num(nums) --@return int + local t = 1 + local num = 0 + for i=#nums,1,-1 do + num = num + t * nums[i] + t = t * 10 + end + return num +end + +--@params int +function num2nums(num) --@return {int} + local nums = {} + while num > 0 do + table.insert(nums, 1, num % 10) + num = (num- nums[1])/10 + end + return nums +end + +--@params {int}, {int} +function compare_nums(nums1,nums2) --@return bool + for i=1,#nums1 do + if nums1[i] ~= nums2[i] then + return false + end + end + return true +end + +--@params {int}, int +function afc(nums, n) --@return {int} + return num2nums(nums2num(nums)+n) +end + +local lu = require("luaunit") + +function TestAfc() + for _, data in ipairs({ + {ints = {1, 2, 3, 4}, x = 12, Output= {1, 2, 4, 6}}, + {ints = {2, 7, 4}, x = 181, Output= {4, 5, 5}}, + {ints = {9, 9, 9}, x = 1, Output= {1, 0, 0, 0}}, + {ints = {1, 0, 0, 0, 0}, x = 9999, Output= {1, 9, 9, 9, 9}}, + {ints = {0}, x = 1000,Output= {1, 0, 0, 0}}}) do + lu.assertEquals(afc(data.ints,data.x),data.Output) + end +end + +lu.run() diff --git a/challenge-344/pokgopun/lua/ch-2.lua b/challenge-344/pokgopun/lua/ch-2.lua new file mode 100644 index 0000000000..6de8bfcdfa --- /dev/null +++ b/challenge-344/pokgopun/lua/ch-2.lua @@ -0,0 +1,127 @@ +#!/usr/bin/env lua5.4 +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-344/ +--[[ + +Task 2: Array Formation + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two list: @source and @target. + + Write a script to see if you can build the exact @target by putting + these smaller lists from @source together in some order. You cannot + break apart or change the order inside any of the smaller lists in + @source. + +Example 1 + +Input: @source = ([2,3], [1], [4]) + @target = (1, 2, 3, 4) +Output: true + +Use in the order: [1], [2,3], [4] + +Example 2 + +Input: @source = ([1,3], [2,4]) + @target = (1, 2, 3, 4) +Output: false + +Example 3 + +Input: @source = ([9,1], [5,8], [2]) + @target = (5, 8, 2, 9, 1) +Output: true + +Use in the order: [5,8], [2], [9,1] + +Example 4 + +Input: @source = ([1], [3]) + @target = (1, 2, 3) +Output: false + +Missing number: 2 + +Example 5 + +Input: @source = ([7,4,6]) + @target = (7, 4, 6) +Output: true + +Use in the order: [7, 4, 6] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 26th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +--]] +--# solution by pokgopun@gmail.com + +--@params {int}, {int} +function compare_nums(nums1,nums2) --@return bool + for i=1,#nums1 do + if nums1[i] ~= nums2[i] then + return false + end + end + return true +end + +--@params {{type}}, int +function permgen(a, n) + n = n or #a + if n == 0 then + coroutine.yield(a) + else + for i = 1, n do + a[n], a[i] = a[i], a[n] -- Swap elements + permgen(a, n - 1) + a[n], a[i] = a[i], a[n] -- Backtrack (swap back) + end + end +end + +--@params {{int}}, {int} +function af(source, target) --@return bool, error + local co = coroutine.create(function() permgen(source) end) + while true do + local status, p = coroutine.resume(co) + if not status then + return nil, "Error: "..p + end + if p then + local t = {} + local i = 1 + for _, ints in ipairs(p) do + local l = #ints + table.move(ints,1,l,i,t) + i = i + l + end + if compare_nums(target,t) then + return true + end + else + break -- No more permutations + end + end + return false +end + +local lu = require("luaunit") + +function TestAf() + for _, data in ipairs({ + {source = {{2,3}, {1}, {4}},target = {1, 2, 3, 4},Output= true}, + {source = {{1,3}, {2,4}},target = {1, 2, 3, 4},Output= false}, + {source = {{9,1}, {5,8}, {2}},target = {5, 8, 2, 9, 1},Output= true}, + {source = {{1}, {3}},target = {1, 2, 3},Output= false}, + {source = {{7,4,6}},target = {7, 4, 6},Output= true}}) do + lu.assertEquals(af(data.source,data.target), data.Output) + end +end + +lu.run() |
