Skip to content

Commit 911d6f4

Browse files
committed
fix c# runner
1 parent bc1ad22 commit 911d6f4

File tree

8 files changed

+141
-79
lines changed

8 files changed

+141
-79
lines changed

algorithms/bit-manipulation/xor-swap/csharp/XorSwap.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
using System;
22

3-
public class XorSwap
3+
public static class XorSwap
44
{
5-
void Swap( ref int a, ref int b)
6-
{
7-
a ^= b;
8-
b ^= a;
9-
a ^= b;
10-
}
5+
public static int[] Swap(int a, int b)
6+
{
7+
if (a != b)
8+
{
9+
a ^= b;
10+
b ^= a;
11+
a ^= b;
12+
}
13+
14+
return new[] { a, b };
15+
}
1116

12-
void Main()
17+
public static void Main(string[] args)
1318
{
14-
int a = 5;
15-
int b = 10;
16-
XorSwap(ref a, ref b);
19+
Console.WriteLine(string.Join(" ", Swap(5, 10)));
1720
}
1821
}
1922

20-

algorithms/dynamic-programming/digit-dp/csharp/DigitDp.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ static int CountDigitDp(int n, int target) {
4646
for (int k = 0; k < 2; k++)
4747
memo[i, j, k] = -1;
4848

49-
return Solve(0, 0, 1);
49+
int count = Solve(0, 0, 1);
50+
if (target == 0) {
51+
count--;
52+
}
53+
return count;
5054
}
5155

