Skip to content

Commit

Permalink
Add SortTheOdd
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslieu committed Dec 22, 2021
1 parent 00ad9ba commit 7e6cceb
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
27 changes: 27 additions & 0 deletions SortTheOdd/SortTheOdd.Tests/SortTheOdd.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SortTheOdd\SortTheOdd.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions SortTheOdd/SortTheOdd.Tests/SortTheOddTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;

namespace SortTheOdd.Tests
{
public class SortTheOddTests
{
[Fact]
public void Test_Sort1()
{
Assert.Equal(new int[] { 1, 3, 2, 8, 5, 4 }, SortTheOdd.Sort(new int[] { 5, 3, 2, 8, 1, 4 }));
Assert.Equal(new int[] { 1, 3, 5, 8, 0 }, SortTheOdd.Sort(new int[] { 5, 3, 1, 8, 0 }));
Assert.Equal(new int[] { }, SortTheOdd.Sort(new int[] { }));
}
}
}
31 changes: 31 additions & 0 deletions SortTheOdd/SortTheOdd.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31912.275
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SortTheOdd", "SortTheOdd\SortTheOdd.csproj", "{5431BAA1-1C2C-4333-8BD3-D41E96C23E93}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SortTheOdd.Tests", "SortTheOdd.Tests\SortTheOdd.Tests.csproj", "{9073AF51-5E90-40E4-8811-8EFE091919AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5431BAA1-1C2C-4333-8BD3-D41E96C23E93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5431BAA1-1C2C-4333-8BD3-D41E96C23E93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5431BAA1-1C2C-4333-8BD3-D41E96C23E93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5431BAA1-1C2C-4333-8BD3-D41E96C23E93}.Release|Any CPU.Build.0 = Release|Any CPU
{9073AF51-5E90-40E4-8811-8EFE091919AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9073AF51-5E90-40E4-8811-8EFE091919AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9073AF51-5E90-40E4-8811-8EFE091919AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9073AF51-5E90-40E4-8811-8EFE091919AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B7496AFD-5BDE-4968-A422-6294804995F5}
EndGlobalSection
EndGlobal
33 changes: 33 additions & 0 deletions SortTheOdd/SortTheOdd/SortTheOdd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace SortTheOdd
{
/// <summary>
/// You will be given an array of numbers.
/// You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.
/// </summary>
public class SortTheOdd
{
public static int[] Sort(int[] arr)
{
if (arr.Length == 0) { return arr; }
var odds = new List<int>();
var index = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % 2 != 0)
{
odds.Add(arr[i]);
index.Add(i);
}
}

var oddsArr = odds.ToArray();
Array.Sort(oddsArr);

for (int i = 0; i < index.Count; i++)
{
arr[index[i]] = oddsArr[i];
}
return arr;
}
}
}
9 changes: 9 additions & 0 deletions SortTheOdd/SortTheOdd/SortTheOdd.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 7e6cceb

Please sign in to comment.