Skip to content

Instantly share code, notes, and snippets.

@pomtom
Created May 30, 2018 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pomtom/0f8b509b8f1785191dcc550169997627 to your computer and use it in GitHub Desktop.
Save pomtom/0f8b509b8f1785191dcc550169997627 to your computer and use it in GitHub Desktop.
Algorithm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlackLight.Recruitment.UnitTests
{
class Algorithms
{
public static string ReverseWordsInString(string input)
{
string reversalstring = string.Empty;
if (string.IsNullOrWhiteSpace(input))
{
return null;
}
try
{
string[] splitedstring = input.Split(' ');
Array.Reverse(splitedstring);
for (int i = 0; i <= splitedstring.Length - 1; i++)
{
reversalstring += splitedstring[i] + "" + ' ';
}
reversalstring = reversalstring.TrimEnd(' ');
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return reversalstring;
}
public static string ReverseWordsInStringWithoutUsingAnyLinqMethods(string input)
{
string reversalstring = string.Empty;
if (string.IsNullOrWhiteSpace(input))
{
return null;
}
try
{
int counter;
string[] splitedstring = input.Split(' ');
int totalindex = splitedstring.Length - 1;
counter = totalindex;
for (int i = totalindex; counter >= 0; totalindex--)
{
reversalstring += splitedstring[counter] + "" + ' ';
--counter;
}
reversalstring = reversalstring.TrimEnd(' ');
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return reversalstring;
}
internal static int[] SortWithoutUsingBuiltInSortMethods(int[] input)
{
int[] reversalNumber = new int[12];
int cnt = input.Length - 1;
for (int i = 0; i < cnt; i++)
{
for (int j = cnt; j > i; j--)
{
if (((IComparable)input[j - 1]).CompareTo(input[j]) > 0)
{
var temp = input[j - 1];
input[j - 1] = input[j];
input[j] = temp;
}
}
}
for (int i = 0; i < input.Length; i++)
{
reversalNumber[i] = input[i];
}
return reversalNumber;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment