Skip to content

Commit 656704a

Browse files
committed
fix: Convert parallel-letter-frequency exercise to use async/await
1 parent f689412 commit 656704a

File tree

4 files changed

+92
-57
lines changed

4 files changed

+92
-57
lines changed
Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
1-
public static class ParallelLetterFrequency
1+
public class ParallelLetterFrequency
22
{
3-
public static Dictionary<char, int> Calculate(IEnumerable<string> texts)
3+
public async Task<Dictionary<char, int>> Calculate(IEnumerable<string> texts)
44
{
5-
return texts.AsParallel().Aggregate(new Dictionary<char, int>(), AddCount);
6-
}
5+
var textList = texts.ToList();
76

8-
private static Dictionary<char, int> AddCount(Dictionary<char, int> target, string text)
7+
if (!textList.Any())
8+
{
9+
return new Dictionary<char, int>();
10+
}
11+
// Process each text in parallel using Task.Run
12+
var tasks = textList.Select(text => Task.Run(() => CountLettersInText(text)));
13+
14+
// Wait for all tasks to complete
15+
var results = await Task.WhenAll(tasks);
16+
17+
return MergeDictionaries(results);
18+
}
19+
20+
private Dictionary<char, int> CountLettersInText(string text)
921
{
10-
foreach (var kv in text.ToLower().Where(char.IsLetter).GroupBy(c => c))
22+
return text.ToLower()
23+
.Where(char.IsLetter)
24+
.GroupBy(c => c)
25+
.ToDictionary(g => g.Key, g => g.Count());
26+
}
27+
28+
private Dictionary<char, int> MergeDictionaries(IEnumerable<Dictionary<char, int>> dictionaries)
29+
{
30+
var result = new Dictionary<char, int>();
31+
32+
foreach (var dictionary in dictionaries)
1133
{
12-
if (target.ContainsKey(kv.Key))
13-
{
14-
target[kv.Key] += kv.Count();
15-
}
16-
else
34+
foreach (var kvp in dictionary)
1735
{
18-
target[kv.Key] = kv.Count();
36+
if (result.ContainsKey(kvp.Key))
37+
result[kvp.Key] += kvp.Value;
38+
else
39+
result[kvp.Key] = kvp.Value;
1940
}
2041
}
21-
22-
return target;
42+
43+
return result;
2344
}
2445
}

exercises/practice/parallel-letter-frequency/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"j2jensen",
77
"robkeim",
88
"tushartyagi",
9-
"wolf99"
9+
"wolf99",
10+
"karanchadha10"
1011
],
1112
"files": {
1213
"solution": [

exercises/practice/parallel-letter-frequency/ParallelLetterFrequency.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public static class ParallelLetterFrequency
22
{
3-
public static Dictionary<char, int> Calculate(IEnumerable<string> texts)
3+
public static Task<Dictionary<char, int>> Calculate(IEnumerable<string> texts)
44
{
55
throw new NotImplementedException("You need to implement this method.");
66
}

exercises/practice/parallel-letter-frequency/ParallelLetterFrequencyTests.cs

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
public class ParallelLetterFrequencyTests
22
{
33
[Fact]
4-
public void No_texts()
4+
public async Task No_texts()
55
{
6-
string[] texts = []; var expected = new Dictionary<char, int>
7-
{ };
8-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
6+
string[] texts = [];
7+
var expected = new Dictionary<char, int> { };
8+
var result = await ParallelLetterFrequency.Calculate(texts);
9+
Assert.Equal(expected, result);
910
}
1011

11-
[Fact(Skip = "Remove this Skip property to run this test")]
12-
public void One_text_with_one_letter()
12+
[Fact]
13+
public async Task One_text_with_one_letter()
1314
{
1415
string[] texts = [
1516
"a"
@@ -18,11 +19,12 @@ public void One_text_with_one_letter()
1819
{
1920
['a'] = 1
2021
};
21-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
22+
var result = await ParallelLetterFrequency.Calculate(texts);
23+
Assert.Equal(expected, result);
2224
}
2325

24-
[Fact(Skip = "Remove this Skip property to run this test")]
25-
public void One_text_with_multiple_letters()
26+
[Fact]
27+
public async Task One_text_with_multiple_letters()
2628
{
2729
string[] texts = [
2830
"bbcccd"
@@ -33,11 +35,12 @@ public void One_text_with_multiple_letters()
3335
['c'] = 3,
3436
['d'] = 1
3537
};
36-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
38+
var result = await ParallelLetterFrequency.Calculate(texts);
39+
Assert.Equal(expected, result);
3740
}
3841

39-
[Fact(Skip = "Remove this Skip property to run this test")]
40-
public void Two_texts_with_one_letter()
42+
[Fact]
43+
public async Task Two_texts_with_one_letter()
4144
{
4245
string[] texts = [
4346
"e",
@@ -48,11 +51,12 @@ public void Two_texts_with_one_letter()
4851
['e'] = 1,
4952
['f'] = 1
5053
};
51-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
54+
var result = await ParallelLetterFrequency.Calculate(texts);
55+
Assert.Equal(expected, result);
5256
}
5357

54-
[Fact(Skip = "Remove this Skip property to run this test")]
55-
public void Two_texts_with_multiple_letters()
58+
[Fact]
59+
public async Task Two_texts_with_multiple_letters()
5660
{
5761
string[] texts = [
5862
"ggh",
@@ -64,11 +68,12 @@ public void Two_texts_with_multiple_letters()
6468
['h'] = 3,
6569
['i'] = 1
6670
};
67-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
71+
var result = await ParallelLetterFrequency.Calculate(texts);
72+
Assert.Equal(expected, result);
6873
}
6974

70-
[Fact(Skip = "Remove this Skip property to run this test")]
71-
public void Ignore_letter_casing()
75+
[Fact]
76+
public async Task Ignore_letter_casing()
7277
{
7378
string[] texts = [
7479
"m",
@@ -78,11 +83,12 @@ public void Ignore_letter_casing()
7883
{
7984
['m'] = 2
8085
};
81-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
86+
var result = await ParallelLetterFrequency.Calculate(texts);
87+
Assert.Equal(expected, result);
8288
}
8389

84-
[Fact(Skip = "Remove this Skip property to run this test")]
85-
public void Ignore_whitespace()
90+
[Fact]
91+
public async Task Ignore_whitespace()
8692
{
8793
string[] texts = [
8894
" ",
@@ -91,13 +97,14 @@ public void Ignore_whitespace()
9197
];
9298
var expected = new Dictionary<char, int>
9399
{ };
94-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
100+
var result = await ParallelLetterFrequency.Calculate(texts);
101+
Assert.Equal(expected, result);
95102
}
96103

97-
[Fact(Skip = "Remove this Skip property to run this test")]
98-
public void Ignore_punctuation()
104+
[Fact]
105+
public async Task Ignore_punctuation()
99106
{
100-
string[] texts = [
107+
string[] texts = [
101108
"!",
102109
"?",
103110
";",
@@ -106,11 +113,12 @@ public void Ignore_punctuation()
106113
];
107114
var expected = new Dictionary<char, int>
108115
{ };
109-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
116+
var result = await ParallelLetterFrequency.Calculate(texts);
117+
Assert.Equal(expected, result);
110118
}
111119

112-
[Fact(Skip = "Remove this Skip property to run this test")]
113-
public void Ignore_numbers()
120+
[Fact]
121+
public async Task Ignore_numbers()
114122
{
115123
string[] texts = [
116124
"1",
@@ -125,11 +133,12 @@ public void Ignore_numbers()
125133
];
126134
var expected = new Dictionary<char, int>
127135
{ };
128-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
136+
var result = await ParallelLetterFrequency.Calculate(texts);
137+
Assert.Equal(expected, result);
129138
}
130139

131-
[Fact(Skip = "Remove this Skip property to run this test")]
132-
public void Unicode_letters()
140+
[Fact]
141+
public async Task Unicode_letters()
133142
{
134143
string[] texts = [
135144
"本",
@@ -144,11 +153,12 @@ public void Unicode_letters()
144153
['ほ'] = 1,
145154
['ø'] = 1
146155
};
147-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
156+
var result = await ParallelLetterFrequency.Calculate(texts);
157+
Assert.Equal(expected, result);
148158
}
149159

150-
[Fact(Skip = "Remove this Skip property to run this test")]
151-
public void Combination_of_lower_and_uppercase_letters_punctuation_and_white_space()
160+
[Fact]
161+
public async Task Combination_of_lower_and_uppercase_letters_punctuation_and_white_space()
152162
{
153163
string[] texts = [
154164
"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."
@@ -178,11 +188,12 @@ public void Combination_of_lower_and_uppercase_letters_punctuation_and_white_spa
178188
['w'] = 9,
179189
['y'] = 4
180190
};
181-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
191+
var result = await ParallelLetterFrequency.Calculate(texts);
192+
Assert.Equal(expected, result);
182193
}
183194

184-
[Fact(Skip = "Remove this Skip property to run this test")]
185-
public void Large_texts()
195+
[Fact]
196+
public async Task Large_texts()
186197
{
187198
string[] texts = [
188199
"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!",
@@ -218,11 +229,12 @@ public void Large_texts()
218229
['x'] = 7,
219230
['y'] = 251
220231
};
221-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
232+
var result = await ParallelLetterFrequency.Calculate(texts);
233+
Assert.Equal(expected, result);
222234
}
223235

224-
[Fact(Skip = "Remove this Skip property to run this test")]
225-
public void Many_small_texts()
236+
[Fact]
237+
public async Task Many_small_texts()
226238
{
227239
string[] texts = [
228240
"abbccc",
@@ -282,6 +294,7 @@ public void Many_small_texts()
282294
['b'] = 100,
283295
['c'] = 150
284296
};
285-
Assert.Equal(expected, ParallelLetterFrequency.Calculate(texts));
297+
var result = await ParallelLetterFrequency.Calculate(texts);
298+
Assert.Equal(expected, result);
286299
}
287300
}

0 commit comments

Comments
 (0)