-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path008 - Sort the odd.cs
35 lines (31 loc) · 1.01 KB
/
008 - Sort the odd.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
using System;
using System.Linq;
// Sort the odd
// C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /debug+ "008 - Sort the odd.cs"
public class Kata
{
public static int[] SortArray(int[] array)
{
if (array.Length == 0)
return new int[] {};
var subset = array.Where(num => (num % 2) != 0).OrderBy(num => num).Select(num => num).ToArray();
int[] res = (int[])Array.CreateInstance(typeof(int), array.Length);
for (int i = 0, j = 0; i < array.Length; i++)
{
if ((array[i] % 2) == 0)
res[i] = array[i];
else
res[i] = subset[j++];
}
return res;
}
}
static class Program
{
static void Main()
{
Console.WriteLine("[{0}]", string.Join(",", Kata.SortArray(new int[] { 5, 3, 2, 8, 1, 4 }).Select(num => num.ToString()).ToArray()));
Console.WriteLine("[{0}]", string.Join(",", Kata.SortArray(new int[] { 5, 3, 1, 8, 0 }).Select(num => num.ToString()).ToArray()));
Console.WriteLine("[{0}]", string.Join(",", Kata.SortArray(new int[] { }).Select(num => num.ToString()).ToArray()));
}
}