-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter8_code_to_copy.txt
More file actions
156 lines (115 loc) · 3.35 KB
/
chapter8_code_to_copy.txt
File metadata and controls
156 lines (115 loc) · 3.35 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
P66
package pfb.basics
fun main(args: Array<String>) {
val sightings = mutableListOf<String>()
sightings.add("emu")
sightings.add("magpie")
sightings.add("galah")
sightings.add("emu")
println("Number of bird sightings: " + sightings.size)
if (sightings.contains("emu")) {
println("Saw an emu!")
}
if (sightings.contains("brolga")) {
println("Saw a brolga!")
}
println("Third sighting: " + sightings[2])
}
package pfb.basics
fun main(args: Array<String>) {
val birds = mutableSetOf<String>()
birds.add("emu")
birds.add("magpie")
birds.add("galah")
birds.add("emu")
println("Number of bird species: " + birds.size)
if (birds.contains("emu")) {
println("Saw an emu!")
}
if (birds.contains("brolga")) {
println("Saw a brolga!")
}
}
P67
package pfb.basics
fun main(args: Array<String>) {
val nameToAge = mutableMapOf<String, Int>()
nameToAge.put("Harry", 15)
nameToAge.put("Luna", 16)
nameToAge.put("Snape", 36)
println("Harry's age: " + nameToAge["Harry"])
println("Luna's age: " + nameToAge["Luna"])
println("Snape's age: " + nameToAge["Snape"])
}
P68
package pfb.basics
fun main(args: Array<String>) {
val nameToAge = mutableMapOf<String, Int>()
nameToAge.put("Harry", 15)
nameToAge.put("Luna", 16)
nameToAge.put("Snape", 36)
nameToAge.put("Luna", 17)//Happy birthday!
nameToAge.remove("Snape")//So long Snape!
for (name in nameToAge.keys) {
val age = nameToAge[name]
println("$name is $age years old")
}
}
package pfb.basics
fun main(args: Array<String>) {
//Record the sightings.
val sightings = mutableListOf<String>()
sightings.add("emu")
sightings.add("magpie")
sightings.add("galah")
sightings.add("emu")
//Count the species.
val speciesToCount = mutableMapOf<String, Int>()
for (sighting in sightings) {
val countSoFar = speciesToCount[sighting]
val updatedCount = countSoFar + 1
speciesToCount.put(sighting, updatedCount)
}
//Print the species counts.
for (species in speciesToCount.keys) {
val count = speciesToCount[species]
println("Number of $species sightings: $count")
}
}
P70
package pfb.basics
fun main(args: Array<String>) {
//Record the sightings.
val sightings = mutableListOf<String>()
sightings.add("emu")
sightings.add("magpie")
sightings.add("galah")
sightings.add("emu")
//Count the species.
val speciesToCount = mutableMapOf<String, Int>()
for (sighting in sightings) {
val countSoFar = speciesToCount[sighting] ?: 0
val updatedCount = countSoFar + 1
speciesToCount.put(sighting, updatedCount)
}
//Print the species counts.
for (species in speciesToCount.keys) {
val count = speciesToCount[species]
println("Number of $species sightings: $count")
}
}
--------------------------------
P71
package pfb.basics
fun main(args: Array<String>) {
val sightings = mutableListOf<String>()
sightings.add("pee-wee")
sightings.add("cockatoo")
sightings.add("thick-knee")
sightings.add("brolga")
println("Number of bird sightings: " + sightings.size)
if (sightings.contains("brolga")) {
println("Saw a brolga!")
}
println("Third sighting: " + sightings[2])
}