1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
library;
List<int> squaresOfEven(List<int> xs) =>
xs.where((int x) => x.isEven).map((int x) => x * x).toList();
Map<int, List<String>> groupByLength(List<String> words) {
final result = <int, List<String>>{};
for (final w in words) {
(result[w.length] ??= <String>[]).add(w);
}
return result;
}
Map<String, int> wordCounts(String text) {
final counts = <String, int>{};
for (final raw in text.toLowerCase().split(RegExp(r'\s+'))) {
final word = raw.replaceAll(RegExp(r'[^a-z0-9]'), '');
if (word.isEmpty) continue;
counts[word] = (counts[word] ?? 0) + 1;
}
return counts;
}
Set<String> uniqueWords(String text) =>
text.toLowerCase().split(RegExp(r'[^a-z]+')).where((String w) => w.isNotEmpty).toSet();
List<int> sortedDescending(List<int> xs) => [...xs]..sort((int a, int b) => b.compareTo(a));
List<int> fixedSlots() => List<int>.filled(3, 0);
List<int> range(int n) => List<int>.generate(n, (int i) => i);
List<String> readOnlyTags() => List<String>.unmodifiable(<String>['dart', 'flutter']);
List<String> buildTags({
bool starred = false,
List<String> extra = const <String>[],
List<String>? more,
}) =>
[
'dart',
if (starred) 'starred',
for (final e in extra) 'extra:$e',
...?more,
];
Map<String, Map<String, int>> inventory() => {
'fruits': {'apple': 3, 'pear': 5},
'tools': {'hammer': 1},
};