Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 7, 2017 20:59
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 jianminchen/5cb8539adc1e7b450db28fa1c5f27515 to your computer and use it in GitHub Desktop.
Save jianminchen/5cb8539adc1e7b450db28fa1c5f27515 to your computer and use it in GitHub Desktop.
Count word practice - C# practice on August 7, 2017 12:00 pm, it took 40 minutes, failed to run the code because of the bug in first writing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SplitStringAndSort
{
class Program
{
/// <summary>
/// count word practice - August 7, 2017
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
var result = CountWordPractice("this practice is good one. Good to practice.");
}
public static string[,] CountWordPractice(string document)
{
// your code goes here
if (document == null || document.Length == 0)
{
return new string[0, 2];
}
var delimiter = new char[] { '.', '!', ' ' };
var split = document.Split(delimiter);
var dictionary = addToDictionary(split);
// bucket sort
var size = dictionary.Values.Max(); // LINQ
var sorted = new List<string[]>[size]; // 0 - max value, ..., size -1 - 1
var total = dictionary.Count; //
foreach (var pair in dictionary)
{
var key = pair.Key;
var value = pair.Value;
int index = size - value;
if(sorted[index] == null)
{
sorted[index] = new List<string[]>(); // added after the practice
}
sorted[index].Add(new string[] { key, value.ToString() });
}
// output to two dimension array
var result = new string[total, 2];
var row = 0; // added aftr the mocking - bug in writing
for (int i = 0; i < size; i++)
{
var visit = sorted[i]; // visit is List<string[]>
foreach (var item in visit) // add the loop after the mocking ... bug in writing
{
result[row, 0] = item[0]; // visit[0] is the array, string new List<string[]>[size]
result[row, 1] = item[1];
row++;
}
}
return result;
}
private static Dictionary<string, int> addToDictionary(string[] items)
{
var result = new Dictionary<string, int>();
foreach (var item in items)
{
var key = filter(item); // you'll -> youll
if (result.ContainsKey(key))
{
result[key]++;
}
else
{
result.Add(key, 1);
}
}
return result;
}
private static string filter(string s)
{
//var filterString = "abc'";
var filtered = new StringBuilder();
foreach (var item in s)
{
if (item != '\'')
{
filtered.Append(item);
}
}
return filtered.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment