The PR started as a simple implementation for the task http://rosettacode.org/wiki/Balanced_brackets but some improvements on the lib and tests.sh where also added
Pull-Request: #1307
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
--- /dev/null
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Balanced brackets
+# SEE: <http://rosettacode.org/wiki/Balanced_brackets>
+module balanced_brackets
+
+# Are `[` and `]` balanced?
+# Other characters are ignored.
+#
+# assert is_balanced("[][[]]")
+# assert is_balanced("")
+# assert not is_balanced("[[]")
+# assert not is_balanced("][][")
+fun is_balanced(s: String): Bool
+do
+ var l = 0
+ for x in s.chars do
+ if x == '[' then
+ l += 1
+ else if x == ']' then
+ l -= 1
+ if l < 0 then return false
+ end
+ end
+ return l == 0
+end
+
+var n = 3
+if args.not_empty then n = args.first.to_i
+
+for i in [0..10[ do
+ var a = (['[', ']'] * n)
+ a.shuffle
+ var b = a.join("")
+ if is_balanced(b) then print "{b} is well-balanced" else print "{b} is not well-balanced"
+end
# In order to reproduce executions, the behavior of the application must be deterministic
# for a given sequence of inputs.
# The main source of differences in executions is caused by the `rand` function,
-# Set the environment variable `MNIT_SRAND` to a value to force srand to be initialized with this value.
+# Set the environment variable `NIT_SRAND` to a value to force srand to be initialized with this value.
#
# The input event file is made of event descriptions, one event by line.
#
redef fun setup
do
- var env = "MNIT_SRAND".environ
- if env != "" then
- srand_from(env.to_i)
- else
- srand_from(0)
- end
-
var input = "MNIT_READ_INPUT".environ
if input != "" then
injected_input_stream = new FileReader.open(input)
private fun get_environ: NativeString is extern "string_NativeString_NativeString_get_environ_0"
private fun setenv( v : NativeString ) is extern "string_NativeString_NativeString_setenv_1"
end
+
+redef class Sys
+ redef init
+ do
+ var x = "NIT_SRAND".environ
+ if x != "" then srand_from(x.to_i)
+ end
+end
redef class Collection[ E ]
# Return a random element form the collection
# There must be at least one element in the collection
+ #
+ # ~~~
+ # var x = [1,2,3].rand
+ # assert x == 1 or x == 2 or x == 3
+ # ~~~
fun rand: E
do
if is_empty then abort
end
abort
end
+
+ # Return a new array made of elements in a random order.
+ #
+ # ~~~
+ # var a = [1,2,1].to_shuffle
+ # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
+ # ~~~
+ fun to_shuffle: Array[E]
+ do
+ var res = self.to_a
+ res.shuffle
+ return res
+ end
end
redef class SequenceRead[E]
end
end
+redef class AbstractArray[E]
+ # Reorder randomly the elements in self.
+ #
+ # ~~~
+ # var a = new Array[Int]
+ #
+ # a.shuffle
+ # assert a.is_empty
+ #
+ # a.add 1
+ # a.shuffle
+ # assert a == [1]
+ #
+ # a.add 2
+ # a.shuffle
+ # assert a == [1,2] or a == [2,1]
+ # ~~~
+ #
+ # ENSURE self.shuffle.has_exactly(old(self))
+ fun shuffle
+ do
+ for i in [0..length[ do
+ var j = i + (length-i).rand
+ var tmp = self[i]
+ self[i] = self[j]
+ self[j] = tmp
+ end
+ end
+end
+
redef class Sys
init
do
--- /dev/null
+][[]][ is not well-balanced
+[[]][] is well-balanced
+[]][][ is not well-balanced
+[[[]]] is well-balanced
+[][]][ is not well-balanced
+]][[][ is not well-balanced
+[]]][[ is not well-balanced
+[]][][ is not well-balanced
+[][][] is well-balanced
+][]][[ is not well-balanced
export LANG=C
export LC_ALL=C
export NIT_TESTING=true
-export MNIT_SRAND=0
+export NIT_SRAND=0
unset NIT_DIR