From: Jean Privat Date: Thu, 7 May 2015 01:56:08 +0000 (-0400) Subject: Merge: Balanced brackets X-Git-Tag: v0.7.5~76 X-Git-Url: http://nitlanguage.org?hp=ace6ea4a957ca05aa6ace7e928973b6a158ae74c Merge: Balanced brackets 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 Reviewed-by: Lucas Bajolet --- diff --git a/examples/rosettacode/balanced_brackets.nit b/examples/rosettacode/balanced_brackets.nit new file mode 100644 index 0000000..a1ae476 --- /dev/null +++ b/examples/rosettacode/balanced_brackets.nit @@ -0,0 +1,39 @@ +#!/usr/bin/env nit +# +# This file is part of NIT ( http://www.nitlanguage.org ). +# This program is public domain + +# Task: Balanced brackets +# SEE: +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 diff --git a/lib/mnit/mnit_injected_input.nit b/lib/mnit/mnit_injected_input.nit index 4d6ddfa..da2e33d 100644 --- a/lib/mnit/mnit_injected_input.nit +++ b/lib/mnit/mnit_injected_input.nit @@ -20,7 +20,7 @@ # 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. # @@ -75,13 +75,6 @@ redef class App 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) diff --git a/lib/standard/environ.nit b/lib/standard/environ.nit index 2f6ae46..0abe7f3 100644 --- a/lib/standard/environ.nit +++ b/lib/standard/environ.nit @@ -60,3 +60,11 @@ redef class NativeString 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 diff --git a/lib/standard/math.nit b/lib/standard/math.nit index 2b55dca..9e1744f 100644 --- a/lib/standard/math.nit +++ b/lib/standard/math.nit @@ -241,6 +241,11 @@ 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 @@ -252,6 +257,19 @@ redef class Collection[ E ] 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] @@ -263,6 +281,36 @@ 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 diff --git a/tests/sav/balanced_brackets.res b/tests/sav/balanced_brackets.res new file mode 100644 index 0000000..ea08659 --- /dev/null +++ b/tests/sav/balanced_brackets.res @@ -0,0 +1,10 @@ +][[]][ 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 diff --git a/tests/tests.sh b/tests/tests.sh index 39e0cbd..2769eba 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -21,7 +21,7 @@ export LANG=C export LC_ALL=C export NIT_TESTING=true -export MNIT_SRAND=0 +export NIT_SRAND=0 unset NIT_DIR