diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-25 01:40:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-25 01:40:24 +0100 |
| commit | 68ac9e0880fcf10ea654b981f12355b83c0edb69 (patch) | |
| tree | f1810dc2d885cc0212694d586939159f6c725fb3 | |
| parent | caf014fcbe44b3c1ff9500d268d4bc861d89e16a (diff) | |
| parent | e3fbd1fea4f9be222d42c378822cabc71d5f0ac0 (diff) | |
| download | perlweeklychallenge-club-68ac9e0880fcf10ea654b981f12355b83c0edb69.tar.gz perlweeklychallenge-club-68ac9e0880fcf10ea654b981f12355b83c0edb69.tar.bz2 perlweeklychallenge-club-68ac9e0880fcf10ea654b981f12355b83c0edb69.zip | |
Merge pull request #12562 from HVukman/branch-for-challenge-335
Branch for challenge 335
| -rw-r--r-- | challenge-335/hvukman/lua/335_p1.lua | 50 | ||||
| -rw-r--r-- | challenge-335/hvukman/lua/335_p2.lua | 49 | ||||
| -rw-r--r-- | challenge-335/hvukman/picolisp/335_p1.l | 38 | ||||
| -rw-r--r-- | challenge-335/hvukman/picolisp/335_p2.l | 65 |
4 files changed, 202 insertions, 0 deletions
diff --git a/challenge-335/hvukman/lua/335_p1.lua b/challenge-335/hvukman/lua/335_p1.lua new file mode 100644 index 0000000000..fae6ac2a3a --- /dev/null +++ b/challenge-335/hvukman/lua/335_p1.lua @@ -0,0 +1,50 @@ +local inp = {{ "bella", "label", "roller" }, { "cool", "lock", "cook" } , { "hello", "world", "pole"} + , { "abc", "def", "ghi" }, { "aab", "aac", "aaa"}} + + +function Common(X) + local unique = {} + + + for i,v in ipairs(X) do + for j=1,#v do + local sub = string.sub(v,j,j) + unique[sub] = true + end + end + + + local res = {} + io.write ("Output: ") + -- check each unique character + for i,_ in pairs(unique) do + local res = {} + for _,v in ipairs(X) do + + local sum =0 + for index in (v):gmatch(i) do + sum = sum + 1 + -- print("index ", index) + end + if sum>0 then + table.insert(res,sum) + end + end + + if #res==3 then + table.sort(res) + + for jj=1,res[1] do + io.write (i," ") + end + + end + + end + print("") +end + +for i,v in ipairs(inp) do + Common(v) + +end diff --git a/challenge-335/hvukman/lua/335_p2.lua b/challenge-335/hvukman/lua/335_p2.lua new file mode 100644 index 0000000000..d60e13baff --- /dev/null +++ b/challenge-335/hvukman/lua/335_p2.lua @@ -0,0 +1,49 @@ +local moves = { {{0, 0} , {2, 0} , {1, 1} , {2, 1} , {2 ,2} }
+ , { {0, 0} , {1,1} , {0, 1} , {0, 2} , {1 ,0}, {2,0} }
+ , { {0, 0} , {1,1} , {2, 0} , {1, 0} , {1 ,2}, {2,1}, {0, 1} , {0 ,2}, {2,2} }
+ , { {0, 0} , {1,1 } }
+ , { {1,1} , {0,0} , {2,2} , {0, 1} , {1 ,0}, {0,2} }
+}
+local magic_square = { {4,9,2}, {3,5,7},{8,1,6}}
+-- https://en.wikipedia.org/wiki/Magic_square
+-- any winning combination is 15
+
+function Winner(X)
+ -- a game with less than 3 moves cannot be won
+ if #X>=3 then
+
+ local score_a = 0
+ local score_b = 0
+
+ for j=1,#X do
+
+ if j%2==0 then -- move b
+ score_b = score_b + magic_square[1+X[j][1]] [1+X[j][2]]
+ else -- move a
+ score_a = score_a + magic_square[1+X[j][1]] [1+X[j][2]]
+ end
+ -- winning score is 15
+ if score_a == 15 then
+ print ("Winner A")
+ else if score_b == 15 then
+ print ("winner B")
+ end
+ -- if at the end no score is 15 => draw
+ if j==#X and score_a~=15 and score_b~=15 then
+ print("draw")
+ end
+
+ end
+ end
+ else
+ print("pending")
+ end
+
+end
+
+for _,v in ipairs(moves) do
+ Winner(v)
+end
+
+
+
diff --git a/challenge-335/hvukman/picolisp/335_p1.l b/challenge-335/hvukman/picolisp/335_p1.l new file mode 100644 index 0000000000..d2928449c5 --- /dev/null +++ b/challenge-335/hvukman/picolisp/335_p1.l @@ -0,0 +1,38 @@ +(setq words '( '("bella" "label" "roller") '("cool" "lock" "cook") )) + +(de flatten (Lst) + (fish atom Lst) ) + +(de common (XX) +(setq chars (uniq (flatten (mapcar chop XX)))) + (for Y chars + # (println "char " Y) + (make + (for X XX + (let dummy (filter (quote (Z) (= Z Y )) (chop X)) + (if dummy + (link (size dummy)) + ) + ) + ) + + (cond + ( (= (size (made)) (size XX)) + (for Z (car (made)) + (print Y " ") + ) + (println "") + ) + ) + + ) + +) +) + +(for X words + (common X) +) + + +# (load "perlpico/335_p1.l") diff --git a/challenge-335/hvukman/picolisp/335_p2.l b/challenge-335/hvukman/picolisp/335_p2.l new file mode 100644 index 0000000000..6c2f0ff0b1 --- /dev/null +++ b/challenge-335/hvukman/picolisp/335_p2.l @@ -0,0 +1,65 @@ + +(setq magic_square '( (4 9 2) (3 5 7) (8 1 6)) ) + +(de Winner (X) + (if (<= 3 (length X)) + (let (score_a 0 score_b 0) + + (for Y (length X) + + (let (point (get X Y) point_x (get point 1) point_y (get point 2) + magic_square_x (get magic_square (+ 1 point_x)) + magic_square_y (get magic_square_x (+ 1 point_y)) + ) + # magic_square_y as point to get from magic_square + # (println "point " point) + # (println "mpoint " magic_square_y) + + + # every second move by b + (if (= 0 (% Y 2)) + (setq score_b (+ score_b magic_square_y)) + (setq score_a (+ score_a magic_square_y)) + ) + # (println "score_a " score_a) + # (println "score_b " score_b) + (cond + + ( + (= score_a 15) + 'winnerA + ) + ( + (= score_b 15) + 'winnerB + ) + ( + (= Y (length X)) + 'draw + ) + + + ) + + ) + + ) + + ) + 'pending + ) + +) + +(setq games '( ( (0 0) (2 0) (1 1) (2 1) (2 2) ) + ( (0 0) (1 1) (0 1) (0 2) (1 2) (2 0)) + ((0 0) (1 1) (2 0) (1 0) (1 2) (2 1) (0 1) (0 2) (2 2)) + ( (0 0 ) (1 1)) + ( (1 1) (0 0) (2 2) (0 1) (1 0) (0 2)) +) ) + +(for X games + (println (Winner X )) +) + +# (load "perlpico/335_p2.l")
\ No newline at end of file |
