aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-26 23:19:47 +0000
committerGitHub <noreply@github.com>2025-10-26 23:19:47 +0000
commit5c212ef407c91b7e9b5f0ba9501cbc94ad8aff02 (patch)
tree4e2535bbd90da62792d3357f8023486a14f11630
parented6eb0e16d0e747d1c14c702c5b64e179eb6cae5 (diff)
parentbf1573a3b7ddd48d6c82bac798923235f9c19d42 (diff)
downloadperlweeklychallenge-club-5c212ef407c91b7e9b5f0ba9501cbc94ad8aff02.tar.gz
perlweeklychallenge-club-5c212ef407c91b7e9b5f0ba9501cbc94ad8aff02.tar.bz2
perlweeklychallenge-club-5c212ef407c91b7e9b5f0ba9501cbc94ad8aff02.zip
Merge pull request #12918 from HVukman/branch-for-challenge-344
Branch for challenge 344
-rw-r--r--challenge-344/hvukman/k/344_p2.k8
-rw-r--r--challenge-344/hvukman/lua/344_p1.lua29
-rw-r--r--challenge-344/hvukman/lua/344_p2.lua73
-rw-r--r--challenge-344/hvukman/picolisp/344_p1.l11
-rw-r--r--challenge-344/hvukman/picolisp/344_p2.l35
5 files changed, 156 insertions, 0 deletions
diff --git a/challenge-344/hvukman/k/344_p2.k b/challenge-344/hvukman/k/344_p2.k
new file mode 100644
index 0000000000..404d1036e4
--- /dev/null
+++ b/challenge-344/hvukman/k/344_p2.k
@@ -0,0 +1,8 @@
+prm:{$[0=x;,!0;,/(prm x-1){?[1+x;y;0]}/:\:!x]}
+perm:{x[prm[#x]]}
+/ returns 0! if no permutation equals target
+{target::x;source::y; &1=*/'{x=target}'(,//'perm(source))}[1 2 3 4;((2;3);(1);(4)) ]
+{target::x;source::y; &1=*/'{x=target}'(,//'perm(source))}[1 2 3 4;((1;3);(2;4))]
+{target::x;source::y; &1=*/'{$[(#x)=#target;x=target;0 ]}'(,//'perm(source))}[5 8 2 9 1;((9;1);(5;8);(2))]
+{target::x;source::y; &1=*/'{$[(#x)=#target;x=target;0 ]}'(,//'perm(source))}[1 2 3;((1);(3))]
+{target::x;source::y; &1=*/'{x=target}'(,//'perm(source))}[7 4 6;((7;4;6))]
diff --git a/challenge-344/hvukman/lua/344_p1.lua b/challenge-344/hvukman/lua/344_p1.lua
new file mode 100644
index 0000000000..353d44ba6c
--- /dev/null
+++ b/challenge-344/hvukman/lua/344_p1.lua
@@ -0,0 +1,29 @@
+
+local function arrayfromcompute(ints,x)
+
+ local str = ""
+
+ for i,v in ipairs(ints) do
+ str = str .. v
+ end
+
+ str = tostring(tonumber(str) + x )
+ local empty = {}
+
+ for i=1,#str do
+ table.insert(empty,string.sub(str,i,i))
+ end
+
+ for i,v in ipairs(empty) do
+ io.write(v," ")
+ end
+ io.write('\n')
+end
+
+
+arrayfromcompute({1,2,3,4},12)
+arrayfromcompute({2,7,4},181)
+arrayfromcompute({9,9,9},1)
+arrayfromcompute({1,0,0,0,0},9999)
+arrayfromcompute({0},1000)
+
diff --git a/challenge-344/hvukman/lua/344_p2.lua b/challenge-344/hvukman/lua/344_p2.lua
new file mode 100644
index 0000000000..64a78a55eb
--- /dev/null
+++ b/challenge-344/hvukman/lua/344_p2.lua
@@ -0,0 +1,73 @@
+
+ local function permgen (a, n)
+ if n == 0 then
+ coroutine.yield(a)
+ else
+ for i=1,n do
+ -- put i-th element as the last one
+ a[n], a[i] = a[i], a[n]
+ -- generate all permutations of the other elements
+ permgen(a, n - 1)
+ -- restore i-th element
+ a[n], a[i] = a[i], a[n]
+
+ end
+ end
+ end
+
+
+ local function result (a,t)
+ local found = {}
+ local count = 0
+
+ -- a is permutation
+ for _,v in ipairs(a) do
+ for _,k in ipairs(v) do
+ table.insert(found,k)
+ end
+ end
+
+ for i=1,#found do
+
+ -- if key=value add up
+ if found[i]==t[i] then
+ count = count + 1
+ end
+ end
+ -- true if all keys equals values
+ return count==#t
+ end
+
+
+
+ local function perm (a)
+ local n = #a
+ local co = coroutine.create(function () permgen(a, n) end)
+ return function () -- iterator
+ local code, res = coroutine.resume(co)
+ return res
+ end
+ end
+
+
+
+local function arrayform(t,x)
+
+ local search = false
+
+ for p in perm(x) do
+ search=(result (p,t))
+ if result (p,t)==true then break end
+ end
+
+ print(search)
+
+end
+
+
+arrayform({1,2,3,4},{{2, 3},{1},{4}})
+arrayform({1,2,3,4},{{1, 3},{2,4}})
+arrayform({5, 8, 2, 9, 1},{{9,1},{5,8},{2}})
+arrayform({1,2,3},{{1},{3}})
+arrayform({7,4,6},{{7,4,6}})
+-- permute dummy
diff --git a/challenge-344/hvukman/picolisp/344_p1.l b/challenge-344/hvukman/picolisp/344_p1.l
new file mode 100644
index 0000000000..17fa1208df
--- /dev/null
+++ b/challenge-344/hvukman/picolisp/344_p1.l
@@ -0,0 +1,11 @@
+
+(de array_from_compute (X Y )
+ (mapcar '((Z) (prin Z " , ")) (mapcar format (chop (format (+ (format (pack (mapcar format X))) Y )))))
+ (prinl "^J")
+)
+
+(array_from_compute '(1 2 3 4) 12)
+(array_from_compute '(2 7 4) 181)
+(array_from_compute '(9 9 9) 1)
+(array_from_compute '(1, 0, 0, 0, 0) 9999)
+(array_from_compute '(0) 1000)
diff --git a/challenge-344/hvukman/picolisp/344_p2.l b/challenge-344/hvukman/picolisp/344_p2.l
new file mode 100644
index 0000000000..fdc57466bc
--- /dev/null
+++ b/challenge-344/hvukman/picolisp/344_p2.l
@@ -0,0 +1,35 @@
+# for permutations
+(load "@lib/simul.l")
+
+(de flatten (Lst)
+ (fish atom Lst)
+)
+
+# map get over flattened permutations
+# see if target is member
+(de arrayformation (X Y)
+ (if
+ (member X
+ (make
+ (for Y (mapcar flatten (permute Y))
+ (link
+ (make (for Z (length Y)
+ (link (get Y Z))
+ )
+ )
+ )
+ )
+ )
+ )
+ 'true
+ 'false
+ )
+)
+
+(arrayformation '(1 2 3 4) '( (2 3 ) (1) (4)))
+(arrayformation '(1 2 3 4) '( (1 3 ) ( 2 4) ))
+(arrayformation '(5 8 2 9 1) '( (9 1 ) ( 5 8 ) (2) ))
+(arrayformation '(1 2 3) '( ( 1 ) (3 ) ))
+(arrayformation '(7 4 6) '( ( 7 4 6 ) ))
+
+