gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
expanded sample size, first version
[mjc2wsl.git] / samples / alpha-mj-exp / Parentheses.mj
diff --git a/samples/alpha-mj-exp/Parentheses.mj b/samples/alpha-mj-exp/Parentheses.mj
new file mode 100644 (file)
index 0000000..d691ac7
--- /dev/null
@@ -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
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner