Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions exercises/practice/parallel-letter-frequency/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
public static class ParallelLetterFrequency
{
public static Dictionary<char, int> Calculate(IEnumerable<string> texts)
public static async Task<Dictionary<char, int>> Calculate(IEnumerable<string> texts)
{
return texts.AsParallel().Aggregate(new Dictionary<char, int>(), AddCount);
}
var textList = texts.ToList();

private static Dictionary<char, int> AddCount(Dictionary<char, int> target, string text)
if (!textList.Any())
{
return new Dictionary<char, int>();
}
// Process each text in parallel using Task.Run
var tasks = textList.Select(text => Task.Run(() => CountLettersInText(text)));

// Wait for all tasks to complete
var results = await Task.WhenAll(tasks);

return MergeDictionaries(results);
}

private static Dictionary<char, int> CountLettersInText(string text)
{
foreach (var kv in text.ToLower().Where(char.IsLetter).GroupBy(c => c))
return text.ToLower()
.Where(char.IsLetter)
.GroupBy(c => c)
.ToDictionary(g => g.Key, g => g.Count());
}

private static Dictionary<char, int> MergeDictionaries(IEnumerable<Dictionary<char, int>> dictionaries)
{
var result = new Dictionary<char, int>();

foreach (var dictionary in dictionaries)
{
if (target.ContainsKey(kv.Key))
{
target[kv.Key] += kv.Count();
}
else
foreach (var kvp in dictionary)
{
target[kv.Key] = kv.Count();
if (result.ContainsKey(kvp.Key))
result[kvp.Key] += kvp.Value;
else
result[kvp.Key] = kvp.Value;
}
}

return target;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"j2jensen",
"robkeim",
"tushartyagi",
"wolf99"
"wolf99",
"karanchadha10"
],
"files": {
"solution": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public static class ParallelLetterFrequency
{
public static Dictionary<char, int> Calculate(IEnumerable<string> texts)
public static Task<Dictionary<char, int>> Calculate(IEnumerable<string> texts)
{
throw new NotImplementedException("You need to implement this method.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
public class ParallelLetterFrequencyTests
{
[Fact]
public void No_texts()
public async Task No_texts()
{
string[] texts = []; var expected = new Dictionary<char, int>
{ };
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
string[] texts = [];
var expected = new Dictionary<char, int> { };
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void One_text_with_one_letter()
[Fact]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you re-add the skips on all but the first test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, @ErikSchierboom kindly review, thanks.

public async Task One_text_with_one_letter()
{
string[] texts = [
"a"
Expand All @@ -18,11 +19,12 @@ public void One_text_with_one_letter()
{
['a'] = 1
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void One_text_with_multiple_letters()
[Fact]
public async Task One_text_with_multiple_letters()
{
string[] texts = [
"bbcccd"
Expand All @@ -33,11 +35,12 @@ public void One_text_with_multiple_letters()
['c'] = 3,
['d'] = 1
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Two_texts_with_one_letter()
[Fact]
public async Task Two_texts_with_one_letter()
{
string[] texts = [
"e",
Expand All @@ -48,11 +51,12 @@ public void Two_texts_with_one_letter()
['e'] = 1,
['f'] = 1
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Two_texts_with_multiple_letters()
[Fact]
public async Task Two_texts_with_multiple_letters()
{
string[] texts = [
"ggh",
Expand All @@ -64,11 +68,12 @@ public void Two_texts_with_multiple_letters()
['h'] = 3,
['i'] = 1
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Ignore_letter_casing()
[Fact]
public async Task Ignore_letter_casing()
{
string[] texts = [
"m",
Expand All @@ -78,11 +83,12 @@ public void Ignore_letter_casing()
{
['m'] = 2
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Ignore_whitespace()
[Fact]
public async Task Ignore_whitespace()
{
string[] texts = [
" ",
Expand All @@ -91,13 +97,14 @@ public void Ignore_whitespace()
];
var expected = new Dictionary<char, int>
{ };
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Ignore_punctuation()
[Fact]
public async Task Ignore_punctuation()
{
string[] texts = [
string[] texts = [
"!",
"?",
";",
Expand All @@ -106,11 +113,12 @@ public void Ignore_punctuation()
];
var expected = new Dictionary<char, int>
{ };
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Ignore_numbers()
[Fact]
public async Task Ignore_numbers()
{
string[] texts = [
"1",
Expand All @@ -125,11 +133,12 @@ public void Ignore_numbers()
];
var expected = new Dictionary<char, int>
{ };
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Unicode_letters()
[Fact]
public async Task Unicode_letters()
{
string[] texts = [
"本",
Expand All @@ -144,11 +153,12 @@ public void Unicode_letters()
['ほ'] = 1,
['ø'] = 1
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Combination_of_lower_and_uppercase_letters_punctuation_and_white_space()
[Fact]
public async Task Combination_of_lower_and_uppercase_letters_punctuation_and_white_space()
{
string[] texts = [
"There, peeping among the cloud-wrack above a dark tower high up in the mountains, Sam saw a white star twinkle for a while. The beauty of it smote his heart, as he looked up out of the forsaken land, and hope returned to him. For like a shaft, clear and cold, the thought pierced him that in the end, the shadow was only a small and passing thing: there was light and high beauty forever beyond its reach."
Expand Down Expand Up @@ -178,11 +188,12 @@ public void Combination_of_lower_and_uppercase_letters_punctuation_and_white_spa
['w'] = 9,
['y'] = 4
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Large_texts()
[Fact]
public async Task Large_texts()
{
string[] texts = [
"I am a sick man.... I am a spiteful man. I am an unattractive man.\nI believe my liver is diseased. However, I know nothing at all about my disease, and do not\nknow for certain what ails me. I don't consult a doctor for it,\nand never have, though I have a respect for medicine and doctors.\nBesides, I am extremely superstitious, sufficiently so to respect medicine,\nanyway (I am well-educated enough not to be superstitious, but I am superstitious).\nNo, I refuse to consult a doctor from spite.\nThat you probably will not understand. Well, I understand it, though.\nOf course, I can't explain who it is precisely that I am mortifying in this case by my spite:\nI am perfectly well aware that I cannot \"pay out\" the doctors by not consulting them;\nI know better than anyone that by all this I am only injuring myself and no one else.\nBut still, if I don't consult a doctor it is from spite.\nMy liver is bad, well - let it get worse!\nI have been going on like that for a long time - twenty years. Now I am forty.\nI used to be in the government service, but am no longer.\nI was a spiteful official. I was rude and took pleasure in being so.\nI did not take bribes, you see, so I was bound to find a recompense in that, at least.\n(A poor jest, but I will not scratch it out. I wrote it thinking it would sound very witty;\nbut now that I have seen myself that I only wanted to show off in a despicable way -\nI will not scratch it out on purpose!) When petitioners used to come for\ninformation to the table at which I sat, I used to grind my teeth at them,\nand felt intense enjoyment when I succeeded in making anybody unhappy.\nI almost did succeed. For the most part they were all timid people - of course,\nthey were petitioners. But of the uppish ones there was one officer in particular\nI could not endure. He simply would not be humble, and clanked his sword in a disgusting way.\nI carried on a feud with him for eighteen months over that sword. At last I got the better of him.\nHe left off clanking it. That happened in my youth, though. But do you know,\ngentlemen, what was the chief point about my spite? Why, the whole point,\nthe real sting of it lay in the fact that continually, even in the moment of the acutest spleen,\nI was inwardly conscious with shame that I was not only not a spiteful but not even an embittered man,\nthat I was simply scaring sparrows at random and amusing myself by it.\nI might foam at the mouth, but bring me a doll to play with, give me a cup of tea with sugar in it,\nand maybe I should be appeased. I might even be genuinely touched,\nthough probably I should grind my teeth at myself afterwards and lie awake at night with shame for\nmonths after. That was my way. I was lying when I said just now that I was a spiteful official.\nI was lying from spite. I was simply amusing myself with the petitioners and with the officer,\nand in reality I never could become spiteful. I was conscious every moment in myself of many,\nvery many elements absolutely opposite to that. I felt them positively swarming in me,\nthese opposite elements. I knew that they had been swarming in me all my life and craving some outlet from me,\nbut I would not let them, would not let them, purposely would not let them come out.\nThey tormented me till I was ashamed: they drove me to convulsions and - sickened me, at last,\nhow they sickened me!",
Expand Down Expand Up @@ -218,11 +229,12 @@ public void Large_texts()
['x'] = 7,
['y'] = 251
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Many_small_texts()
[Fact]
public async Task Many_small_texts()
{
string[] texts = [
"abbccc",
Expand Down Expand Up @@ -282,6 +294,7 @@ public void Many_small_texts()
['b'] = 100,
['c'] = 150
};
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
var result = await ParallelLetterFrequency.Calculate(texts);
Assert.Equal(expected, result);
}
}
Loading