exemple: add zig-zag_matrix task of Rosetta code
authorArthur Delamare <arthur.delamare@viacesi.fr>
Wed, 10 Jun 2015 19:37:45 +0000 (15:37 -0400)
committerArthur Delamare <arthur.delamare@viacesi.fr>
Mon, 15 Jun 2015 02:25:04 +0000 (22:25 -0400)
Signed-off-by: Arthur Delamare <arthur.delamare@viacesi.fr>

examples/rosettacode/zigzag_matrix.nit [new file with mode: 0644]
tests/sav/zigzag_matrix.res [new file with mode: 0644]

diff --git a/examples/rosettacode/zigzag_matrix.nit b/examples/rosettacode/zigzag_matrix.nit
new file mode 100644 (file)
index 0000000..f671392
--- /dev/null
@@ -0,0 +1,76 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+#
+# Task: Zig-zag matrix
+# SEE: <http://rosettacode.org/wiki/Zig-zag_matrix>
+
+fun zig_zag(number: Int): Array[Array[Int]] do
+       var first_number = 0
+       var last_number = number * number - 1
+       # x: line position
+       # y: column position
+       var x = 0
+       var y = 0
+       # true: top-right
+       # false: bottom-left
+       var trajectory = true
+       # first array: column
+       # second array: line
+       var array = new Array[Array[Int]]
+       for i in [0..number[ do array.add(new Array[Int].filled_with(0, number))
+
+       while first_number <= last_number do
+               array[y][x] = first_number
+               array[number - y - 1][number - x - 1] = last_number
+
+               first_number += 1
+               last_number -= 1
+
+               if x == 0 and y == 0 then
+                       x += 1
+                       trajectory = false
+               else
+                       if y == 0 and trajectory then
+                               trajectory = false
+                               x += 1
+                               continue
+                       end
+                       if y == 0 and not trajectory then
+                               x -= 1
+                               y += 1
+                               continue
+                       end
+                       if x == 0 and not trajectory then
+                               trajectory = true
+                               y += 1
+                               continue
+                       end
+                       if x == 0 and trajectory then
+                               x += 1
+                               y -= 1
+                               continue
+                       end
+                       if trajectory then
+                               x += 1
+                               y -= 1
+                       else
+                               x -= 1
+                               y += 1
+                       end
+               end
+       end
+       return array
+end
+
+var number = 10
+var number_length_max = (number * number - 1).to_s.length
+for line in zig_zag(number) do
+       for element in line do
+               printn element
+               var blank = number_length_max - element.to_s.length + 1
+               for x in blank.times do printn " "
+       end
+       print ""
+end
diff --git a/tests/sav/zigzag_matrix.res b/tests/sav/zigzag_matrix.res
new file mode 100644 (file)
index 0000000..3264ff7
--- /dev/null
@@ -0,0 +1,10 @@
+0  1  5  6  14 15 27 28 44 45 
+2  4  7  13 16 26 29 43 46 63 
+3  8  12 17 25 30 42 47 62 64 
+9  11 18 24 31 41 48 61 65 78 
+10 19 23 32 40 49 60 66 77 79 
+20 22 33 39 50 59 67 76 80 89 
+21 34 38 51 58 68 75 81 88 90 
+35 37 52 57 69 74 82 87 91 96 
+36 53 56 70 73 83 86 92 95 97 
+54 55 71 72 84 85 93 94 98 99