-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecurse2PoweredN.cs
100 lines (92 loc) · 3.09 KB
/
Recurse2PoweredN.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms
{
/// <summary>
/// Recurse2PoweredN
/// MakeAllBinaries
/// MakeAllBinariesWhereLevelNotZero
/// MakeBracketVariants
/// </summary>
internal class Recurse2PoweredN
{
internal static void Run()
{
//MakeAllBinaries(2);
MakeAllBinaries(4);
//MakeAllBinaries(12);
//MakeAllBinaries(22);
MakeAllBinariesWhereLevelNotZero(4, 2);
MakeAllBinariesWhereLevelNotZero(5, 4);
MakeBracketVariants(3);
}
private static void MakeAllBinaries(int n)
{
var res = new List<string>();
MakeAllBinariesRecurse(n, res);
Console.WriteLine("For " + n + ", count = " + res.Count);
foreach (var item in res)
Console.WriteLine(" " + item);
}
private static void MakeAllBinariesWhereLevelNotZero(int n, int level)
{
var res = new List<string>();
MakeAllBinariesWhereLevelNotZeroRecurse(n, res, level);
Console.WriteLine("For " + n + ", count = " + res.Count);
//foreach (var item in res)
// Console.WriteLine(" " + item);
}
private static void MakeBracketVariants(int n)
{
var res = new List<string>();
MakeBracketVariantsRecurse(n, n, res);
Console.WriteLine("For " + n + ", count = " + res.Count);
foreach (var item in res)
Console.WriteLine(" " + item);
}
/* RECURSE */
private static void MakeAllBinariesRecurse(int n, List<string> res, string acc = "")
{
if (n == 0)
{
res.Add(acc);
return;
}
{
MakeAllBinariesRecurse(n - 1, res, acc + '0');
MakeAllBinariesRecurse(n - 1, res, acc + "1");
}
}
private static void MakeAllBinariesWhereLevelNotZeroRecurse(int n, List<string> res,
int levelWithoutZero, string acc = "")
{
if (n == 0)
{
res.Add(acc);
return;
}
{
if (n-1 != levelWithoutZero)
MakeAllBinariesWhereLevelNotZeroRecurse(n - 1, res, levelWithoutZero, acc + '0');
MakeAllBinariesWhereLevelNotZeroRecurse(n - 1, res, levelWithoutZero, acc + "1");
}
}
private static void MakeBracketVariantsRecurse(int nopen, int nclose, List<string> res, string acc = "")
{
if (nopen == 0 && nclose == 0)
{
res.Add(acc);
return;
}
{
if (nopen > 0)
MakeBracketVariantsRecurse(nopen - 1, nclose, res, acc + '{');
if (nclose > 0 && nclose > nopen)
MakeBracketVariantsRecurse(nopen, nclose - 1, res, acc + "}");
}
}
}
}