diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-24 11:30:49 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-24 11:30:49 +0100 |
| commit | 480d500ac1c1c6cd84abc83851a2c5a369c93994 (patch) | |
| tree | ef6012cfc0f5fd78ec19758620013910ec5a98ba | |
| parent | 7bb07521ad0fd34a0540877f9d48f1c6bb97281d (diff) | |
| download | perlweeklychallenge-club-480d500ac1c1c6cd84abc83851a2c5a369c93994.tar.gz perlweeklychallenge-club-480d500ac1c1c6cd84abc83851a2c5a369c93994.tar.bz2 perlweeklychallenge-club-480d500ac1c1c6cd84abc83851a2c5a369c93994.zip | |
- Added more guest contributions by Laurent Rosenfeld.
| -rw-r--r-- | challenge-174/laurent-rosenfeld/javascript/ch-2.js | 34 | ||||
| -rw-r--r-- | challenge-174/laurent-rosenfeld/julia/ch-2.jl | 26 | ||||
| -rw-r--r-- | challenge-174/laurent-rosenfeld/python/ch-2.py | 25 | ||||
| -rw-r--r-- | challenge-174/laurent-rosenfeld/ruby/ch-2.rb | 23 |
4 files changed, 108 insertions, 0 deletions
diff --git a/challenge-174/laurent-rosenfeld/javascript/ch-2.js b/challenge-174/laurent-rosenfeld/javascript/ch-2.js new file mode 100644 index 0000000000..c943830310 --- /dev/null +++ b/challenge-174/laurent-rosenfeld/javascript/ch-2.js @@ -0,0 +1,34 @@ +function permute(inputArray) { + let inAry = [...inputArray].sort(); //copy and sort input + return inAry.reduce(function permute(res, item, key, arr) { + return res.concat(arr.length > 1 && arr.slice(0, key) + .concat(arr.slice(key + 1)) + .reduce(permute, []) + .map(function (perm) { + return [item].concat(perm); + }) || item); + }, []); +} + +function permutation2rank(perms, in_perm) { + let input = JSON.stringify(in_perm) + for (var i = 0; i < perms.length; i++) { + // stringify permutations to enable comparison + if (JSON.stringify(perms[i]) == input) { + return i + } + } +} + +function rank2permutation(perm_list, index) { + return perm_list[index] +} + +let perm_in = [3, 1, 2]; +let perms = permute(perm_in) +console.log(perms) +let rank = permutation2rank(perms, perm_in) +console.log("Permutation", perm_in, "has rank", rank) +for (var i = 0; i < perms.length; i++) { + console.log("Rank: ", i, " -> permutation ", rank2permutation(perms, i)) +} diff --git a/challenge-174/laurent-rosenfeld/julia/ch-2.jl b/challenge-174/laurent-rosenfeld/julia/ch-2.jl new file mode 100644 index 0000000000..704124f860 --- /dev/null +++ b/challenge-174/laurent-rosenfeld/julia/ch-2.jl @@ -0,0 +1,26 @@ +# Note: Julia array subscripts start at 1, not 0 +using Combinatorics + +function permute(in_list) + return collect(permutations(sort(in_list), length(in_list))) +end + +function permutation2rank(perms, input) + for i in 1:length(perms) + if perms[i] == input + return i + end + end +end + +function rank2permutation(perm_list, index) + return perm_list[index] +end + +perm_in = [3, 1, 2] +perms = permute(perm_in) +println("Permutations: ", perms) +println("Permutation ", perm_in, " -> rank ", permutation2rank(perms, perm_in)) +for i in 1:length(perms) + println("Rank: ", i, " -> permutation ", rank2permutation(perms, i)) +end diff --git a/challenge-174/laurent-rosenfeld/python/ch-2.py b/challenge-174/laurent-rosenfeld/python/ch-2.py new file mode 100644 index 0000000000..c472dd0db0 --- /dev/null +++ b/challenge-174/laurent-rosenfeld/python/ch-2.py @@ -0,0 +1,25 @@ +def stringify(input): + return " ".join(map(str, input)) + +def permute(input): + temp = input.copy() # avoid modifying input perm with the sort + temp.sort() + return list(itertools.permutations(temp)) + +def permutation2rank(perms, input): + perms_str = map(stringify, perms) + input_str = stringify(input) + for index, value in enumerate(perms_str): + if value == input_str: + return index + +def rank2permutation(permutation, rank): + return permutation[rank] + +perm = [3, 1, 2] +perms = permute(perm) +print("Permutations: ", str(perms)) +rank = permutation2rank(perms, perm) +print("Permutation ", perm, " -> rank ", rank) +for i in range(0, len(perms)): + print("Rank: ", i, " -> permutation ", rank2permutation(perms, i)) diff --git a/challenge-174/laurent-rosenfeld/ruby/ch-2.rb b/challenge-174/laurent-rosenfeld/ruby/ch-2.rb new file mode 100644 index 0000000000..e2fea01378 --- /dev/null +++ b/challenge-174/laurent-rosenfeld/ruby/ch-2.rb @@ -0,0 +1,23 @@ +def permute(in_list) + return in_list.sort.permutation(in_list.length).to_a +end + +def permutation2rank(perms, input) + for i in 0..perms.length - 1 + if perms[i] == input + return i + end + end +end + +def rank2permutation(perms, index) + return perms[index] +end + +perm_in = [3, 1, 2] +perms = permute(perm_in) +puts("Permutations: #{perms} \n") +print("Permutation #{perm_in} -> rank #{permutation2rank(perms, perm_in)} \n") +for i in 1..perms.length - 1 + print("Rank: #{i} -> permutation #{rank2permutation(perms, i)} \n") +end |
