Skip to content

Instantly share code, notes, and snippets.

@JL-Cox
Last active January 9, 2019 19:13
Show Gist options
  • Save JL-Cox/10352f5ca0867c837eb8b53e524b675c to your computer and use it in GitHub Desktop.
Save JL-Cox/10352f5ca0867c837eb8b53e524b675c to your computer and use it in GitHub Desktop.
String Mix
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace StringMix
{
class Program
{
static void Main(string[] args)
{
// Provide args s1 and s2 for Mix method
Mix("Are They HERE?", "yes, they are HEre");
Console.ReadKey();
}
public static string Mix(string s1, string s2)
{
Console.WriteLine($"S1: {s1} - S2 {s2}");
// initialize objects for each string
Sentence s1Obj = new Sentence();
Sentence s2Obj = new Sentence();
List<KeyValuePair<string, string>> kvPairs = new List<KeyValuePair<string, string>>();
Regex reg = new Regex(@"[a-z]");
Match match = reg.Match("");
foreach (var letter in s1)
{
match = reg.Match(letter.ToString());
if (match.Success && !Int32.TryParse(letter.ToString(), out int num))
{
if (s1Obj.letterCount.Where(x => x.Key == letter.ToString()).Count() == 0)
{
s1Obj.letterCount.Add(letter.ToString(), 1);
if (s2Obj.letterCount.Where(x => x.Key == letter.ToString()).Count() == 0)
{
s2Obj.letterCount.Add(letter.ToString(), 0);
}
}
else
{
s1Obj.letterCount[letter.ToString()]++;
}
}
}
foreach (var letter in s2)
{
match = reg.Match(letter.ToString());
if (match.Success && !Int32.TryParse(letter.ToString(), out int nums))
{
if (s2Obj.letterCount.Where(x => x.Key == letter.ToString()).Count() == 0)
{
s2Obj.letterCount.Add(letter.ToString(), 1);
if (s1Obj.letterCount.Where(x => x.Key == letter.ToString()).Count() == 0)
{
s1Obj.letterCount.Add(letter.ToString(), 0);
}
}
else
{
s2Obj.letterCount[letter.ToString()]++;
}
}
}
var answer = "";
foreach (var item in s1Obj.letterCount)
{
if (s1Obj.letterCount[item.Key] > s2Obj.letterCount[item.Key] && s1Obj.letterCount[item.Key] > 1)
{
var letters = "";
for (int i = 0; i <= s1Obj.letterCount[item.Key] - 1; i++)
{
letters += item.Key;
}
var kvp = new KeyValuePair<string, string>("1", letters);
kvPairs.Add(kvp);
}
else if (s1Obj.letterCount[item.Key] < s2Obj.letterCount[item.Key] && s2Obj.letterCount[item.Key] > 1)
{
var letters = "";
for (int i = 0; i <= s2Obj.letterCount[item.Key] - 1; i++)
{
letters += item.Key;
}
var kvp = new KeyValuePair<string, string>("2", letters);
kvPairs.Add(kvp);
}
else if (s1Obj.letterCount[item.Key] == s2Obj.letterCount[item.Key] && s1Obj.letterCount[item.Key] > 1)
{
var letters = "";
for (int i = 0; i <= s1Obj.letterCount[item.Key] - 1; i++)
{
letters += item.Key;
}
var kvp = new KeyValuePair<string, string>("3", letters);
kvPairs.Add(kvp);
}
else
{ }
}
if (kvPairs.Count > 0)
{
//key, length, value
kvPairs = kvPairs.OrderBy(x => x.Value).OrderBy(x => x.Key).OrderByDescending(x => x.Value.Length).ToList<KeyValuePair<string, string>>();
foreach (var item in kvPairs)
{
answer += $"{item.Key}:{item.Value}/";
}
answer = answer.Remove(answer.Length - 1).Replace("3", "=");
}
return answer;
}
}
public class Sentence
{
public string Name { get; set; }
public Dictionary<string, int> letterCount { get; set; }
public Sentence()
{
letterCount = new Dictionary<string, int>();
}
}
}
//Console.WriteLine(" - Letter Counts - \nString1:");
//foreach (var set in s1Obj.letterCount)
//{
// Console.WriteLine(set.Key + ":" + set.Value);
//}
//Console.WriteLine("\nString2:");
//foreach (var set in s2Obj.letterCount)
//{
// Console.WriteLine(set.Key + ":" + set.Value);
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment