Eugene Mmmmm
BAN USERI'm a junior C# developer with IT background (>5 years)
Basic programmer skills: C# (.NET), WinForms, LINQ, OOP, SQLite;
It's simple for pairs.
public struct Result
{
public long x, y;
public Result(long x, long y)
{
this.x = x;
this.y = y;
}
}
static long[] input = new long[]{8,3,5,1,-4,-8,4};
static List<Result> ans = new List<Result>();
static void Main(string[] args)
{
int end;
for (int start=0;start<input.Length-1;start++)
{
end = start + 1;
do
{
if (input[start] + input[end++] == 0) ans.Add(new Result(input[start], input[end-1]));
} while (end!=input.Length);
}
foreach (Result item in ans)
{
Console.WriteLine(item.x+" "+item.y);
}
Console.ReadLine();
}
Simple hardcoded solution for 4 symbols. But how could you scale it up so that user would enter 5 or even six symbols like 'abcde' or 'abcdef'? You can't add FOR cycles and IFs on runtime...
- Eugene Mmmmm January 10, 2015Works good for 4 symbols, but doesn't work for 5 :(
class Program
{
static char[] word = new char[] {'a','b','c','d'};
static char[] temp = new char[word.Length];
static List<string> result = new List<string>();
static void Main(string[] args)
{
for (int i = 0; i < word.Length; i++ )
{
word.CopyTo(temp, 0);
Combine();
Shift();
}
int counter = 0;
foreach(string s in (from c in result select c).Distinct())
{
Console.WriteLine((++counter).ToString() +" "+s);
}
Console.ReadLine();
}
static void Combine()
{
int end;
do
{
for (int start = 0; start < temp.Length - 1; start++)
{
end = start + 1;
do
{
result.Add(new string(word));
Swap(start, end++);
} while (end != temp.Length);
}
} while (new string(word) != new string(temp));
}
static void Swap(int first, int second)
{
char temp = word[first];
word[first] = word[second];
word[second] = temp;
}
static void Shift()
{
char temp = word[word.Length - 1];
for (int i = word.Length-1; i > 0; i--)
{
word[i] = word[i - 1];
}
word[0] = temp;
}
}
I'm sure my solution could be optimized but the basic idea seems to me like this:
class Program
{
static List<string> result = new List<string>();
static int steps;
static List<int> positions = new List<int>();
static char[] inputchar = { '0', '?', '1', '?' };
static void Main(string[] args)
{
int j = 0;
for (int i = 0; i < inputchar.Length;i++ )
if (inputchar[i]=='?')
{
positions.Add(i);
steps++;
}
char[] tempchar = replaceWildCard(inputchar, (int)Math.Pow(2,steps)-1);
foreach (string item in result)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
static char[] replaceWildCard(char[] input, int i)
{
if (i == -1) return input;
string bin = Convert.ToString(i, 2).PadLeft(positions.Count(), '0');
for (int j = 0; j < positions.Count(); j++) input[positions[j]] = bin[j];
result.Add(new string(input));
return replaceWildCard(inputchar, i - 1);
}
}
Output is:
0111
0110
0011
0010
Also tested this code with more input chars. Works fine.
The shortest way, I suppose.
static void Main(string[] args)
{
int[] a = new int[] { 1, 4, 2, 6, 3 };
int[] b = new int[] { 4, 0, 7, 6, 3, 2, 1 };
var ans = a.Concat(b).GroupBy(x => x).Where(y => y.Count() == 1).Select(z => z.Key).ToList();
foreach (int item in ans)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
static void Main(string[] args)
{
Dictionary<string,string> dict = new Dictionary<string, string>()
{
{"internationalization", "i18n"},
{"localization","l10n"},
{"dog","d1g"},
{"accessibility","a11y"}
};
Console.WriteLine(IsAbbrUnique(dict, "dig"));
Console.WriteLine(IsAbbrUnique(dict, "digger"));
Console.ReadLine();
}
static bool IsAbbrUnique(Dictionary<string,string> dict, string word)
{
return !dict.Values.Any(value => value == word[0] + (word.Length - 2).ToString() + word[word.Length - 1]);
}
Someone is downvoting last answers...I wonder why.
- Eugene Mmmmm December 08, 2014class Program
{
static void Main(string[] args)
{
List<Days> person1 = new List<Days>();
List<Days> person2 = new List<Days>();
List<Days> meetings = new List<Days>();
person1.Add(new Days() { X = 1, Y = 5 });
person1.Add(new Days() { X = 10, Y = 14 });
person1.Add(new Days() { X = 19, Y = 20 });
person1.Add(new Days() { X = 21, Y = 24 });
person1.Add(new Days() { X = 27, Y = 30 });
person2.Add(new Days() { X = 3, Y = 5 });
person2.Add(new Days() { X = 12, Y = 15 });
person2.Add(new Days() { X = 18, Y = 21 });
person2.Add(new Days() { X = 23, Y = 24 });
meetings = FindMeetingDays(person1, person2);
foreach (Days days in meetings)
{
Console.WriteLine(days.X + " " + days.Y);
}
Console.ReadLine();
}
private static List<Days> FindMeetingDays(List<Days> person1, List<Days> person2)
{
bool[] meetings = new bool[32];
bool[] busydays1 = new bool[32];
bool[] busydays2 = new bool[32];
Days days = new Days();
List<Days> meeting_days = new List<Days>();
busydays1 = ConvertListDays(person1);
busydays2 = ConvertListDays(person2);
for (byte i = 1; i < 32; i++)
{
meetings[i] = !busydays1[i] && !busydays2[i] ? true : false;
if (meetings[i])
{
if (days.X != 0) days.Y = i;
else days.X = i;
}
if(!meetings[i] && meetings[i-1])
{
meeting_days.Add(days);
days = new Days();
}
}
return meeting_days;
}
private static bool[] ConvertListDays(List<Days> days)
{
bool[] temp = new bool[32];
foreach (Days d in days)
{
for (byte i = d.X; i < d.Y+1; i++)
{
temp[i] = true;
}
}
return temp;
}
}
struct Days
{
public byte X
{
get;
set;
}
public byte Y
{
get;
set;
}
}
int[] array = new int[10] {1,2,4,6,0,7,0,2,1,9};
int[] newarray = new int[10];
int j = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] != 0) newarray[j++] = array[i];
}
I've checked and slightly corrected your code. Now it works fine.
static void FindIntsWithSum( int[] array, int sum)
{
if(0 == array.Length)
return;
Dictionary<int, int> myDict = new Dictionary<int, int>();
for(int index = 0; index < array.Length; ++index)
{
if(myDict.ContainsValue(sum - array[index]))
{
Console.WriteLine("{0} & {1}", array[index], myDict.FirstOrDefault(x => x.Value == sum - array[index]).Value);
return;
}
else
{
myDict.Add(index, array[index]);
}
}
Console.WriteLine("This array does not contain values that sum upto: {0}", sum);
return;
}
The easiest hardcoded solution for 3-value subsets:
- Eugene Mmmmm January 10, 2015