diff options
| author | HVukman <peterslopp@googlemail.com> | 2025-07-27 19:10:54 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-27 19:10:54 +0200 |
| commit | 5b3fad24f1e302c82c7f838af5593b90ca6bc353 (patch) | |
| tree | 0b72a5a8626247a9ef11aa8ea486bdd969a98d21 /challenge-331/hvukman/lua | |
| parent | 1b5873f2797e27cf00ca842b39f5c3762facb50c (diff) | |
| download | perlweeklychallenge-club-5b3fad24f1e302c82c7f838af5593b90ca6bc353.tar.gz perlweeklychallenge-club-5b3fad24f1e302c82c7f838af5593b90ca6bc353.tar.bz2 perlweeklychallenge-club-5b3fad24f1e302c82c7f838af5593b90ca6bc353.zip | |
Add files via upload
Diffstat (limited to 'challenge-331/hvukman/lua')
| -rw-r--r-- | challenge-331/hvukman/lua/331_p2.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-331/hvukman/lua/331_p2.lua b/challenge-331/hvukman/lua/331_p2.lua new file mode 100644 index 0000000000..b27e23c72e --- /dev/null +++ b/challenge-331/hvukman/lua/331_p2.lua @@ -0,0 +1,50 @@ +-- http://lua-users.org/wiki/CopyTable
+-- Deepcopy of table
+function deepcopy(orig)
+ local orig_type = type(orig)
+ local copy
+ if orig_type == 'table' then
+ copy = {}
+ for orig_key, orig_value in next, orig, nil do
+ copy[deepcopy(orig_key)] = deepcopy(orig_value)
+ end
+ setmetatable(copy, deepcopy(getmetatable(orig)))
+ else -- number, string, boolean, etc
+ copy = orig
+ end
+ return copy
+end
+
+
+-- turn word to table and then swap it
+function Swap(Table, Pos1, Pos2)
+ -- deep copy of table and then return it
+ local new_tab = deepcopy(Table)
+ new_tab[Pos1], new_tab[Pos2] = new_tab[Pos2], new_tab[Pos1]
+ return new_tab
+end
+
+local word = {}
+local inp = "feed"
+local buddy = "feed"
+
+for i= 1,#inp do
+ table.insert(word,string.sub(inp,i,i))
+end
+
+local isbuddy = false
+for j= 1,#word-1 do
+ local newword = (table.concat(Swap(word,j,j+1)))
+ -- print (newword)
+ if newword == buddy then
+ print ("buddy: ", newword)
+ print("true")
+ isbuddy = true
+ break
+ end
+
+end
+
+if (not isbuddy) then
+ print("false")
+end
\ No newline at end of file |
