Created
January 8, 2018 07:42
-
-
Save jianminchen/e16437c8887c8b96c99d9263f2883160 to your computer and use it in GitHub Desktop.
Word count engine - write my own C# code, and play with LINQ GroupBy and OrderByDescending clause etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
class Solution | |
{ | |
public static string[,] WordCountEngine(string document) | |
{ | |
if(document == null || document.Length == 0) | |
{ | |
return new string[0,0]; | |
} | |
var preprocessed = document.ToLower().Replace('\'', '\0'); // remove ' to empty space | |
var splitted = preprocessed.Split(", !".ToCharArray()); | |
// https://stackoverflow.com/questions/7325278/group-by-in-linq | |
var map = splitted.GroupBy(x => x).OrderByDescending(g => g.Count()); | |
var countedWord = new List<string>(); | |
var countValue = new List<string>(); | |
foreach(var item in map) | |
{ | |
var previous = ""; | |
int index = 0; | |
foreach(string element in item) "a a a b b c" | |
{ | |
var isNew = previous.CompareTo(element) != 0; | |
if(isNew) | |
{ | |
if(previous != "") | |
{ | |
countValue.Add(index.ToString()); | |
index = 0; | |
} | |
countedWord.Add(element); | |
} | |
index++; | |
previous = element; | |
} | |
} | |
Console.WriteLine(countedWord.Count); | |
Console.WriteLine(countValue.Count); | |
for(int i = 0; i < Math.Min(countedWord.Count,countValue.Count) ; i++) | |
{ | |
Console.WriteLine(countedWord[i] +" "+ countValue[i]); | |
} | |
return new string[0,0]; | |
} | |
static void Main(string[] args) | |
{ | |
WordCountEngine("a b c a a b c"); | |
} | |
} | |
/* | |
string[] words = string.Split(" .!") | |
lowercase -> Practice -> practice | |
bucket sort | |
dictionary<string, int> practice 3 | |
sort by total value -> | |
value -> max value | |
value-> OrderByValue -> | |
value = 3 value 2 value 1 | |
bucket perfect by | |
get | |
just | |
descending -> biggest value first -> go to small value | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment