Skip to content

Commit 07d8e11

Browse files
authored
Merge pull request kodecocodes#19 from raywenderlich/08-collections
08 collections
2 parents bb3f9a4 + dae9c5c commit 07d8e11

File tree

8 files changed

+581
-0
lines changed

8 files changed

+581
-0
lines changed

08-collections/assets/.keep

Whitespace-only changes.

08-collections/projects/challenge/.keep

Whitespace-only changes.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2020 Razeware LLC
2+
// For full license & permission details, see LICENSE.
3+
4+
void main() {
5+
challenge1();
6+
challenge2();
7+
challenge3();
8+
}
9+
10+
const paragraphOfText = 'Once upon a time there was a Dart programmer who '
11+
'had a challenging challenge to solve. Though the challenge was great, '
12+
'a solution did come. The end.';
13+
14+
void challenge1() {
15+
/// Challenge 1: A unique request
16+
///
17+
/// Write a function that takes a paragraph of text and returns a
18+
/// collection of unique String characters that the text contains.
19+
20+
Set<String> uniqueCodePoints(String text) {
21+
return text.runes
22+
.map((codePoint) => String.fromCharCode(codePoint))
23+
.toSet();
24+
}
25+
26+
print(uniqueCodePoints(paragraphOfText));
27+
}
28+
29+
void challenge2() {
30+
/// Challenge 2: Counting on you
31+
///
32+
/// Repeat Challenge 1 but this time have the function return a
33+
/// collection that contains the frequency count of every unique
34+
/// character.
35+
36+
Map<String, int> characterFrequencyMap(String text) {
37+
final characterFrequencyMap = <String, int>{};
38+
for (var codePoint in text.runes) {
39+
final character = String.fromCharCode(codePoint);
40+
if (characterFrequencyMap.containsKey(character)) {
41+
characterFrequencyMap[character]++;
42+
} else {
43+
characterFrequencyMap[character] = 1;
44+
}
45+
}
46+
return characterFrequencyMap;
47+
}
48+
49+
print(characterFrequencyMap(paragraphOfText));
50+
}
51+
52+
void challenge3() {
53+
/// Challenge 3: Mapping users
54+
///
55+
/// Create a class called `User` with properties for `id` and `name`.
56+
/// Make a `List` with three users, specifying any appropriate names
57+
/// and IDs you like. Then write a function that converts your user
58+
/// list to a list of maps whose keys are `id` and `name`.
59+
60+
final users = [
61+
User(1, 'Brian'),
62+
User(2, 'Chris'),
63+
User(3, 'Pablo'),
64+
];
65+
66+
// Note: You'll find many APIs use `dynamic` instead of `Object` when
67+
// converting a custom object to a map. Either one works, though using
68+
// `Object` when possible allows you to keep type safety.
69+
List<Map<String, Object>> usersToMapList(List<User> users) {
70+
final userMapList = <Map<String, Object>>[];
71+
for (var user in users) {
72+
final userMap = {
73+
'id': user.id,
74+
'name': user.name,
75+
};
76+
userMapList.add(userMap);
77+
}
78+
return userMapList;
79+
}
80+
81+
print(usersToMapList(users));
82+
}
83+
84+
class User {
85+
User(this.id, this.name);
86+
final int id;
87+
final String name;
88+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright (c) 2020 Razeware LLC
2+
// For full license & permission details, see LICENSE.
3+
4+
void main() {
5+
listsMiniExercise1();
6+
listsMiniExercise2();
7+
listsMiniExercise3();
8+
9+
mapsMiniExercise1();
10+
mapsMiniExercise2();
11+
mapsMiniExercise3();
12+
13+
higherOrderMethodsMiniExercise1();
14+
higherOrderMethodsMiniExercise2();
15+
}
16+
17+
void listsMiniExercise1() {
18+
/// Lists: Mini-exercise 1
19+
///
20+
/// Create an empty list of type `String`. Call it `months`. Use
21+
/// the `add` method to add the names of the twelve months.
22+
23+
final months = <String>[];
24+
months.add('January');
25+
months.add('February');
26+
months.add('March');
27+
months.add('April');
28+
months.add('May');
29+
months.add('June');
30+
months.add('July');
31+
months.add('August');
32+
months.add('September');
33+
months.add('October');
34+
months.add('November');
35+
months.add('December');
36+
print(months);
37+
}
38+
39+
void listsMiniExercise2() {
40+
/// Lists: Mini-exercise 2
41+
///
42+
/// Make an immutable list with the same elements as in Mini-exercise 1.
43+
44+
const months = [
45+
'January',
46+
'February',
47+
'March',
48+
'April',
49+
'May',
50+
'June',
51+
'July',
52+
'August',
53+
'September',
54+
'October',
55+
'November',
56+
'December',
57+
];
58+
print(months);
59+
}
60+
61+
void listsMiniExercise3() {
62+
/// Lists: Mini-exercise 3
63+
///
64+
/// Use collection `for` to create a new list with the month names in
65+
/// all uppercase.
66+
67+
const months = [
68+
'January',
69+
'February',
70+
'March',
71+
'April',
72+
'May',
73+
'June',
74+
'July',
75+
'August',
76+
'September',
77+
'October',
78+
'November',
79+
'December',
80+
];
81+
final bigMonths = [for (var month in months) month.toUpperCase()];
82+
print(bigMonths);
83+
}
84+
85+
void mapsMiniExercise1() {
86+
/// Maps: Mini-exercise 1
87+
///
88+
/// Create a map with the following keys: `name`, `profession`, `country`
89+
/// and `city`. For the values, add your own information.
90+
91+
final myMap = {
92+
'name': 'Li Ming',
93+
'profession': 'software engineer',
94+
'country': 'China',
95+
'city': 'Beijing',
96+
};
97+
print(myMap);
98+
}
99+
100+
void mapsMiniExercise2() {
101+
/// Maps: Mini-exercise 2
102+
///
103+
/// You suddenly decide to move to Toronto, Canada. Programmatically update
104+
/// the values for `country` and `city`.
105+
106+
final myMap = {
107+
'name': 'Li Ming',
108+
'profession': 'software engineer',
109+
'country': 'China',
110+
'city': 'Beijing',
111+
};
112+
myMap['country'] = 'Canada';
113+
myMap['city'] = 'Toronto';
114+
print(myMap);
115+
}
116+
117+
void mapsMiniExercise3() {
118+
/// Maps: Mini-exercise 3
119+
///
120+
/// Iterate over the map and print all the values.
121+
122+
final myMap = {
123+
'name': 'Li Ming',
124+
'profession': 'software engineer',
125+
'country': 'Canada',
126+
'city': 'Toronto',
127+
};
128+
129+
for (var value in myMap.values) {
130+
print(value);
131+
}
132+
133+
// Or:
134+
myMap.forEach((key, value) => print(value));
135+
}
136+
137+
void higherOrderMethodsMiniExercise1() {
138+
/// Higher order methods: Mini-exercise 1
139+
///
140+
/// Given the following exam scores:
141+
///
142+
/// ```
143+
/// final scores = [89, 77, 46, 93, 82, 67, 32, 88];
144+
/// ```
145+
///
146+
/// Use `sort` to find the highest and lowest grades.
147+
148+
final scores = [89, 77, 46, 93, 82, 67, 32, 88];
149+
scores.sort();
150+
final lowest = scores.first;
151+
final highest = scores.last;
152+
print(lowest);
153+
print(highest);
154+
}
155+
156+
void higherOrderMethodsMiniExercise2() {
157+
/// Higher order methods: Mini-exercise 2
158+
///
159+
/// Given the following exam scores:
160+
///
161+
/// ```
162+
/// final scores = [89, 77, 46, 93, 82, 67, 32, 88];
163+
/// ```
164+
///
165+
/// Use `where` to find all the B grades, that is, all the scores
166+
/// between 80 and 90.
167+
168+
final scores = [89, 77, 46, 93, 82, 67, 32, 88];
169+
final bGrades = scores.where((score) => score >= 80 && score < 90);
170+
print(bGrades);
171+
}

08-collections/projects/final/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)