diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-31 10:27:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-31 10:27:42 +0100 |
| commit | 148579e6b33e8d78ee58a900d60c6d6264d6106f (patch) | |
| tree | 188fcd42aeb8eeff99fad90b436d0d0f7f45c4a1 | |
| parent | fd7295adcd11759e120d5e8ccec537c08d6bbe73 (diff) | |
| parent | cc58dbc1166b4555b358a4defac1fa8e10e81976 (diff) | |
| download | perlweeklychallenge-club-148579e6b33e8d78ee58a900d60c6d6264d6106f.tar.gz perlweeklychallenge-club-148579e6b33e8d78ee58a900d60c6d6264d6106f.tar.bz2 perlweeklychallenge-club-148579e6b33e8d78ee58a900d60c6d6264d6106f.zip | |
Merge pull request #12427 from HVukman/branch-for-challenge-331
Branch for challenge 331
| -rw-r--r-- | challenge-331/hvukman/f#/331_p1.fs | 7 | ||||
| -rw-r--r-- | challenge-331/hvukman/f#/331_p2.fs | 14 | ||||
| -rw-r--r-- | challenge-331/hvukman/lua/331_p1.lua | 20 | ||||
| -rw-r--r-- | challenge-331/hvukman/lua/331_p2.lua | 50 |
4 files changed, 91 insertions, 0 deletions
diff --git a/challenge-331/hvukman/f#/331_p1.fs b/challenge-331/hvukman/f#/331_p1.fs new file mode 100644 index 0000000000..93291e5af5 --- /dev/null +++ b/challenge-331/hvukman/f#/331_p1.fs @@ -0,0 +1,7 @@ +// part 1 +// Trim white space, split at space, take last and length +printf "part 1\n" +let lastword (x:string) = x.Trim(' ').Split(" ") |> Array.last |> String.length +printf "%i\n" (lastword "The Weekly Challenge") +printf "%i\n" (lastword " hello world ") +printf "%i\n" (lastword "Let's begin the fun") diff --git a/challenge-331/hvukman/f#/331_p2.fs b/challenge-331/hvukman/f#/331_p2.fs new file mode 100644 index 0000000000..4c6c6b2a64 --- /dev/null +++ b/challenge-331/hvukman/f#/331_p2.fs @@ -0,0 +1,14 @@ +let rec sth4 (x,y,i) = if i <= Seq.length(x)-2 then + // Update and switch at index i and i+1 + let updated = Seq.toArray x |> Array.updateAt i (x:string).[i+1] |> Array.updateAt (i+1) (x:string).[i] + // compare arrays + let erg = updated = (Seq.toArray y) + if erg= true then printf "%b\n" erg else sth4 (x,y,(i+1)) + // if i too big, return "false" + else printf "%s\n" "false" + + +sth4 ("fuck","fcuk",0) +sth4 ("love","love",0) +sth4 ("fodo","food",0) +sth4 ("feed","feed",0) diff --git a/challenge-331/hvukman/lua/331_p1.lua b/challenge-331/hvukman/lua/331_p1.lua new file mode 100644 index 0000000000..bf882dab1d --- /dev/null +++ b/challenge-331/hvukman/lua/331_p1.lua @@ -0,0 +1,20 @@ +-- http://lua-users.org/wiki/StringTrim +function Trim(s) + return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)' +end + +function Last_length (X) + local words = {} + + for word in Trim(X):gmatch("%w+") do + table.insert(words, word) + end + + local last_word = words[#words] + print(#last_word) + +end + +Last_length("The Weekly Challenge") +Last_length(" Hello World ") +Last_length("Let's begin the fun") 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 |
