X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=samples%2Falpha-mj-exp%2FParentheses.mj;fp=samples%2Falpha-mj-exp%2FParentheses.mj;h=d691ac7dfa7892acbcdb5db5813df56ba0c241e1;hp=0000000000000000000000000000000000000000;hb=1601c2c11f9f93dbb8c6d64c9e584c5dd42f79c6;hpb=9d93fc4b07c1f0d93ad47580dc7e507cea267709 diff --git a/samples/alpha-mj-exp/Parentheses.mj b/samples/alpha-mj-exp/Parentheses.mj new file mode 100644 index 0000000..d691ac7 --- /dev/null +++ b/samples/alpha-mj-exp/Parentheses.mj @@ -0,0 +1,44 @@ +program Parentheses +{ + // generate all possibilities of n pairs of parentheses + // so that they are valid (open - closed) + + void parentheses(char[] solution, int pos, int left, int right, int num) + int i; + { + if (right == num) { + i = 0; + while (i < len(solution)) { + print(solution[i]); + print(' '); + i++; + } + print('\n'); + } + if (left < num) { + solution[pos] = '('; + parentheses(solution, pos+1, left+1, right, num); + } + if (left > right) { + solution[pos] = ')'; + parentheses(solution, pos+1, left, right+1, num); + } + } + + void pars(char[] sln, int num) { + if (num > 0) { + parentheses(sln, 0, 0, 0, num); + } + } + + void main() + int i, dimension; + char[] solution; + { + print('d'); + print('?'); + read(dimension); + solution = new char[2*dimension]; + pars(solution, dimension); + } +} \ No newline at end of file