aboutsummaryrefslogtreecommitdiff
path: root/challenge-109
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-04-25 03:20:59 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-04-25 03:20:59 +0100
commit3531cc83e5609d9a827b954b4bb017bd1124ccbc (patch)
tree09ac6b83780cbd562d589daf0857a49ff40ead6a /challenge-109
parent863fbce2df95c7b213a29e2110c57004efde6b88 (diff)
downloadperlweeklychallenge-club-3531cc83e5609d9a827b954b4bb017bd1124ccbc.tar.gz
perlweeklychallenge-club-3531cc83e5609d9a827b954b4bb017bd1124ccbc.tar.bz2
perlweeklychallenge-club-3531cc83e5609d9a827b954b4bb017bd1124ccbc.zip
- Added guest contributions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-109')
-rw-r--r--challenge-109/laurent-rosenfeld/c/ch-2.c33
-rw-r--r--challenge-109/laurent-rosenfeld/d/ch-2.d14
-rw-r--r--challenge-109/laurent-rosenfeld/go/ch-2.go37
-rw-r--r--challenge-109/laurent-rosenfeld/julia/ch-2.julia25
-rw-r--r--challenge-109/laurent-rosenfeld/kotlin/ch-2.kt28
-rw-r--r--challenge-109/laurent-rosenfeld/lua/ch-2.lua20
-rw-r--r--challenge-109/laurent-rosenfeld/nim/ch-2.nim12
-rw-r--r--challenge-109/laurent-rosenfeld/python/ch-2.py6
-rw-r--r--challenge-109/laurent-rosenfeld/ruby/ch-2.rb9
-rw-r--r--challenge-109/laurent-rosenfeld/scala/ch-2.scala14
10 files changed, 198 insertions, 0 deletions
diff --git a/challenge-109/laurent-rosenfeld/c/ch-2.c b/challenge-109/laurent-rosenfeld/c/ch-2.c
new file mode 100644
index 0000000000..85022d6dd9
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/c/ch-2.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+int check_squares (int p[]) {
+ int sum = p[0] + p[1];
+ return ( p[1] + p[2] + p[3] == sum &&
+ p[3] + p[4] + p[5] == sum &&
+ p[5] + p[6] == sum);
+}
+
+void swap (int a[], int m, int n) {
+ int temp = a[m];
+ a[m] = a[n];
+ a[n] = temp;
+}
+
+int main() {
+ int in[7] = {7, 6, 5, 4, 3, 2, 1}; # values must be in descending order
+ int i, j;
+ int fact = 5040; //factorial 7
+ while (fact--) {
+ if (check_squares(in)) {
+ for (int i = 0; i < 6; i++) printf("%d ", in[i]);
+ printf("%d\n", in[6]);
+ }
+ i = 1;
+ while (in[i] > in[i-1]) i++;
+ j = 0;
+ while (in[j] < in[i]) j++;
+ swap(in, i, j);
+ i--;
+ for (j = 0; j < i; i--, j++) swap(in, i, j);
+ }
+}
diff --git a/challenge-109/laurent-rosenfeld/d/ch-2.d b/challenge-109/laurent-rosenfeld/d/ch-2.d
new file mode 100644
index 0000000000..8ce0553755
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/d/ch-2.d
@@ -0,0 +1,14 @@
+bool check_squares (int [] p) {
+ int sum = p[0] + p[1];
+ return ( p[1] + p[2] + p[3] == sum &&
+ p[3] + p[4] + p[5] == sum &&
+ p[5] + p[6] == sum);
+}
+
+void main() {
+ import std.stdio, std.algorithm;
+ auto items = [1, 2, 3, 4, 5, 6, 7];
+ do
+ if (check_squares(items)) items.writeln;
+ while (items.nextPermutation);
+}
diff --git a/challenge-109/laurent-rosenfeld/go/ch-2.go b/challenge-109/laurent-rosenfeld/go/ch-2.go
new file mode 100644
index 0000000000..2af4e8f721
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/go/ch-2.go
@@ -0,0 +1,37 @@
+package main
+
+import "fmt"
+
+func check_squares ( p[] int) bool {
+ var sum = p[0] + p[1]
+ return ( p[1] + p[2] + p[3] == sum &&
+ p[3] + p[4] + p[5] == sum &&
+ p[5] + p[6] == sum)
+}
+
+func main() {
+ var in = []int{1, 2, 3, 4, 5, 6, 7}
+ var max = len(in) - 1
+ var i, j int
+ for c := 1; c < 5040; c++ { // 7! = 5040
+ i = max - 1
+ j = max
+ for in[i] > in[i+1] {
+ i--
+ }
+ for in[j] < in[i] {
+ j--
+ }
+ in[i], in[j] = in[j], in[i]
+ j = max
+ i += 1
+ for i < j {
+ in[i], in[j] = in[j], in[i]
+ i++
+ j--
+ }
+ if check_squares(in) {
+ fmt.Println(in)
+ }
+ }
+}
diff --git a/challenge-109/laurent-rosenfeld/julia/ch-2.julia b/challenge-109/laurent-rosenfeld/julia/ch-2.julia
new file mode 100644
index 0000000000..d60cea1b4d
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/julia/ch-2.julia
@@ -0,0 +1,25 @@
+function check_squares(p)
+ ∑ = p[1] + p[2]
+ return (p[2] + p[3] + p[4] == ∑ &&
+ p[4] + p[5] + p[6] == ∑ &&
+ p[6] + p[7] == ∑)
+end
+
+function permute(k, in) # Heap's algorithm
+ if (k == 1)
+ if (check_squares(in)) println(in) end
+ # println(in)
+ else
+ permute(k - 1, in)
+ for i = 1:(k-1)
+ if (k % 2 == 0)
+ in[i], in[k] = in[k], in[i]
+ else
+ in[1], in[k] = in[k], in[1]
+ end
+ permute(k - 1, in)
+ end
+ end
+end
+
+permute(7, [1, 2, 3, 4, 5, 6, 7])
diff --git a/challenge-109/laurent-rosenfeld/kotlin/ch-2.kt b/challenge-109/laurent-rosenfeld/kotlin/ch-2.kt
new file mode 100644
index 0000000000..a24a2d1f28
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/kotlin/ch-2.kt
@@ -0,0 +1,28 @@
+fun <T> permute(input: List<T>): List<List<T>> {
+ if (input.size == 1) return listOf(input)
+ val perms = mutableListOf<List<T>>()
+ val toInsert = input[0]
+ for (perm in permute(input.drop(1))) {
+ for (i in 0..perm.size) {
+ val newPerm = perm.toMutableList()
+ newPerm.add(i, toInsert)
+ perms.add(newPerm)
+ }
+ }
+ return perms
+}
+
+fun check_squares(p: List<Int>): Boolean {
+ val sum = p[0] + p[1]
+ return ( p[1] + p[2] + p[3] == sum &&
+ p[3] + p[4] + p[5] == sum &&
+ p[5] + p[6] == sum )
+}
+
+fun main(args: Array<String>) {
+ val input = listOf(1, 2, 3, 4, 5, 6, 7)
+ val perms = permute(input)
+ for (perm in perms) {
+ if (check_squares(perm)) println(perm)
+ }
+}
diff --git a/challenge-109/laurent-rosenfeld/lua/ch-2.lua b/challenge-109/laurent-rosenfeld/lua/ch-2.lua
new file mode 100644
index 0000000000..e972de6a62
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/lua/ch-2.lua
@@ -0,0 +1,20 @@
+local function check_square (p)
+ sum = p[1] + p[2]
+ return p[2] + p[3] + p[4] == sum and
+ p[4] + p[5] + p[6] == sum and
+ p[6] + p[7] == sum
+end
+
+local function permute(perm, n)
+ if n == 0 and check_square(perm) then
+ print( table.concat(perm, ' ') )
+ else
+ for i = 1, n do
+ perm[i], perm[n] = perm[n], perm[i]
+ permute(perm, n - 1)
+ perm[i], perm[n] = perm[n], perm[i]
+ end
+ end
+end
+
+permute({1,2,3,4,5,6,7}, 7)
diff --git a/challenge-109/laurent-rosenfeld/nim/ch-2.nim b/challenge-109/laurent-rosenfeld/nim/ch-2.nim
new file mode 100644
index 0000000000..129ac8a3ce
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/nim/ch-2.nim
@@ -0,0 +1,12 @@
+import algorithm
+
+proc check_squares (p: array[0..6, int]) : bool =
+ var sum = p[0] + p[1]
+ return ( p[1] + p[2] + p[3] == sum and
+ p[3] + p[4] + p[5] == sum and p[5] + p[6] == sum )
+
+
+var input = [1, 2, 3, 4, 5, 6, 7] # List has to start sorted
+while input.nextPermutation():
+ if check_squares(input):
+ echo input
diff --git a/challenge-109/laurent-rosenfeld/python/ch-2.py b/challenge-109/laurent-rosenfeld/python/ch-2.py
new file mode 100644
index 0000000000..95138e89c6
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/python/ch-2.py
@@ -0,0 +1,6 @@
+import itertools
+
+input = range(1, 8) # 1 to 7
+for p in itertools.permutations(input):
+ if sum(p[0:2]) == sum(p[1:4]) == sum(p[3:6]) == sum(p[5:7]):
+ print (perm)
diff --git a/challenge-109/laurent-rosenfeld/ruby/ch-2.rb b/challenge-109/laurent-rosenfeld/ruby/ch-2.rb
new file mode 100644
index 0000000000..0a7fc1d4ba
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/ruby/ch-2.rb
@@ -0,0 +1,9 @@
+input = [1, 2, 3, 4, 5, 6, 7]
+for p in input.permutation
+ sum = p[0] + p[1]
+ if p[1] + p[2] + p[3] == sum and
+ p[3] + p[4] + p[5] == sum and
+ p[5] + p[6] == sum then
+ puts p.join(" ")
+ end
+end
diff --git a/challenge-109/laurent-rosenfeld/scala/ch-2.scala b/challenge-109/laurent-rosenfeld/scala/ch-2.scala
new file mode 100644
index 0000000000..f033cde350
--- /dev/null
+++ b/challenge-109/laurent-rosenfeld/scala/ch-2.scala
@@ -0,0 +1,14 @@
+object fourSquares extends App {
+ val in = (1 to 7).toList
+ for (perm <- in.permutations) {
+ val sum = perm.slice(0, 2).sum;
+ // In Scala, slice(0, 2) returns items 0 and 1
+ if (
+ perm.slice(1, 4).sum == sum &&
+ perm.slice(3, 6).sum == sum &&
+ perm.slice(5, 7).sum == sum
+ ) {
+ println(perm.mkString(" "))
+ }
+ }
+}