allanchanly
BAN USERclass BinaryOddAI
{
public static void Do()
{
int[] array = { 0, 0, 1, 2, 2 };
Console.WriteLine(GetOdd(array, 0, array.Length - 1));
int[] array2 = { 0, 0, 1, 1, 3, 2, 2 };
Console.WriteLine(GetOdd(array2, 0, array2.Length - 1));
int[] array3 = { 1, 1, 3 };
Console.WriteLine(GetOdd(array3, 0, array3.Length - 1));
}
private static int GetOdd(int[] array, int start, int end)
{
if (array == null || array.Length % 2 == 0)
{
return -1;
}
if (array.Length == 1)
{
return array[0];
}
if (start == end)
{
return array[start];
}
int mid = (start + end) / 2;
if (array[mid] != array[mid - 1] && array[mid] != array[mid + 1])
{
return array[mid];
}
if (array[mid] == array[mid + 1])
{
return GetOdd(array, start, mid - 1);
}
else
{
return GetOdd(array, mid + 1, end);
}
}
}
I like your approach
- allanchanly October 21, 2013class PermutationCombinationAI
{
public static void Do()
{
List<char> input = new List<char>() { 'a','b','c' };
List<List<char>> container = new List<List<char>>();
SimplePowerSet(input, container);
foreach (List<char> set in container)
{
Utils.PrintGList<char>(set);
}
}
private static void SimplePowerSet(List<char> input, List<List<char>> container)
{
List<char> temp = new List<char>();
temp.AddRange(input);
container.Add(temp);
if (temp.Count > 1)
{
List<char> temp2 = new List<char>();
for (int i = 0; i < temp.Count; i++)
{
temp2 = new List<char>();
temp2.AddRange(temp);
temp2.RemoveAt(i);
SimplePowerSet(temp2, container);
}
}
}
}
class AnagramAI
{
public static void Do()
{
List<string> inputs = new List<string>() { "star", "rats", "ice", "cie", "arts" };
AnagramBuckets(inputs);
}
public static void AnagramBuckets(List<string> input)
{
List<string> processed = new List<string>();
List<List<string>> buckets = new List<List<string>>();
List<string> anagrams = null;
for (int i = 0; i < input.Count; i++)
{
//Console.Write("{0}", input[i]);
if (processed.Contains(input[i]))
{
continue;
}
else
{
anagrams = new List<string>();
anagrams.Add(input[i]);
processed.Add(input[i]);
}
for (int j = i + 1; j < input.Count; j++)
{
if (!processed.Contains(input[j]) && IsAnagram(input[i], input[j]))
{
//Console.Write(",{0}", input[j]);
anagrams.Add(input[j]);
processed.Add(input[j]);
}
}
//Console.WriteLine();
buckets.Add(anagrams);
}
foreach (List<string> buck in buckets)
{
foreach (string s in buck)
{
Console.Write("{0},", s);
}
Console.WriteLine();
}
}
private static bool IsAnagram(string a, string b)
{
if (a.Length == b.Length)
{
char[] charas = a.ToCharArray();
Array.Sort<char>(charas);
char[] charbs = b.ToCharArray();
Array.Sort<char>(charbs);
for (int i = 0; i < charas.Length; i++)
{
if (charas[i] != charbs[i])
{
return false;
}
}
return true;
}
return false;
}
}
class CharNumberAI
{
public static void Do()
{
Dictionary<int, List<char>> dict = new Dictionary<int, List<char>>();
dict.Add(0, new List<char>() { 'z', 'a', 'q', 'x', 's', 'w' });
dict.Add(1, new List<char>() { 'c', 'd', 'e' });
dict.Add(2, new List<char>() { 'v', 'f', 'r' });
dict.Add(3, new List<char>() { 'b', 'g', 't' });
SimpleDo(1230, dict);
}
public static void SimpleDo(int n, Dictionary<int, List<char>> dict)
{
List<List<char>> allchars = new List<List<char>>();
int num = 0;
while (n > 0)
{
num = n % 10;
n = n / 10;
allchars.Add(dict[num]);
}
PrintPowerSet(allchars, allchars.Count - 1, new StringBuilder());
}
private static void PrintPowerSet(List<List<char>> allchars, int index, StringBuilder sb)
{
List<char> chars = allchars[index];
StringBuilder subSb = new StringBuilder();
foreach (char ch in chars)
{
subSb.Clear();
subSb.Append(sb.ToString());
if (index == 0)
{
//Console.Write("{0};", ch);
subSb.Append(ch);
subSb.Append(";");
Console.WriteLine(subSb.ToString());
}
else
{
//Console.Write(ch);
subSb.Append(ch);
PrintPowerSet(allchars, index - 1, subSb);
}
}
}
}
class DVRAI
{
public static void Do()
{
char[,] matrix =
{
{'a','b','c','d','e'},
{'f','g','h','i','j'},
{'k','l','m','n','o'},
{'p','q','r','s','t'},
{'u','v','w','x','y'}
};
SimpleDo(matrix, "hello");
}
public static void SimpleDo(char[,] matrix, string name)
{
int xStart = 0, yStart = 0, xNext = 0, yNext = 0;
StringBuilder sb = new StringBuilder();
foreach (char ch in name)
{
GetPosition(ch, ref xNext, ref yNext);
if (xNext > xStart)
{
for (int i = xStart; i < xNext; i++)
{
sb.Append("D");
}
}
if (xNext < xStart)
{
for (int i = xNext; i < xStart; i++)
{
sb.Append("U");
}
}
if (yNext > yStart)
{
for(int i = yStart; i < yNext; i++)
{
sb.Append("R");
}
}
if (yNext < yStart)
{
for (int i = yNext; i < yStart; i++)
{
sb.Append("L");
}
}
sb.Append("!");
xStart = xNext;
yStart = yNext;
}
Console.WriteLine(sb.ToString());
}
private static void GetPosition(char ch, ref int x, ref int y)
{
int i = ch - 'a';
x = i / 5;
y = i % 5;
}
}
I think Tries will work in this scenario.
- allanchanly September 30, 2013public static void SimpleDo(List<object> numbers, int contiNum)
{
int start = 0, end = 0, tempStart = 0, tempEnd = 0, count = 0, tempCount = 0;
HashSet<object> digits = new HashSet<object>();
for (int i = 0; i < numbers.Count; i++)
{
tempStart = i;
tempEnd = i;
tempCount = 0;
digits.Clear();
for (int j = i; j < numbers.Count; j++)
{
if (digits.Count < contiNum)
{
tempEnd = j;
tempCount++;
if (!digits.Contains(numbers[j]))
{
digits.Add(numbers[j]);
}
}
else if (digits.Count == contiNum && digits.Contains(numbers[j]))
{
tempEnd = j;
tempCount++;
}
else
{
if (tempCount > count)
{
count = tempCount;
start = tempStart;
end = tempEnd;
}
break;
}
}
}
for (int i = start; i <= end && i < numbers.Count; i++)
{
Console.Write("{0},", numbers[i]);
}
Console.WriteLine();
}
public static void SimpleDo(int num, int[] digits)
{
for (int i = 0; i <= num; i++)
{
int num2 = i;
int digit = 0;
while (num2 > 0)
{
digit = num2 % 10;
foreach (int d in digits)
{
if (d == digit)
{
Console.Write("{0}\t", i);
}
}
num2 = num2 / 10;
}
}
Console.WriteLine();
}
- allanchanly September 24, 2013a simply way to solve this, but there is another optimized algorithm to do it
public static void SimpleDo(int num, int[] digits)
{
for (int i = 0; i <= num; i++)
{
int num2 = i;
int digit = 0;
while (num2 > 0)
{
digit = num2 % 10;
foreach (int d in digits)
{
if (d == digit)
{
Console.Write("{0}\t", i);
}
}
num2 = num2 / 10;
}
}
}
here is the C# implementation, but I haven't developed any test case yet, :)
public static int SimpleDo(List<int> coins, int sum)
{
//build tree
CoinArrange2TreeNode parent,left, right, root,temp;
root = new CoinArrange2TreeNode(0) { Add = false, Sum = 0 };
Queue<CoinArrange2TreeNode> parents = new Queue<CoinArrange2TreeNode>();
parents.Enqueue(root);
for (int i = 0; i < coins.Count; i++)
{
while (parents.Count != 0)
{
parent = parents.Dequeue();
left = new CoinArrange2TreeNode(coins[i]) { Add = false, Parent = parent, Sum = parent.Sum };
right = new CoinArrange2TreeNode(coins[i]) { Add = true, Parent = parent, Sum = parent.Sum + coins[i] };
}
}
//search tree
parents.Clear();
parents.Enqueue(root);
int min = Int32.MaxValue;
while (parents.Count != 0)
{
temp = parents.Dequeue();
if (temp.Sum == sum && temp.Count < min)
{
min = temp.Count;
}
if (temp.Left != null)
{
parents.Enqueue(temp.Left);
}
if (temp.Right != null)
{
parents.Enqueue(temp.Right);
}
}
return min == Int32.MaxValue ? -1 : min;
}
class CoinArrange2TreeNode
{
public int Value { get; set; }
public bool Add { get; set; }
public int Sum { get; set; }
public int Count { get; set; }
public CoinArrange2TreeNode Parent { get; set; }
public CoinArrange2TreeNode Left { get; set; }
public CoinArrange2TreeNode Right { get; set; }
public CoinArrange2TreeNode(int v)
{
Value = v;
}
}
get the in-order visit sequence of the given tree, and check whether it is in order or not.
- allanchanly October 23, 2013