Solutions updated
This commit is contained in:
parent
9d79aacedd
commit
9040ae6b70
|
@ -2,9 +2,8 @@
|
|||
package lambdasExercise1
|
||||
import atomictest.eq
|
||||
|
||||
fun transform(list: List<String>): List<Int> {
|
||||
return list.map { it.length }
|
||||
}
|
||||
fun transform(list: List<String>): List<Int> =
|
||||
list.map { it.length }
|
||||
|
||||
fun main() {
|
||||
transform(listOf("abc", "ab")) eq listOf(3, 2)
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 134
|
||||
- offset: 127
|
||||
length: 9
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -6,9 +6,8 @@ data class Author(val name: String)
|
|||
|
||||
data class Book(val title: String, val author: Author)
|
||||
|
||||
fun getAuthors(books: List<Book>): Set<Author> {
|
||||
return books.map { it.author }.toSet()
|
||||
}
|
||||
fun getAuthors(books: List<Book>): Set<Author> =
|
||||
books.map { it.author }.toSet()
|
||||
|
||||
fun main() {
|
||||
val books = listOf(
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 211
|
||||
length: 38
|
||||
length: 31
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -4,15 +4,13 @@ import atomictest.eq
|
|||
|
||||
data class Student(val id: Int, val name: String)
|
||||
|
||||
fun registerStudents(names: List<String>, startId: Int = 0): List<Student> {
|
||||
return names.mapIndexed { index, name -> Student(startId + index, name) }
|
||||
}
|
||||
fun registerStudents(names: List<String>, startId: Int = 0): List<Student> =
|
||||
names.mapIndexed { index, name -> Student(startId + index, name) }
|
||||
|
||||
fun main() {
|
||||
val students = listOf("Alice", "Bob")
|
||||
registerStudents(students) eq
|
||||
listOf(Student(0, "Alice"), Student(1, "Bob"))
|
||||
|
||||
registerStudents(students, startId = 10) eq
|
||||
listOf(Student(10, "Alice"), Student(11, "Bob"))
|
||||
}
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 197
|
||||
length: 73
|
||||
length: 66
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -12,7 +12,6 @@ data class Pet(
|
|||
|
||||
enum class Habitat {
|
||||
LAND, WATER, AMPHIBIOUS;
|
||||
|
||||
fun livesIn(pet: Pet) = pet.habitat == this
|
||||
}
|
||||
|
||||
|
|
|
@ -3,16 +3,16 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 381
|
||||
- offset: 380
|
||||
length: 21
|
||||
placeholder_text: TODO()
|
||||
- offset: 447
|
||||
- offset: 446
|
||||
length: 22
|
||||
placeholder_text: TODO()
|
||||
- offset: 516
|
||||
- offset: 515
|
||||
length: 27
|
||||
placeholder_text: TODO()
|
||||
- offset: 608
|
||||
- offset: 607
|
||||
length: 30
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -4,10 +4,9 @@ import atomictest.eq
|
|||
|
||||
data class Person(val name: String, val age: Int)
|
||||
|
||||
fun findOldest(people: List<Person>, quantity: Int = 1): List<String> {
|
||||
return people.sortedByDescending { it.age }
|
||||
fun findOldest(people: List<Person>, quantity: Int = 1): List<String> =
|
||||
people.sortedByDescending { it.age }
|
||||
.take(quantity).map { it.name }.sorted()
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val people = listOf(
|
||||
|
@ -16,4 +15,4 @@ fun main() {
|
|||
Person("Alice", 25))
|
||||
findOldest(people, 1) eq listOf("Bob")
|
||||
findOldest(people, 2) eq listOf("Alice", "Bob")
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 224
|
||||
length: 88
|
||||
length: 81
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -4,7 +4,7 @@ import atomictest.eq
|
|||
|
||||
val operation = { x: Int ->
|
||||
x in setOf(1, 5, 32)
|
||||
// other options:
|
||||
// Other options:
|
||||
// x % 5 == 0
|
||||
// x.toString().contains('5')
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 129
|
||||
length: 89
|
||||
length: 88
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
package operationsOnCollectionsExercise3
|
||||
import atomictest.eq
|
||||
|
||||
fun sum(list: List<Int?>): Int {
|
||||
return list.filterNotNull().sum()
|
||||
}
|
||||
fun sum(list: List<Int?>): Int =
|
||||
list.filterNotNull().sum()
|
||||
|
||||
fun main() {
|
||||
sum(listOf(1, 2, null)) eq 3
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 134
|
||||
length: 33
|
||||
length: 26
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Sequences/School1.kt
|
||||
// Sequences/Task2.kt
|
||||
package sequencesExercise2
|
||||
|
||||
fun School.studentInstructors(
|
||||
|
|
|
@ -3,10 +3,10 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 125
|
||||
- offset: 123
|
||||
length: 94
|
||||
placeholder_text: TODO()
|
||||
- offset: 291
|
||||
- offset: 289
|
||||
length: 114
|
||||
placeholder_text: TODO()
|
||||
- name: src/School.kt
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Sequences/Task2.kt
|
||||
// Sequences/Task3.kt
|
||||
package sequencesExercise3
|
||||
|
||||
fun School.averageInstructorRating(instructor: Instructor): Double =
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Sequences/School2.kt
|
||||
// Sequences/Task4.kt
|
||||
package sequencesExercise4
|
||||
|
||||
fun School.favouriteInstructor(student: Student): Instructor? =
|
||||
fun School.favoriteInstructor(student: Student): Instructor? =
|
||||
lessons
|
||||
.filter { student in it.students }
|
||||
.groupBy { it.instructor }
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 118
|
||||
- offset: 115
|
||||
length: 137
|
||||
placeholder_text: TODO()
|
||||
- name: src/School.kt
|
||||
|
|
|
@ -37,7 +37,7 @@ class TestSequencesExercise4 : TestSchool() {
|
|||
Assert.assertEquals("Wrong result for ${student.name}, " +
|
||||
"$schoolLessons:",
|
||||
favInstructor,
|
||||
schl(schoolLessons).favouriteInstructor(student))
|
||||
schl(schoolLessons).favoriteInstructor(student))
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Sequences/Task3.kt
|
||||
// Sequences/Task5.kt
|
||||
package sequencesExercise5
|
||||
|
||||
fun School.instructorsWithLargestClass(): Set<Instructor> {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Sequences/School3.kt
|
||||
// Sequences/Task6.kt
|
||||
package sequencesExercise6
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 107
|
||||
- offset: 105
|
||||
length: 30
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Sequences/Task4.kt
|
||||
// Sequences/Task7.kt
|
||||
package sequencesExercise7
|
||||
|
||||
fun oddWithout1(): Sequence<Int> {
|
||||
|
@ -8,4 +8,4 @@ fun oddWithout1(): Sequence<Int> {
|
|||
|
||||
fun main() {
|
||||
println(oddWithout1().take(20).sum())
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
// Sequences/School4.kt
|
||||
// Sequences/Task8.kt
|
||||
package sequencesExercise8
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 156
|
||||
- offset: 154
|
||||
length: 76
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -4,9 +4,8 @@ import atomictest.eq
|
|||
|
||||
class Person(val name: String, val age: Int)
|
||||
|
||||
fun List<Person>.getNamesOfAdults(): List<String> {
|
||||
return filter { it.age > 17 }.map { it.name }
|
||||
}
|
||||
fun List<Person>.getNamesOfAdults(): List<String> =
|
||||
filter { it.age > 17 }.map { it.name }
|
||||
|
||||
fun main() {
|
||||
val people = listOf(Person("Alice", 17), Person("Bob", 19))
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 194
|
||||
length: 45
|
||||
length: 38
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
package theImportanceOfLambdasExercise2
|
||||
import atomictest.eq
|
||||
|
||||
fun filterNonBlank(strings: List<String>): List<String> {
|
||||
return strings.filter { it.isNotBlank() }
|
||||
}
|
||||
fun filterNonBlank(strings: List<String>): List<String> =
|
||||
strings.filter { it.isNotBlank() }
|
||||
|
||||
fun main() {
|
||||
filterNonBlank(listOf("", "a", " ")) eq listOf("a")
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 154
|
||||
length: 41
|
||||
length: 34
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -12,7 +12,7 @@ class Alien(val name: String, val species: String, private var planet: String) {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
val alien1 = Alien("Arthricia", "Cat Person", "PurgeWorld")
|
||||
val alien1 = Alien("Arthricia", "Cat Person", "PurgeWorld")
|
||||
println(alien1)
|
||||
alien1.movePlanet("Earth C-137")
|
||||
println(alien1)
|
||||
|
|
|
@ -3,12 +3,12 @@ package exceptionsExercise3
|
|||
import atomictest.capture
|
||||
import atomictest.eq
|
||||
|
||||
fun repeatChar(c: Char, n: Int): String {
|
||||
fun repeatChar(ch: Char, n: Int): String {
|
||||
if (n < 0)
|
||||
throw IllegalArgumentException("Count 'n' must be non-negative, but was $n.")
|
||||
var s = ""
|
||||
repeat(n) {
|
||||
s += c
|
||||
s += ch
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 143
|
||||
length: 145
|
||||
- offset: 144
|
||||
length: 146
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
## Exceptions (#3)
|
||||
|
||||
Implement a function named `repeatChar()` that takes parameters `c` (the
|
||||
Implement a function named `repeatChar()` that takes parameters `ch` (the
|
||||
character to be repeated) and `n` (the number of times to repeat it) and
|
||||
returns a `String` consisting of `c` repeated `n` times. The function throws
|
||||
returns a `String` consisting of `ch` repeated `n` times. The function throws
|
||||
an `IllegalArgumentException` if `n` is negative. The exception message should
|
||||
be `"Count 'n' must be non-negative, but was x."` where `x` is replaced with
|
||||
the value of `n`.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Lists/IntList1.kt
|
||||
// Lists/Task2.kt
|
||||
package listsExercise2
|
||||
|
||||
fun countOccurrences(list: IntList, number: Int): Int {
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 103
|
||||
- offset: 100
|
||||
length: 113
|
||||
placeholder_text: TODO()
|
||||
- name: src/IntList.kt
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Lists/Task2.kt
|
||||
// Lists/Task3.kt
|
||||
package listsExercise3
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Packages/aaa.kt
|
||||
// Packages/EquilateralTriangle.kt
|
||||
package pythagorean
|
||||
import kotlin.math.sqrt
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
// Packages/Task3.kt
|
||||
package packagesExercise3
|
||||
import pythagorean.EquilateralTriangle
|
||||
|
|
@ -1,25 +1,25 @@
|
|||
type: edu
|
||||
files:
|
||||
- name: src/Task.kt
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
- name: src/EquilateralTriangle.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 0
|
||||
length: 154
|
||||
placeholder_text: /*TODO*/
|
||||
- name: src/Main.kt
|
||||
- offset: 55
|
||||
length: 115
|
||||
placeholder_text: // TODO class EquilateralTriangle
|
||||
- name: src/Task3.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 26
|
||||
- offset: 47
|
||||
length: 38
|
||||
placeholder_text: // TODO import
|
||||
- offset: 81
|
||||
- offset: 102
|
||||
length: 76
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val et = EquilateralTriangle(1.0)
|
||||
println(et.area()) // 0.4330127018922193
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
feedback_link: |
|
||||
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Introduction+to+Objects+%2F+Imports+%26+Packages+%2F+Exercise1
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// BaseClassInit/Task3.kt
|
||||
// BaseClassInit/Task2.kt
|
||||
package baseClassInitializationExercise2
|
||||
import atomictest.trace
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// BaseClassInit/Task3.kt
|
||||
package baseClassInitializationExercise3
|
||||
import atomictest.trace
|
||||
|
||||
|
|
|
@ -3,10 +3,10 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 66
|
||||
- offset: 92
|
||||
length: 146
|
||||
placeholder_text: '/* TODO: Implement Animal, Cat and Dog classes */'
|
||||
- offset: 227
|
||||
- offset: 253
|
||||
length: 119
|
||||
placeholder_text: |-
|
||||
/*
|
||||
|
|
|
@ -8,8 +8,8 @@ data class SpaceShip(val name: String) {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
SpaceShip("SuperhighspeedShip") eq
|
||||
"SpaceShip(name=SuperhighspeedShip)"
|
||||
SpaceShip("SuperHighSpeedShip") eq
|
||||
"SpaceShip(name=SuperHighSpeedShip)"
|
||||
SpaceShip("MClass", 29321) eq
|
||||
"SpaceShip(name=MClass-29321)"
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
// ScopeFunctions/ScopeFuncSoln2.kt
|
||||
// ScopeFunctions/Task2.kt
|
||||
package scopeFunctionsExercise2
|
||||
import classdelegation.SpaceShipControls
|
||||
import atomictest.*
|
||||
|
|
|
@ -3,19 +3,19 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 225
|
||||
- offset: 216
|
||||
length: 48
|
||||
placeholder_text: TODO()
|
||||
- offset: 297
|
||||
- offset: 288
|
||||
length: 101
|
||||
placeholder_text: TODO()
|
||||
- offset: 420
|
||||
- offset: 411
|
||||
length: 92
|
||||
placeholder_text: TODO()
|
||||
- offset: 538
|
||||
- offset: 529
|
||||
length: 92
|
||||
placeholder_text: TODO()
|
||||
- offset: 646
|
||||
- offset: 637
|
||||
length: 101
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -10,10 +10,9 @@ data class AirlineTicket(
|
|||
)
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
val ticket = AirlineTicket("Bruce", "Eckel", 123456, "DEN", "HND")
|
||||
val ticket = AirlineTicket("Bruce", "Eckel",
|
||||
123456, "DEN", "HND")
|
||||
println(ticket)
|
||||
*/
|
||||
}
|
||||
/* Expected output:
|
||||
AirlineTicket(firstName=Bruce, lastName=Eckel, ticket=123456, origin=DEN, destination=HND)
|
||||
|
|
|
@ -6,5 +6,13 @@ files:
|
|||
- offset: 54
|
||||
length: 143
|
||||
placeholder_text: // Implement AirlineTicket class
|
||||
- offset: 212
|
||||
length: 90
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val ticket = AirlineTicket("Bruce", "Eckel",
|
||||
123456, "DEN", "HND")
|
||||
println(ticket)
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -13,11 +13,13 @@ fun AirlineTicket.transferTicket(
|
|||
otherFirstName: String,
|
||||
otherLastName: String
|
||||
): AirlineTicket {
|
||||
return this.copy(firstName = otherFirstName, lastName = otherLastName)
|
||||
return this.copy(firstName = otherFirstName,
|
||||
lastName = otherLastName)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val ticket = AirlineTicket("Bruce", "Eckel", 123456, "DEN", "HND")
|
||||
val ticket = AirlineTicket("Bruce", "Eckel",
|
||||
123456, "DEN", "HND")
|
||||
println(ticket.transferTicket("Svetlana", "Isakova"))
|
||||
}
|
||||
/* Output:
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 304
|
||||
length: 70
|
||||
length: 74
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -2,19 +2,21 @@
|
|||
package destructuringDeclarationsExercise1
|
||||
import atomictest.eq
|
||||
|
||||
fun calculate(n1: Int, n2: Int): Triple<Boolean, Int, Int> {
|
||||
return if (n1 < 0 || n2 < 0) Triple(false, 0, 0)
|
||||
else Triple(true, n1 + n2, n1 * n2)
|
||||
fun calculate(
|
||||
n1: Int, n2: Int
|
||||
): Triple<Boolean, Int, Int> {
|
||||
return if (n1 < 0 || n2 < 0) Triple(false, 0, 0)
|
||||
else Triple(true, n1 + n2, n1 * n2)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val result = calculate(5, 7)
|
||||
result.first eq true
|
||||
result.second eq 12
|
||||
result.third eq 35
|
||||
val (success, plus, multiply) =
|
||||
calculate(11, 13)
|
||||
success eq true
|
||||
plus eq 24
|
||||
multiply eq 143
|
||||
val result = calculate(5, 7)
|
||||
result.first eq true
|
||||
result.second eq 12
|
||||
result.third eq 35
|
||||
val (success, plus, multiply) =
|
||||
calculate(11, 13)
|
||||
success eq true
|
||||
plus eq 24
|
||||
multiply eq 143
|
||||
}
|
||||
|
|
|
@ -3,14 +3,14 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 124
|
||||
- offset: 128
|
||||
length: 25
|
||||
placeholder_text: Any /* replace with required type */
|
||||
- offset: 156
|
||||
length: 88
|
||||
- offset: 158
|
||||
length: 86
|
||||
placeholder_text: TODO()
|
||||
- offset: 261
|
||||
length: 225
|
||||
length: 201
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val result = calculate(5, 7)
|
||||
|
|
|
@ -3,21 +3,21 @@ package destructuringDeclarationsExercise2
|
|||
import atomictest.eq
|
||||
|
||||
class Computation(
|
||||
val data: Int,
|
||||
val info: String
|
||||
val data: Int,
|
||||
val info: String,
|
||||
) {
|
||||
operator fun component1() = data
|
||||
operator fun component2() = info
|
||||
operator fun component1() = data
|
||||
operator fun component2() = info
|
||||
}
|
||||
|
||||
fun evaluate(input: Int) =
|
||||
if (input > 5)
|
||||
Computation(input * 2, "High")
|
||||
else
|
||||
Computation(input * 2, "Low")
|
||||
if (input > 5)
|
||||
Computation(input * 2, "High")
|
||||
else
|
||||
Computation(input * 2, "Low")
|
||||
|
||||
fun main() {
|
||||
val (value, description) = evaluate(7)
|
||||
value eq 14
|
||||
description eq "High"
|
||||
val (value, description) = evaluate(7)
|
||||
value eq 14
|
||||
description eq "High"
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ files:
|
|||
- offset: 91
|
||||
length: 5
|
||||
placeholder_text: data class
|
||||
- offset: 159
|
||||
length: 78
|
||||
- offset: 148
|
||||
length: 74
|
||||
placeholder_text: ""
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -14,7 +14,7 @@ fun displayPersonInfo(person: Person) {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
// val person = Person("Alice", 30)
|
||||
// val person = Person("Alice", 30)
|
||||
val person = Person("Alice", "Johnson", 30)
|
||||
displayPersonInfo(person)
|
||||
}
|
|
@ -12,7 +12,7 @@ files:
|
|||
val (name, age) = person
|
||||
// TODO
|
||||
- offset: 281
|
||||
length: 111
|
||||
length: 112
|
||||
placeholder_text: |-
|
||||
val person = Person("Alice", 30)
|
||||
// val person = Person("Alice", "Johnson", 30)
|
||||
|
|
|
@ -6,8 +6,6 @@ val <T> List<T>.reversed: List<T>
|
|||
get() = reversed()
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
val list = listOf(1, 2, 3)
|
||||
list.reversed eq listOf(3, 2, 1)
|
||||
*/
|
||||
}
|
|
@ -6,5 +6,12 @@ files:
|
|||
- offset: 91
|
||||
length: 54
|
||||
placeholder_text: '// TODO: implement ''reversed'''
|
||||
- offset: 160
|
||||
length: 63
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val list = listOf(1, 2, 3)
|
||||
list.reversed eq listOf(3, 2, 1)
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -8,11 +8,9 @@ val Rectangle.isSquare
|
|||
get() = width == height
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
val rectangle = Rectangle(1, 2)
|
||||
rectangle.isSquare eq false
|
||||
|
||||
val square = Rectangle(3, 3)
|
||||
square.isSquare eq true
|
||||
*/
|
||||
}
|
|
@ -6,5 +6,15 @@ files:
|
|||
- offset: 91
|
||||
length: 98
|
||||
placeholder_text: // TODO
|
||||
- offset: 204
|
||||
length: 121
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val rectangle = Rectangle(1, 2)
|
||||
rectangle.isSquare eq false
|
||||
|
||||
val square = Rectangle(3, 3)
|
||||
square.isSquare eq true
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -9,7 +9,6 @@ fun Container?.empty() = this == null || contents == null
|
|||
fun Container?.full() = !empty()
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
val container = Container(42)
|
||||
container.empty() eq false
|
||||
container.full() eq true
|
||||
|
@ -17,5 +16,4 @@ fun main() {
|
|||
val emptyContainer = Container(null)
|
||||
emptyContainer.empty() eq true
|
||||
emptyContainer.full() eq false
|
||||
*/
|
||||
}
|
|
@ -6,5 +6,17 @@ files:
|
|||
- offset: 139
|
||||
length: 91
|
||||
placeholder_text: // TODO Implement 'empty' and 'full' extension functions
|
||||
- offset: 245
|
||||
length: 193
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val container = Container(42)
|
||||
container.empty() eq false
|
||||
container.full() eq true
|
||||
|
||||
val emptyContainer = Container(null)
|
||||
emptyContainer.empty() eq true
|
||||
emptyContainer.full() eq false
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -17,7 +17,6 @@ class CountingSet<E> {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
val cs = CountingSet<String>()
|
||||
cs.add("abc")
|
||||
cs.add("abc")
|
||||
|
@ -25,5 +24,4 @@ fun main() {
|
|||
cs.count("abc") eq 2
|
||||
cs.count("def") eq 1
|
||||
cs.count("xyz") eq 0
|
||||
*/
|
||||
}
|
|
@ -6,5 +6,17 @@ files:
|
|||
- offset: 88
|
||||
length: 238
|
||||
placeholder_text: class CountingSet
|
||||
- offset: 341
|
||||
length: 149
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val cs = CountingSet<String>()
|
||||
cs.add("abc")
|
||||
cs.add("abc")
|
||||
cs.add("def")
|
||||
cs.count("abc") eq 2
|
||||
cs.count("def") eq 1
|
||||
cs.count("xyz") eq 0
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -14,15 +14,15 @@ class Rectangle(
|
|||
fun main() {
|
||||
println(Rectangle())
|
||||
|
||||
// without argument names
|
||||
// Without argument names
|
||||
println(Rectangle(1.1))
|
||||
println(Rectangle(1.1, 2.2, "blue"))
|
||||
|
||||
// mixed positional and named arguments
|
||||
// Mixed positional and named arguments
|
||||
println(Rectangle(1.1, 2.2, color = "blue"))
|
||||
println(Rectangle(side1 = 1.1, side2 = 2.2, "blue"))
|
||||
|
||||
// names for all arguments
|
||||
// Names for all arguments
|
||||
println(Rectangle(color = "blue"))
|
||||
println(Rectangle(side1 = 1.1, side2 = 2.2, color = "blue"))
|
||||
}
|
|
@ -7,9 +7,9 @@ fun joinComments(s: String): String =
|
|||
|
||||
fun main() {
|
||||
val s = """
|
||||
// first
|
||||
// second
|
||||
// third
|
||||
// First
|
||||
// Second
|
||||
// Third
|
||||
"""
|
||||
joinComments(s) eq "first; second; third"
|
||||
joinComments(s) eq "First; Second; Third"
|
||||
}
|
|
@ -1,21 +1,17 @@
|
|||
// NonNullAssertions/Task1.kt
|
||||
package nonNullAssertionsExercise1
|
||||
import atomictest.*
|
||||
|
||||
class Rocket {
|
||||
fun ignition() {
|
||||
println("Liftoff!")
|
||||
}
|
||||
fun ignition() = "Liftoff!"
|
||||
}
|
||||
|
||||
fun launch(rocket: Rocket?) {
|
||||
rocket!!.ignition()
|
||||
}
|
||||
fun launch(rocket: Rocket?) = rocket!!.ignition()
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
val rocket = Rocket()
|
||||
launch(rocket)
|
||||
// throws exception:
|
||||
// launch(null)
|
||||
*/
|
||||
launch(rocket) eq "Liftoff!"
|
||||
capture {
|
||||
launch(null)
|
||||
} eq "NullPointerException"
|
||||
}
|
|
@ -3,11 +3,21 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 66
|
||||
length: 63
|
||||
- offset: 86
|
||||
length: 46
|
||||
placeholder_text: class Rocket
|
||||
- offset: 131
|
||||
length: 53
|
||||
- offset: 134
|
||||
length: 49
|
||||
placeholder_text: fun launch() {}
|
||||
- offset: 198
|
||||
length: 113
|
||||
placeholder_text: |-
|
||||
/*
|
||||
val rocket = Rocket()
|
||||
launch(rocket) eq "Liftoff!"
|
||||
capture {
|
||||
launch(null)
|
||||
} eq "NullPointerException"
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -4,3 +4,7 @@ Create a class `Rocket` containing a member function `ignition()` that returns
|
|||
"Liftoff!". Create a function `launch()` that has a nullable `Rocket` parameter.
|
||||
Use a non-null assertion to call `ignition` on `rocket` and return the result.
|
||||
In `main()`, ensure that `launch()` throws an exception if you pass it a `null`.
|
||||
|
||||
<sub> This task doesn't contain automatic tests,
|
||||
so it's always marked as "Correct" when you run "Check".
|
||||
Please compare your solution with the one provided! </sub>
|
||||
|
|
|
@ -2,15 +2,14 @@ package nonNullAssertionsExercise1
|
|||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import util.checkParametersOfMemberFunction
|
||||
import util.checkParametersOfTopLevelFunction
|
||||
import util.loadMemberFunction
|
||||
import util.runAndCheckSystemOutput
|
||||
import util.*
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import kotlin.reflect.full.createInstance
|
||||
|
||||
class TestNonNullAssertionsExercise1 {
|
||||
@Test fun testRocket() {
|
||||
unimplementedTest()
|
||||
/*
|
||||
val rocketClass = Rocket::class
|
||||
val rocketInstance = rocketClass.createInstance()
|
||||
val ignitionFunc = loadMemberFunction(rocketClass, "ignition")
|
||||
|
@ -30,5 +29,6 @@ class TestNonNullAssertionsExercise1 {
|
|||
Assert.assertEquals("Wrong result after calling 'launch(null)', expected NullPointerException",
|
||||
e.targetException::class.simpleName, "NullPointerException")
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
// NonNullAssertions/Task2.kt
|
||||
package nonNullAssertionsExercise2
|
||||
import atomictest.*
|
||||
|
||||
fun List<Int>.headPlusTail(): Triple<Int?, Int?, Int?> =
|
||||
when {
|
||||
isEmpty() -> Triple(null, null, null)
|
||||
size == 1 -> Triple(first(), null, first())
|
||||
else -> Triple(first(), last(), first() + last())
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val ints = mutableListOf<Int>()
|
||||
trace(ints.headPlusTail())
|
||||
for (n in -2..10 step 2) {
|
||||
ints.add(n)
|
||||
trace(ints.headPlusTail())
|
||||
trace(ints.headPlusTail()!!.third)
|
||||
}
|
||||
trace eq """
|
||||
(null, null, null)
|
||||
(-2, null, -2)
|
||||
-2
|
||||
(-2, 0, -2)
|
||||
-2
|
||||
(-2, 2, 0)
|
||||
0
|
||||
(-2, 4, 2)
|
||||
2
|
||||
(-2, 6, 4)
|
||||
4
|
||||
(-2, 8, 6)
|
||||
6
|
||||
(-2, 10, 8)
|
||||
8
|
||||
"""
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
type: edu
|
||||
files:
|
||||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 316
|
||||
length: 378
|
||||
placeholder_text: // TODO
|
||||
- offset: 86
|
||||
length: 213
|
||||
placeholder_text: // fun List<Int>.headPlusTail()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
|
@ -0,0 +1,18 @@
|
|||
## Non-`null` Assertions (#2)
|
||||
|
||||
Define an extension function `List<Int>.headPlusTail()` that returns a `Triple`
|
||||
containing (1) the first element in the `List<Int>`, (2) the last element in the
|
||||
`List<Int>`, (3) the sum of the first and last elements. If the `List<Int>` is
|
||||
empty, return `null` for all three entries of the `Triple`. If the `List<Int>`
|
||||
consists of a single element, return that element as the first entry, followed
|
||||
by `null`, followed by that element as the result.
|
||||
|
||||
In `main()`, create a `mutableListOf<Int>` called `ints`. Call
|
||||
`trace(ints.headPlusTail())`. Next, create a `for` loop that steps `n` through
|
||||
the range `-2..10`, skipping every other element. The loop calls `ints.add(n)`,
|
||||
then `trace(ints.headPlusTail())`, and finally it selects the result of the call
|
||||
to `ints.headPlusTail()` using a non-null assertion.
|
||||
|
||||
<sub> This task doesn't contain automatic tests,
|
||||
so it's always marked as "Correct" when you run "Check".
|
||||
Please compare your solution with the one provided! </sub>
|
|
@ -0,0 +1,10 @@
|
|||
package nonNullAssertionsExercise2
|
||||
|
||||
import org.junit.Test
|
||||
import util.unimplementedTest
|
||||
|
||||
class TestNonNullAssertionsExercise2 {
|
||||
@Test fun testSolution() {
|
||||
unimplementedTest()
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
content:
|
||||
- Examples
|
||||
- Exercise 1
|
||||
- Exercise 2
|
||||
- Exercise 3
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
// NullableTypes/Task2.kt
|
||||
package nullableTypesExercise2
|
||||
|
||||
// Type your solution here
|
||||
fun zilch(): String? = null
|
||||
fun zilch2(): String? = zilch()
|
||||
|
||||
fun main() {
|
||||
var x: String? = zilch()
|
||||
var y: String? = zilch2()
|
||||
}
|
|
@ -6,3 +6,7 @@ returns the result of calling `zilch()`, and has an explicit return type.
|
|||
|
||||
In `main()`, call `zilch()` and assign the result to a `var` that you explicitly
|
||||
type. Do the same for `zilch2()`. What must you do to get this code to compile?
|
||||
|
||||
<sub> This task doesn't contain automatic tests,
|
||||
so it's always marked as "Correct" when you run "Check".
|
||||
Please compare your solution with the one provided! </sub>
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
package safeCallsAndTheElvisOperatorExercise1
|
||||
import atomictest.eq
|
||||
|
||||
fun downcase(s: String?): String = s?.toLowerCase() ?: ""
|
||||
fun downcase(s: String?): String =
|
||||
s?.toLowerCase() ?: ""
|
||||
|
||||
fun main() {
|
||||
/*
|
||||
downcase(null) eq ""
|
||||
downcase("ABC") eq "abc"
|
||||
*/
|
||||
}
|
|
@ -4,7 +4,14 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 98
|
||||
length: 57
|
||||
length: 59
|
||||
placeholder_text: fun downcase() {}
|
||||
- offset: 172
|
||||
length: 49
|
||||
placeholder_text: |-
|
||||
/*
|
||||
downcase(null) eq ""
|
||||
downcase("ABC") eq "abc"
|
||||
*/
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -1,30 +1,25 @@
|
|||
// BreakAndContinue/Task1.kt
|
||||
package breakAndContinueExercise1
|
||||
import atomictest.*
|
||||
|
||||
fun readNumbers() {
|
||||
fun readNumbers(vararg n: String) {
|
||||
var sum = 0
|
||||
while (true) {
|
||||
val input = readLine()
|
||||
val number = input?.toIntOrNull()
|
||||
for (input in n) {
|
||||
val number = input.toIntOrNull()
|
||||
if (number == null) {
|
||||
println("Not a number: $input")
|
||||
trace("Not a number: $input")
|
||||
} else {
|
||||
sum += number
|
||||
}
|
||||
if (number == 0) break
|
||||
}
|
||||
println("Sum: $sum")
|
||||
trace("Sum: $sum")
|
||||
}
|
||||
|
||||
fun main() {
|
||||
readNumbers()
|
||||
}
|
||||
/* Input/Output:
|
||||
>>> 1
|
||||
>>> a
|
||||
Not a number: a
|
||||
>>> 3
|
||||
>>> 10
|
||||
>>> 0
|
||||
Sum: 14
|
||||
*/
|
||||
readNumbers("1", "a", "3", "10", "0", "19")
|
||||
trace eq """
|
||||
Not a number: a
|
||||
Sum: 14
|
||||
"""
|
||||
}
|
|
@ -3,8 +3,8 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 86
|
||||
length: 250
|
||||
- offset: 122
|
||||
length: 222
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -1,23 +1,29 @@
|
|||
package breakAndContinueExercise1
|
||||
|
||||
import atomictest.trace
|
||||
import org.junit.FixMethodOrder
|
||||
import org.junit.Test
|
||||
import org.junit.runners.MethodSorters
|
||||
import util.TIMEOUT
|
||||
import util.checkInputOutput
|
||||
import util.loadTraceContent
|
||||
import util.resetTraceContent
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
class TestBreakAndContinueExercise1 {
|
||||
private fun testInput(input: String, expectedOutput: String) {
|
||||
checkInputOutput("Wrong output for input:\n$input\n", input, expectedOutput, ::readNumbers)
|
||||
private fun testData(expectedOutput: List<String>, input: List<String>) {
|
||||
resetTraceContent()
|
||||
readNumbers(*input.toTypedArray())
|
||||
assertEquals(expectedOutput, loadTraceContent(),
|
||||
"Wrong result for:\n$input\n")
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
fun test1() = testInput("1\n0", "Sum: 1")
|
||||
fun test1() = testData(listOf("Sum: 1"), listOf("1", "0"))
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
fun test2() = testInput("af\n1\n0", "Not a number: af\nSum: 1")
|
||||
fun test2() = testData(listOf("Not a number: af", "Sum: 1"), listOf("af", "1", "0"))
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
fun test3() = testInput("1\n9\n5\n0", "Sum: 15")
|
||||
fun test3() = testData(listOf("Sum: 15"), listOf("1", "9", "5", "0"))
|
||||
}
|
|
@ -3073,7 +3073,7 @@ public class TestAllExamples extends AbstractTestExamples {
|
|||
|
||||
@Test
|
||||
public void testMain() {
|
||||
testExample("Introduction to Objects/Packages/Exercise 3/src/Main.kt", packagesExercise3.MainKt::main);
|
||||
testExample("Introduction to Objects/Packages/Exercise 3/src/Main.kt", packagesExercise3.Task3Kt::main);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue