diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-20 12:33:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-20 12:33:42 +0100 |
| commit | d81b97819b40fef1af80b0d9585c9fc6b7147b5f (patch) | |
| tree | 60a1ba6f99ddc149a30d73ebc7757daf5822b85f /challenge-278/deadmarshal/lua | |
| parent | 65c93aa7401141c479d8ea993c3efba9b69ef9be (diff) | |
| parent | 83c26b915df95865c54062ab5c1a04a916ac217a (diff) | |
| download | perlweeklychallenge-club-d81b97819b40fef1af80b0d9585c9fc6b7147b5f.tar.gz perlweeklychallenge-club-d81b97819b40fef1af80b0d9585c9fc6b7147b5f.tar.bz2 perlweeklychallenge-club-d81b97819b40fef1af80b0d9585c9fc6b7147b5f.zip | |
Merge pull request #10459 from deadmarshal/TWC278
TWC278
Diffstat (limited to 'challenge-278/deadmarshal/lua')
| -rw-r--r-- | challenge-278/deadmarshal/lua/ch-1.lua | 22 | ||||
| -rw-r--r-- | challenge-278/deadmarshal/lua/ch-2.lua | 16 |
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-278/deadmarshal/lua/ch-1.lua b/challenge-278/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..df34e13834 --- /dev/null +++ b/challenge-278/deadmarshal/lua/ch-1.lua @@ -0,0 +1,22 @@ +#!/usr/bin/env lua + +local function sort_by(t,f) + assert(type(t) == 'table','t must be a table!') + local keys = {} + for k in pairs(t) do keys[#keys+1] = k end + table.sort(keys,function(a,b) return f(t[a],t[b]) end) + return keys +end + +local function sort_string(s) + assert(type(s) == 'string','s must be a string!') + local t = {} + for w in s:gmatch('%S+') do + for k,v in w:gmatch('(%a+)(%d+)') do t[k] = v end + end + return table.concat(sort_by(t,function(a,b) return a < b end),' ') +end + +print(sort_string'and2 Raku3 cousins5 Perl1 are4') +print(sort_string'guest6 Python1 most4 the3 popular5 is2 language7') +print(sort_string'Challenge3 The1 Weekly2') diff --git a/challenge-278/deadmarshal/lua/ch-2.lua b/challenge-278/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..d368132923 --- /dev/null +++ b/challenge-278/deadmarshal/lua/ch-2.lua @@ -0,0 +1,16 @@ +#!/usr/bin/env lua + +local function reverse_string(s,e) + assert(type(s) == 'string' and + type(e) == 'string','s and e must be strings!') + local t,idx = {},string.find(s,e,1,true) + if idx == nil then return s end + for i=1,idx do t[#t+1] = s:sub(i,i) end + table.sort(t) + return table.concat(t,'') .. s:sub(idx+1,#s) +end + +print(reverse_string('challenge','e')) +print(reverse_string('programming','a')) +print(reverse_string('champion','b')) + |
