yair.cohen1
BAN USERpublic static string NumberToReadString(int number)
{
string input = "1";
string tempResult = null;
if (number < 0)
{
throw new ArgumentException("Invalid Argument");
}
for (int i = 0; i < number; i++)
{
tempResult = ReadString(input);
input = tempResult;
}
return input;
}
public static string ReadString(string str)
{
if (String.IsNullOrEmpty(str) || String.IsNullOrWhiteSpace(str))
{
throw new ArgumentException("Invalid Argument");
}
int index = 0, count = 1;
StringBuilder result = new StringBuilder();
char curr, next;
for (index = 0; index < str.Length - 1; index++)
{
curr = str[index];
next = str[index + 1];
if (curr != next)
{
result.AppendFormat("{0}{1}", count, curr);
count = 1;
}
else
{
count++;
}
}
result.AppendFormat("{0}{1}", count, str[index]);
return result.ToString();
}
public static bool HasPath(int[] arr)
{
int idx = 0;
while (true)
{
int hop = arr[idx];
if (hop == 0)
return false;
idx += hop;
if (idx > (arr.Length - 1))
return true;
while (arr[idx] == 0)
idx--;
if (hop == arr[idx])
return false;
}
}
public static List<int> FindMissing(int[] arr, int left, int right)
{
List<int> result = new List<int>();
if ((right - left) == 1)
{
if (Math.Abs(arr[right] - arr[left]) > 1)
{
int step = Math.Sign(arr[right] - arr[left]);//left>right returns -1 left<right returns 1
int missingNum = arr[left] + step;
do
{
result.Add(missingNum);
}
while ((missingNum+=step) != arr[right]);
}
return result;
}
int middle = (left + right) / 2;
result.AddRange(FindMissing(arr, left, middle));
result.AddRange(FindMissing(arr, middle, right));
return result;
}
- yair.cohen1 December 15, 2014