5256
static void Main(string[] args) {
Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,72 @@
11
using System;
2+
using System.Collections.Generic;
23

34
public class DelaunayTriangulation
45
{
5-
public static int Compute(int[] arr)
6+
private struct Point
67
{
7-
int n = arr[0];
8-
if (n < 3) return 0;
8+
public int X;
9+
public int Y;
910

10-
double[] px = new double[n], py = new double[n];
11-
for (int i = 0; i < n; i++)
11+
public Point(int x, int y)
1212
{
13-
px[i] = arr[1 + 2 * i];
14-
py[i] = arr[1 + 2 * i + 1];
13+
X = x;
14+
Y = y;
1515
}
16+
}
1617

17-
double EPS = 1e-9;
18-
int count = 0;
18+
private static long Cross(Point o, Point a, Point b)
19+
{
20+
return (long)(a.X - o.X) * (b.Y - o.Y) - (long)(a.Y - o.Y) * (b.X - o.X);
21+
}
1922

20-
for (int i = 0; i < n; i++)
23+
private static int ConvexHullVertexCount(Point[] points)
24+
{
25+
if (points.Length <= 1) return points.Length;
26+
27+
Array.Sort(points, (a, b) =>
2128
{
22-
for (int j = i + 1; j < n; j++)
29+
int cmp = a.X.CompareTo(b.X);
30+
return cmp != 0 ? cmp : a.Y.CompareTo(b.Y);
31+
});
32+
33+
var hull = new List<Point>();
34+
35+
foreach (Point p in points)
36+
{
37+
while (hull.Count >= 2 && Cross(hull[hull.Count - 2], hull[hull.Count - 1], p) <= 0)
2338
{
24-
for (int k = j + 1; k < n; k++)
25-
{
26-
double ax = px[i], ay = py[i];
27-
double bx = px[j], by = py[j];
28-
double cx = px[k], cy = py[k];
29-
30-
double d = 2.0 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
31-
if (Math.Abs(d) < EPS) continue;
32-
33-
double ux = ((ax*ax + ay*ay) * (by - cy) +
34-
(bx*bx + by*by) * (cy - ay) +
35-
(cx*cx + cy*cy) * (ay - by)) / d;
36-
double uy = ((ax*ax + ay*ay) * (cx - bx) +
37-
(bx*bx + by*by) * (ax - cx) +
38-
(cx*cx + cy*cy) * (bx - ax)) / d;
39-
40-
double rSq = (ux - ax) * (ux - ax) + (uy - ay) * (uy - ay);
41-
42-
bool valid = true;
43-
for (int m = 0; m < n; m++)
44-
{
45-
if (m == i || m == j || m == k) continue;
46-
double distSq = (ux - px[m]) * (ux - px[m]) + (uy - py[m]) * (uy - py[m]);
47-
if (distSq < rSq - EPS) { valid = false; break; }
48-
}
49-
50-
if (valid) count++;
51-
}
39+
hull.RemoveAt(hull.Count - 1);
5240
}
41+
hull.Add(p);
42+
}
43+
44+
int lowerCount = hull.Count;
45+
for (int i = points.Length - 2; i >= 0; i--)
46+
{
47+
Point p = points[i];
48+
while (hull.Count > lowerCount && Cross(hull[hull.Count - 2], hull[hull.Count - 1], p) <= 0)
49+
{
50+
hull.RemoveAt(hull.Count - 1);
51+
}
52+
hull.Add(p);
53+
}
54+
55+
return Math.Max(0, hull.Count - 1);
56+
}
57+
58+
public static int Compute(int[] arr)
59+
{
60+
int n = arr[0];
61+
if (n < 3) return 0;
62+
63+
Point[] points = new Point[n];
64+
for (int i = 0; i < n; i++)
65+
{
66+
points[i] = new Point(arr[1 + 2 * i], arr[1 + 2 * i + 1]);
5367
}
5468

55-
return count;
69+
int hullVertices = ConvexHullVertexCount(points);
70+
return (2 * n) - 2 - hullVertices;
5671
}
5772
}

algorithms/math/mobius-function/csharp/MobiusFunction.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,43 @@ public class MobiusFunction
44
{
55
public static int MobiusFunctionSum(int n)
66
{
7+
if (n <= 0) return 0;
8+
79
int[] mu = new int[n + 1];
8-
mu[1] = 1;
9-
bool[] isPrime = new bool[n + 1];
10-
Array.Fill(isPrime, true);
10+
bool[] isComposite = new bool[n + 1];
11+
for (int i = 1; i <= n; i++)
12+
{
13+
mu[i] = 1;
14+
}
15+
mu[0] = 0;
1116

1217
for (int i = 2; i <= n; i++)
1318
{
14-
if (isPrime[i])
19+
if (!isComposite[i])
1520
{
21+
for (int j = i * 2; j <= n; j += i)
22+
{
23+
isComposite[j] = true;
24+
}
25+
1626
for (int j = i; j <= n; j += i)
1727
{
18-
if (j != i) isPrime[j] = false;
19-
mu[j] = -mu[j];
28+
mu[j] *= -1;
2029
}
30+
2131
long i2 = (long)i * i;
2232
for (long j = i2; j <= n; j += i2)
33+
{
2334
mu[(int)j] = 0;
35+
}
2436
}
2537
}
38+
2639
int sum = 0;
27-
for (int i = 1; i <= n; i++) sum += mu[i];
40+
for (int i = 1; i <= n; i++)
41+
{
42+
sum += mu[i];
43+
}
2844
return sum;
2945
}
3046

algorithms/sorting/gnome-sort/csharp/GnomeSort.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public static int[] Sort(int[] arr)
1818

1919
int[] result = (int[])arr.Clone();
2020
int n = result.Length;
21+
if (n < 2)
22+
{
23+
return result;
24+
}
2125
int index = 0;
2226

2327
while (index < n)

algorithms/trees/binary-tree/csharp/BinaryTree.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,36 @@ static TreeNode BuildTree(int?[] arr)
1414
{
1515
if (arr.Length == 0 || arr[0] == null) return null;
1616

17-
var root = new TreeNode(arr[0].Value);
18-
var queue = new Queue<TreeNode>();
19-
queue.Enqueue(root);
20-
int i = 1;
17+
var nodes = new TreeNode[arr.Length];
18+
for (int i = 0; i < arr.Length; i++)
19+
{
20+
if (arr[i] != null)
21+
{
22+
nodes[i] = new TreeNode(arr[i].Value);
23+
}
24+
}
2125

22-
while (queue.Count > 0 && i < arr.Length)
26+
for (int i = 0; i < arr.Length; i++)
2327
{
24-
var node = queue.Dequeue();
25-
if (i < arr.Length && arr[i] != null)
28+
if (nodes[i] == null)
29+
{
30+
continue;
31+
}
32+
33+
int leftIndex = (2 * i) + 1;
34+
int rightIndex = (2 * i) + 2;
35+
36+
if (leftIndex < arr.Length)
2637
{
27-
node.Left = new TreeNode(arr[i].Value);
28-
queue.Enqueue(node.Left);
38+
nodes[i].Left = nodes[leftIndex];
2939
}
30-
i++;
31-
if (i < arr.Length && arr[i] != null)
40+
if (rightIndex < arr.Length)
3241
{
33-
node.Right = new TreeNode(arr[i].Value);
34-
queue.Enqueue(node.Right);
42+
nodes[i].Right = nodes[rightIndex];
3543
}
36-
i++;
3744
}
38-
return root;
45+
46+
return nodes[0];
3947
}
4048

4149
static List<int> LevelOrderTraversal(int?[] arr)

algorithms/trees/tree-diameter/csharp/TreeDiameter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public static int Solve(int[] arr)
1414
int m = (arr.Length - 1) / 2;
1515
for (int i = 0; i < m; i++)
1616
{
17-
int u = arr[idx++], v = arr[idx++];
18-
adj[u].Add(v); adj[v].Add(u);
17+
int from = arr[idx++], to = arr[idx++];
18+
adj[from].Add(to);
19+
adj[to].Add(from);
1920
}
2021

2122
(int farthest, int dist) Bfs(int start)
@@ -42,8 +43,8 @@ public static int Solve(int[] arr)
4243
return (far, d[far]);
4344
}
4445

45-
var (u, _) = Bfs(0);
46-
var (_, diameter) = Bfs(u);
46+
var (startNode, _) = Bfs(0);
47+
var (_, diameter) = Bfs(startNode);
4748
return diameter;
4849
}
4950

tests/runners/csharp_runner.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ def normalized_top_level_inputs(raw):
154154
ordered_keys = list(raw.keys())
155155
return [raw[key] for key in ordered_keys]
156156
if isinstance(raw, list):
157+
if (
158+
len(inputs) == 1
159+
and all(not isinstance(item, (list, dict)) for item in raw)
160+
and any(token in str(inputs[0]) for token in ("array", "list", "matrix", "grid", "tree", "points", "interval"))
161+
):
162+
return [raw]
157163
return raw
158164
return [raw]
159165
@@ -577,6 +583,12 @@ def normalized_top_level_inputs(raw, ordered_keys):
577583
keys = list(raw.keys())
578584
return [raw[key] for key in keys]
579585
if isinstance(raw, list):
586+
if (
587+
len(ordered_keys) == 1
588+
and all(not isinstance(item, (list, dict)) for item in raw)
589+
and any(token in str(ordered_keys[0]) for token in ("array", "list", "matrix", "grid", "tree", "points", "interval"))
590+
):
591+
return [raw]
580592
return raw
581593
return [raw]
582594

0 commit comments

Comments
 (0)