From 9040ae6b70329b65d752aa3352bd5194f139bcb3 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 3 Dec 2020 00:49:02 +0100 Subject: [PATCH] Solutions updated --- .../Lambdas/Exercise 1/src/Task.kt | 5 +-- .../Lambdas/Exercise 1/task-info.yaml | 2 +- .../Lambdas/Exercise 2/src/Task.kt | 5 +-- .../Lambdas/Exercise 2/task-info.yaml | 2 +- .../Lambdas/Exercise 3/src/Task.kt | 6 +-- .../Lambdas/Exercise 3/task-info.yaml | 2 +- .../Member References/Exercise 1/src/Task.kt | 1 - .../Exercise 1/task-info.yaml | 8 ++-- .../Exercise 1/src/Task.kt | 7 ++-- .../Exercise 1/task-info.yaml | 2 +- .../Exercise 2/src/Task.kt | 2 +- .../Exercise 2/task-info.yaml | 2 +- .../Exercise 3/src/Task.kt | 5 +-- .../Exercise 3/task-info.yaml | 2 +- .../Sequences/Exercise 2/src/Task.kt | 2 +- .../Sequences/Exercise 2/task-info.yaml | 4 +- .../Sequences/Exercise 3/src/Task.kt | 2 +- .../Sequences/Exercise 4/src/Task.kt | 4 +- .../Sequences/Exercise 4/task-info.yaml | 2 +- .../Sequences/Exercise 4/test/Tests.kt | 2 +- .../Sequences/Exercise 5/src/Task.kt | 2 +- .../Sequences/Exercise 6/src/Task.kt | 2 +- .../Sequences/Exercise 6/task-info.yaml | 2 +- .../Sequences/Exercise 7/src/Task.kt | 4 +- .../Sequences/Exercise 8/src/Task.kt | 2 +- .../Sequences/Exercise 8/task-info.yaml | 2 +- .../Exercise 1/src/Task.kt | 5 +-- .../Exercise 1/task-info.yaml | 2 +- .../Exercise 2/src/Task.kt | 5 +-- .../Exercise 2/task-info.yaml | 2 +- .../Exercise 1/src/Task.kt | 2 +- .../Exceptions/Exercise 3/src/Task.kt | 4 +- .../Exceptions/Exercise 3/task-info.yaml | 4 +- .../Exceptions/Exercise 3/task.md | 4 +- .../Lists/Exercise 2/src/Task.kt | 2 +- .../Lists/Exercise 2/task-info.yaml | 2 +- .../Lists/Exercise 3/src/Task.kt | 2 +- .../src/{Task.kt => EquilateralTriangle.kt} | 2 +- .../Exercise 3/src/{Main.kt => Task3.kt} | 1 + .../Packages/Exercise 3/task-info.yaml | 18 ++++----- .../Exercise 2/src/Task.kt | 2 +- .../Exercise 3/src/Task.kt | 1 + .../Exercise 3/task-info.yaml | 4 +- .../Exercise 2/src/Task.kt | 4 +- .../Scope Functions/Exercise 2/src/Task.kt | 2 +- .../Scope Functions/Exercise 2/task-info.yaml | 10 ++--- Usability/Data Classes/Exercise 1/src/Task.kt | 5 +-- .../Data Classes/Exercise 1/task-info.yaml | 8 ++++ Usability/Data Classes/Exercise 2/src/Task.kt | 6 ++- .../Data Classes/Exercise 2/task-info.yaml | 2 +- .../Exercise 1/src/Task.kt | 26 +++++++------ .../Exercise 1/task-info.yaml | 8 ++-- .../Exercise 2/src/Task.kt | 22 +++++------ .../Exercise 2/task-info.yaml | 4 +- .../Exercise 3/src/Task.kt | 2 +- .../Exercise 3/task-info.yaml | 2 +- .../Exercise 2/src/Task.kt | 2 - .../Exercise 2/task-info.yaml | 7 ++++ .../Exercise 3/src/Task.kt | 2 - .../Exercise 3/task-info.yaml | 10 +++++ .../Exercise 3/src/Task.kt | 2 - .../Exercise 3/task-info.yaml | 12 ++++++ .../Exercise 3/src/Task.kt | 2 - .../Exercise 3/task-info.yaml | 12 ++++++ .../Exercise 1/src/Task.kt | 6 +-- .../Exercise 3/src/Task.kt | 8 ++-- .../Exercise 1/src/Task.kt | 18 ++++----- .../Exercise 1/task-info.yaml | 18 +++++++-- .../Non-null Assertions/Exercise 1/task.md | 4 ++ .../Exercise 1/test/Tests.kt | 8 ++-- .../Exercise 2/src/Task.kt | 37 +++++++++++++++++++ .../Exercise 2/task-info.yaml | 13 +++++++ .../Non-null Assertions/Exercise 2/task.md | 18 +++++++++ .../Exercise 2/test/Tests.kt | 10 +++++ .../Non-null Assertions/lesson-info.yaml | 1 + .../Nullable Types/Exercise 2/src/Task.kt | 8 +++- Usability/Nullable Types/Exercise 2/task.md | 4 ++ .../Exercise 1/src/Task.kt | 5 +-- .../Exercise 1/task-info.yaml | 9 ++++- .../break & continue/Exercise 1/src/Task.kt | 29 ++++++--------- .../Exercise 1/task-info.yaml | 4 +- .../break & continue/Exercise 1/test/Tests.kt | 18 ++++++--- util/test/TestAllExamples.java | 2 +- 83 files changed, 328 insertions(+), 185 deletions(-) rename Introduction to Objects/Packages/Exercise 3/src/{Task.kt => EquilateralTriangle.kt} (78%) rename Introduction to Objects/Packages/Exercise 3/src/{Main.kt => Task3.kt} (87%) create mode 100644 Usability/Non-null Assertions/Exercise 2/src/Task.kt create mode 100644 Usability/Non-null Assertions/Exercise 2/task-info.yaml create mode 100644 Usability/Non-null Assertions/Exercise 2/task.md create mode 100644 Usability/Non-null Assertions/Exercise 2/test/Tests.kt diff --git a/Functional Programming/Lambdas/Exercise 1/src/Task.kt b/Functional Programming/Lambdas/Exercise 1/src/Task.kt index ea2dc88c..f987b137 100644 --- a/Functional Programming/Lambdas/Exercise 1/src/Task.kt +++ b/Functional Programming/Lambdas/Exercise 1/src/Task.kt @@ -2,9 +2,8 @@ package lambdasExercise1 import atomictest.eq -fun transform(list: List): List { - return list.map { it.length } -} +fun transform(list: List): List = + list.map { it.length } fun main() { transform(listOf("abc", "ab")) eq listOf(3, 2) diff --git a/Functional Programming/Lambdas/Exercise 1/task-info.yaml b/Functional Programming/Lambdas/Exercise 1/task-info.yaml index dc9968e1..c1c3b561 100644 --- a/Functional Programming/Lambdas/Exercise 1/task-info.yaml +++ b/Functional Programming/Lambdas/Exercise 1/task-info.yaml @@ -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 diff --git a/Functional Programming/Lambdas/Exercise 2/src/Task.kt b/Functional Programming/Lambdas/Exercise 2/src/Task.kt index af2ab24f..aa224b38 100644 --- a/Functional Programming/Lambdas/Exercise 2/src/Task.kt +++ b/Functional Programming/Lambdas/Exercise 2/src/Task.kt @@ -6,9 +6,8 @@ data class Author(val name: String) data class Book(val title: String, val author: Author) -fun getAuthors(books: List): Set { - return books.map { it.author }.toSet() -} +fun getAuthors(books: List): Set = + books.map { it.author }.toSet() fun main() { val books = listOf( diff --git a/Functional Programming/Lambdas/Exercise 2/task-info.yaml b/Functional Programming/Lambdas/Exercise 2/task-info.yaml index 9bb86903..6d03f37b 100644 --- a/Functional Programming/Lambdas/Exercise 2/task-info.yaml +++ b/Functional Programming/Lambdas/Exercise 2/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 211 - length: 38 + length: 31 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Functional Programming/Lambdas/Exercise 3/src/Task.kt b/Functional Programming/Lambdas/Exercise 3/src/Task.kt index 379991f5..5acac589 100644 --- a/Functional Programming/Lambdas/Exercise 3/src/Task.kt +++ b/Functional Programming/Lambdas/Exercise 3/src/Task.kt @@ -4,15 +4,13 @@ import atomictest.eq data class Student(val id: Int, val name: String) -fun registerStudents(names: List, startId: Int = 0): List { - return names.mapIndexed { index, name -> Student(startId + index, name) } -} +fun registerStudents(names: List, startId: Int = 0): List = + 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")) } \ No newline at end of file diff --git a/Functional Programming/Lambdas/Exercise 3/task-info.yaml b/Functional Programming/Lambdas/Exercise 3/task-info.yaml index 4bf524be..9c01face 100644 --- a/Functional Programming/Lambdas/Exercise 3/task-info.yaml +++ b/Functional Programming/Lambdas/Exercise 3/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 197 - length: 73 + length: 66 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Functional Programming/Member References/Exercise 1/src/Task.kt b/Functional Programming/Member References/Exercise 1/src/Task.kt index 021bdaf6..db99d1ad 100644 --- a/Functional Programming/Member References/Exercise 1/src/Task.kt +++ b/Functional Programming/Member References/Exercise 1/src/Task.kt @@ -12,7 +12,6 @@ data class Pet( enum class Habitat { LAND, WATER, AMPHIBIOUS; - fun livesIn(pet: Pet) = pet.habitat == this } diff --git a/Functional Programming/Member References/Exercise 1/task-info.yaml b/Functional Programming/Member References/Exercise 1/task-info.yaml index d57bf9b5..ae794cc1 100644 --- a/Functional Programming/Member References/Exercise 1/task-info.yaml +++ b/Functional Programming/Member References/Exercise 1/task-info.yaml @@ -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 diff --git a/Functional Programming/Operations on Collections/Exercise 1/src/Task.kt b/Functional Programming/Operations on Collections/Exercise 1/src/Task.kt index 51918d35..a7d96a23 100644 --- a/Functional Programming/Operations on Collections/Exercise 1/src/Task.kt +++ b/Functional Programming/Operations on Collections/Exercise 1/src/Task.kt @@ -4,10 +4,9 @@ import atomictest.eq data class Person(val name: String, val age: Int) -fun findOldest(people: List, quantity: Int = 1): List { - return people.sortedByDescending { it.age } +fun findOldest(people: List, quantity: Int = 1): List = + 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") -} +} \ No newline at end of file diff --git a/Functional Programming/Operations on Collections/Exercise 1/task-info.yaml b/Functional Programming/Operations on Collections/Exercise 1/task-info.yaml index ed327dc5..3297792b 100644 --- a/Functional Programming/Operations on Collections/Exercise 1/task-info.yaml +++ b/Functional Programming/Operations on Collections/Exercise 1/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 224 - length: 88 + length: 81 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Functional Programming/Operations on Collections/Exercise 2/src/Task.kt b/Functional Programming/Operations on Collections/Exercise 2/src/Task.kt index 0ae5b878..82acce8d 100644 --- a/Functional Programming/Operations on Collections/Exercise 2/src/Task.kt +++ b/Functional Programming/Operations on Collections/Exercise 2/src/Task.kt @@ -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') } diff --git a/Functional Programming/Operations on Collections/Exercise 2/task-info.yaml b/Functional Programming/Operations on Collections/Exercise 2/task-info.yaml index 30ccca63..ffa42df2 100644 --- a/Functional Programming/Operations on Collections/Exercise 2/task-info.yaml +++ b/Functional Programming/Operations on Collections/Exercise 2/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 129 - length: 89 + length: 88 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Functional Programming/Operations on Collections/Exercise 3/src/Task.kt b/Functional Programming/Operations on Collections/Exercise 3/src/Task.kt index 51dd33b5..3c29354a 100644 --- a/Functional Programming/Operations on Collections/Exercise 3/src/Task.kt +++ b/Functional Programming/Operations on Collections/Exercise 3/src/Task.kt @@ -2,9 +2,8 @@ package operationsOnCollectionsExercise3 import atomictest.eq -fun sum(list: List): Int { - return list.filterNotNull().sum() -} +fun sum(list: List): Int = + list.filterNotNull().sum() fun main() { sum(listOf(1, 2, null)) eq 3 diff --git a/Functional Programming/Operations on Collections/Exercise 3/task-info.yaml b/Functional Programming/Operations on Collections/Exercise 3/task-info.yaml index 12349086..780442e8 100644 --- a/Functional Programming/Operations on Collections/Exercise 3/task-info.yaml +++ b/Functional Programming/Operations on Collections/Exercise 3/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 134 - length: 33 + length: 26 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Functional Programming/Sequences/Exercise 2/src/Task.kt b/Functional Programming/Sequences/Exercise 2/src/Task.kt index 687c8377..3e0ca132 100644 --- a/Functional Programming/Sequences/Exercise 2/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 2/src/Task.kt @@ -1,4 +1,4 @@ -// Sequences/School1.kt +// Sequences/Task2.kt package sequencesExercise2 fun School.studentInstructors( diff --git a/Functional Programming/Sequences/Exercise 2/task-info.yaml b/Functional Programming/Sequences/Exercise 2/task-info.yaml index b88714e1..2164e428 100644 --- a/Functional Programming/Sequences/Exercise 2/task-info.yaml +++ b/Functional Programming/Sequences/Exercise 2/task-info.yaml @@ -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 diff --git a/Functional Programming/Sequences/Exercise 3/src/Task.kt b/Functional Programming/Sequences/Exercise 3/src/Task.kt index b41f1767..c78d3d7c 100644 --- a/Functional Programming/Sequences/Exercise 3/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 3/src/Task.kt @@ -1,4 +1,4 @@ -// Sequences/Task2.kt +// Sequences/Task3.kt package sequencesExercise3 fun School.averageInstructorRating(instructor: Instructor): Double = diff --git a/Functional Programming/Sequences/Exercise 4/src/Task.kt b/Functional Programming/Sequences/Exercise 4/src/Task.kt index db63c4b3..7ecd5c30 100644 --- a/Functional Programming/Sequences/Exercise 4/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 4/src/Task.kt @@ -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 } diff --git a/Functional Programming/Sequences/Exercise 4/task-info.yaml b/Functional Programming/Sequences/Exercise 4/task-info.yaml index f2fed0ea..aedaf146 100644 --- a/Functional Programming/Sequences/Exercise 4/task-info.yaml +++ b/Functional Programming/Sequences/Exercise 4/task-info.yaml @@ -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 diff --git a/Functional Programming/Sequences/Exercise 4/test/Tests.kt b/Functional Programming/Sequences/Exercise 4/test/Tests.kt index ae8af020..97ebd173 100644 --- a/Functional Programming/Sequences/Exercise 4/test/Tests.kt +++ b/Functional Programming/Sequences/Exercise 4/test/Tests.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) diff --git a/Functional Programming/Sequences/Exercise 5/src/Task.kt b/Functional Programming/Sequences/Exercise 5/src/Task.kt index c680955f..57004139 100644 --- a/Functional Programming/Sequences/Exercise 5/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 5/src/Task.kt @@ -1,4 +1,4 @@ -// Sequences/Task3.kt +// Sequences/Task5.kt package sequencesExercise5 fun School.instructorsWithLargestClass(): Set { diff --git a/Functional Programming/Sequences/Exercise 6/src/Task.kt b/Functional Programming/Sequences/Exercise 6/src/Task.kt index 5d5d70ae..5c20b122 100644 --- a/Functional Programming/Sequences/Exercise 6/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 6/src/Task.kt @@ -1,4 +1,4 @@ -// Sequences/School3.kt +// Sequences/Task6.kt package sequencesExercise6 import atomictest.eq diff --git a/Functional Programming/Sequences/Exercise 6/task-info.yaml b/Functional Programming/Sequences/Exercise 6/task-info.yaml index 02f4f784..2afed700 100644 --- a/Functional Programming/Sequences/Exercise 6/task-info.yaml +++ b/Functional Programming/Sequences/Exercise 6/task-info.yaml @@ -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 diff --git a/Functional Programming/Sequences/Exercise 7/src/Task.kt b/Functional Programming/Sequences/Exercise 7/src/Task.kt index 4135d05c..3fc8c00c 100644 --- a/Functional Programming/Sequences/Exercise 7/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 7/src/Task.kt @@ -1,4 +1,4 @@ -// Sequences/Task4.kt +// Sequences/Task7.kt package sequencesExercise7 fun oddWithout1(): Sequence { @@ -8,4 +8,4 @@ fun oddWithout1(): Sequence { fun main() { println(oddWithout1().take(20).sum()) -} +} \ No newline at end of file diff --git a/Functional Programming/Sequences/Exercise 8/src/Task.kt b/Functional Programming/Sequences/Exercise 8/src/Task.kt index 7ed19997..94c31c51 100644 --- a/Functional Programming/Sequences/Exercise 8/src/Task.kt +++ b/Functional Programming/Sequences/Exercise 8/src/Task.kt @@ -1,4 +1,4 @@ -// Sequences/School4.kt +// Sequences/Task8.kt package sequencesExercise8 import atomictest.eq diff --git a/Functional Programming/Sequences/Exercise 8/task-info.yaml b/Functional Programming/Sequences/Exercise 8/task-info.yaml index ee583efa..6ba71d97 100644 --- a/Functional Programming/Sequences/Exercise 8/task-info.yaml +++ b/Functional Programming/Sequences/Exercise 8/task-info.yaml @@ -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 diff --git a/Functional Programming/The Importance of Lambdas/Exercise 1/src/Task.kt b/Functional Programming/The Importance of Lambdas/Exercise 1/src/Task.kt index 82b94251..c9b2429f 100644 --- a/Functional Programming/The Importance of Lambdas/Exercise 1/src/Task.kt +++ b/Functional Programming/The Importance of Lambdas/Exercise 1/src/Task.kt @@ -4,9 +4,8 @@ import atomictest.eq class Person(val name: String, val age: Int) -fun List.getNamesOfAdults(): List { - return filter { it.age > 17 }.map { it.name } -} +fun List.getNamesOfAdults(): List = + filter { it.age > 17 }.map { it.name } fun main() { val people = listOf(Person("Alice", 17), Person("Bob", 19)) diff --git a/Functional Programming/The Importance of Lambdas/Exercise 1/task-info.yaml b/Functional Programming/The Importance of Lambdas/Exercise 1/task-info.yaml index 38c80fb9..5402ce13 100644 --- a/Functional Programming/The Importance of Lambdas/Exercise 1/task-info.yaml +++ b/Functional Programming/The Importance of Lambdas/Exercise 1/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 194 - length: 45 + length: 38 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Functional Programming/The Importance of Lambdas/Exercise 2/src/Task.kt b/Functional Programming/The Importance of Lambdas/Exercise 2/src/Task.kt index 3dad06b6..f5d37be2 100644 --- a/Functional Programming/The Importance of Lambdas/Exercise 2/src/Task.kt +++ b/Functional Programming/The Importance of Lambdas/Exercise 2/src/Task.kt @@ -2,9 +2,8 @@ package theImportanceOfLambdasExercise2 import atomictest.eq -fun filterNonBlank(strings: List): List { - return strings.filter { it.isNotBlank() } -} +fun filterNonBlank(strings: List): List = + strings.filter { it.isNotBlank() } fun main() { filterNonBlank(listOf("", "a", " ")) eq listOf("a") diff --git a/Functional Programming/The Importance of Lambdas/Exercise 2/task-info.yaml b/Functional Programming/The Importance of Lambdas/Exercise 2/task-info.yaml index 1a666137..3d56ba47 100644 --- a/Functional Programming/The Importance of Lambdas/Exercise 2/task-info.yaml +++ b/Functional Programming/The Importance of Lambdas/Exercise 2/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 154 - length: 41 + length: 34 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Introduction to Objects/Constraining Visibility/Exercise 1/src/Task.kt b/Introduction to Objects/Constraining Visibility/Exercise 1/src/Task.kt index 3fd8f04f..fc251d81 100644 --- a/Introduction to Objects/Constraining Visibility/Exercise 1/src/Task.kt +++ b/Introduction to Objects/Constraining Visibility/Exercise 1/src/Task.kt @@ -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) diff --git a/Introduction to Objects/Exceptions/Exercise 3/src/Task.kt b/Introduction to Objects/Exceptions/Exercise 3/src/Task.kt index 0c72635c..8ff20c9a 100644 --- a/Introduction to Objects/Exceptions/Exercise 3/src/Task.kt +++ b/Introduction to Objects/Exceptions/Exercise 3/src/Task.kt @@ -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 } diff --git a/Introduction to Objects/Exceptions/Exercise 3/task-info.yaml b/Introduction to Objects/Exceptions/Exercise 3/task-info.yaml index 90c7d24e..c1fe42bb 100644 --- a/Introduction to Objects/Exceptions/Exercise 3/task-info.yaml +++ b/Introduction to Objects/Exceptions/Exercise 3/task-info.yaml @@ -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 diff --git a/Introduction to Objects/Exceptions/Exercise 3/task.md b/Introduction to Objects/Exceptions/Exercise 3/task.md index 7a1efad5..09332e87 100644 --- a/Introduction to Objects/Exceptions/Exercise 3/task.md +++ b/Introduction to Objects/Exceptions/Exercise 3/task.md @@ -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`. diff --git a/Introduction to Objects/Lists/Exercise 2/src/Task.kt b/Introduction to Objects/Lists/Exercise 2/src/Task.kt index da49c6fe..229f3445 100644 --- a/Introduction to Objects/Lists/Exercise 2/src/Task.kt +++ b/Introduction to Objects/Lists/Exercise 2/src/Task.kt @@ -1,4 +1,4 @@ -// Lists/IntList1.kt +// Lists/Task2.kt package listsExercise2 fun countOccurrences(list: IntList, number: Int): Int { diff --git a/Introduction to Objects/Lists/Exercise 2/task-info.yaml b/Introduction to Objects/Lists/Exercise 2/task-info.yaml index af413b15..a7c3e786 100644 --- a/Introduction to Objects/Lists/Exercise 2/task-info.yaml +++ b/Introduction to Objects/Lists/Exercise 2/task-info.yaml @@ -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 diff --git a/Introduction to Objects/Lists/Exercise 3/src/Task.kt b/Introduction to Objects/Lists/Exercise 3/src/Task.kt index f65cfa4d..d0551e40 100644 --- a/Introduction to Objects/Lists/Exercise 3/src/Task.kt +++ b/Introduction to Objects/Lists/Exercise 3/src/Task.kt @@ -1,4 +1,4 @@ -// Lists/Task2.kt +// Lists/Task3.kt package listsExercise3 import atomictest.eq diff --git a/Introduction to Objects/Packages/Exercise 3/src/Task.kt b/Introduction to Objects/Packages/Exercise 3/src/EquilateralTriangle.kt similarity index 78% rename from Introduction to Objects/Packages/Exercise 3/src/Task.kt rename to Introduction to Objects/Packages/Exercise 3/src/EquilateralTriangle.kt index b3bf25e4..fbd09bd2 100644 --- a/Introduction to Objects/Packages/Exercise 3/src/Task.kt +++ b/Introduction to Objects/Packages/Exercise 3/src/EquilateralTriangle.kt @@ -1,4 +1,4 @@ -// Packages/aaa.kt +// Packages/EquilateralTriangle.kt package pythagorean import kotlin.math.sqrt diff --git a/Introduction to Objects/Packages/Exercise 3/src/Main.kt b/Introduction to Objects/Packages/Exercise 3/src/Task3.kt similarity index 87% rename from Introduction to Objects/Packages/Exercise 3/src/Main.kt rename to Introduction to Objects/Packages/Exercise 3/src/Task3.kt index 2b4b083a..8e0a6a13 100644 --- a/Introduction to Objects/Packages/Exercise 3/src/Main.kt +++ b/Introduction to Objects/Packages/Exercise 3/src/Task3.kt @@ -1,3 +1,4 @@ +// Packages/Task3.kt package packagesExercise3 import pythagorean.EquilateralTriangle diff --git a/Introduction to Objects/Packages/Exercise 3/task-info.yaml b/Introduction to Objects/Packages/Exercise 3/task-info.yaml index f620e1a1..c12ce81b 100644 --- a/Introduction to Objects/Packages/Exercise 3/task-info.yaml +++ b/Introduction to Objects/Packages/Exercise 3/task-info.yaml @@ -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 diff --git a/Object-Oriented Programming/Base Class Initialization/Exercise 2/src/Task.kt b/Object-Oriented Programming/Base Class Initialization/Exercise 2/src/Task.kt index 040c42df..6884f55d 100644 --- a/Object-Oriented Programming/Base Class Initialization/Exercise 2/src/Task.kt +++ b/Object-Oriented Programming/Base Class Initialization/Exercise 2/src/Task.kt @@ -1,4 +1,4 @@ -// BaseClassInit/Task3.kt +// BaseClassInit/Task2.kt package baseClassInitializationExercise2 import atomictest.trace diff --git a/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt b/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt index 1b235f97..c56fed8f 100644 --- a/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt +++ b/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt @@ -1,3 +1,4 @@ +// BaseClassInit/Task3.kt package baseClassInitializationExercise3 import atomictest.trace diff --git a/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml b/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml index 97177f67..5c35bdd8 100644 --- a/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml +++ b/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml @@ -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: |- /* diff --git a/Object-Oriented Programming/Secondary Constructors/Exercise 2/src/Task.kt b/Object-Oriented Programming/Secondary Constructors/Exercise 2/src/Task.kt index 106f6c70..8dfee98a 100644 --- a/Object-Oriented Programming/Secondary Constructors/Exercise 2/src/Task.kt +++ b/Object-Oriented Programming/Secondary Constructors/Exercise 2/src/Task.kt @@ -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)" } \ No newline at end of file diff --git a/Power Tools/Scope Functions/Exercise 2/src/Task.kt b/Power Tools/Scope Functions/Exercise 2/src/Task.kt index 3b8040d9..a10b0c12 100644 --- a/Power Tools/Scope Functions/Exercise 2/src/Task.kt +++ b/Power Tools/Scope Functions/Exercise 2/src/Task.kt @@ -1,4 +1,4 @@ -// ScopeFunctions/ScopeFuncSoln2.kt +// ScopeFunctions/Task2.kt package scopeFunctionsExercise2 import classdelegation.SpaceShipControls import atomictest.* diff --git a/Power Tools/Scope Functions/Exercise 2/task-info.yaml b/Power Tools/Scope Functions/Exercise 2/task-info.yaml index a54658de..66cec437 100644 --- a/Power Tools/Scope Functions/Exercise 2/task-info.yaml +++ b/Power Tools/Scope Functions/Exercise 2/task-info.yaml @@ -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 diff --git a/Usability/Data Classes/Exercise 1/src/Task.kt b/Usability/Data Classes/Exercise 1/src/Task.kt index 940cb019..e516299a 100644 --- a/Usability/Data Classes/Exercise 1/src/Task.kt +++ b/Usability/Data Classes/Exercise 1/src/Task.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) diff --git a/Usability/Data Classes/Exercise 1/task-info.yaml b/Usability/Data Classes/Exercise 1/task-info.yaml index ac46223a..7ebd0e0a 100644 --- a/Usability/Data Classes/Exercise 1/task-info.yaml +++ b/Usability/Data Classes/Exercise 1/task-info.yaml @@ -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 diff --git a/Usability/Data Classes/Exercise 2/src/Task.kt b/Usability/Data Classes/Exercise 2/src/Task.kt index 653b673f..b23649f1 100644 --- a/Usability/Data Classes/Exercise 2/src/Task.kt +++ b/Usability/Data Classes/Exercise 2/src/Task.kt @@ -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: diff --git a/Usability/Data Classes/Exercise 2/task-info.yaml b/Usability/Data Classes/Exercise 2/task-info.yaml index 5625433f..c89f9f33 100644 --- a/Usability/Data Classes/Exercise 2/task-info.yaml +++ b/Usability/Data Classes/Exercise 2/task-info.yaml @@ -4,7 +4,7 @@ files: visible: true placeholders: - offset: 304 - length: 70 + length: 74 placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/Usability/Destructuring Declarations/Exercise 1/src/Task.kt b/Usability/Destructuring Declarations/Exercise 1/src/Task.kt index a724e15c..eee7de3e 100644 --- a/Usability/Destructuring Declarations/Exercise 1/src/Task.kt +++ b/Usability/Destructuring Declarations/Exercise 1/src/Task.kt @@ -2,19 +2,21 @@ package destructuringDeclarationsExercise1 import atomictest.eq -fun calculate(n1: Int, n2: Int): Triple { - return if (n1 < 0 || n2 < 0) Triple(false, 0, 0) - else Triple(true, n1 + n2, n1 * n2) +fun calculate( + n1: Int, n2: Int +): Triple { + 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 } diff --git a/Usability/Destructuring Declarations/Exercise 1/task-info.yaml b/Usability/Destructuring Declarations/Exercise 1/task-info.yaml index 11857a1a..91ef9af2 100644 --- a/Usability/Destructuring Declarations/Exercise 1/task-info.yaml +++ b/Usability/Destructuring Declarations/Exercise 1/task-info.yaml @@ -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) diff --git a/Usability/Destructuring Declarations/Exercise 2/src/Task.kt b/Usability/Destructuring Declarations/Exercise 2/src/Task.kt index 0d688028..6de5a4af 100644 --- a/Usability/Destructuring Declarations/Exercise 2/src/Task.kt +++ b/Usability/Destructuring Declarations/Exercise 2/src/Task.kt @@ -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" } diff --git a/Usability/Destructuring Declarations/Exercise 2/task-info.yaml b/Usability/Destructuring Declarations/Exercise 2/task-info.yaml index d401b1f4..3e70a846 100644 --- a/Usability/Destructuring Declarations/Exercise 2/task-info.yaml +++ b/Usability/Destructuring Declarations/Exercise 2/task-info.yaml @@ -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 diff --git a/Usability/Destructuring Declarations/Exercise 3/src/Task.kt b/Usability/Destructuring Declarations/Exercise 3/src/Task.kt index ee5eb092..d29de69c 100644 --- a/Usability/Destructuring Declarations/Exercise 3/src/Task.kt +++ b/Usability/Destructuring Declarations/Exercise 3/src/Task.kt @@ -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) } \ No newline at end of file diff --git a/Usability/Destructuring Declarations/Exercise 3/task-info.yaml b/Usability/Destructuring Declarations/Exercise 3/task-info.yaml index 3b7e3003..c1ea77be 100644 --- a/Usability/Destructuring Declarations/Exercise 3/task-info.yaml +++ b/Usability/Destructuring Declarations/Exercise 3/task-info.yaml @@ -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) diff --git a/Usability/Extension Properties/Exercise 2/src/Task.kt b/Usability/Extension Properties/Exercise 2/src/Task.kt index b83e790c..176f5bd9 100644 --- a/Usability/Extension Properties/Exercise 2/src/Task.kt +++ b/Usability/Extension Properties/Exercise 2/src/Task.kt @@ -6,8 +6,6 @@ val List.reversed: List get() = reversed() fun main() { -/* val list = listOf(1, 2, 3) list.reversed eq listOf(3, 2, 1) -*/ } \ No newline at end of file diff --git a/Usability/Extension Properties/Exercise 2/task-info.yaml b/Usability/Extension Properties/Exercise 2/task-info.yaml index a7776941..6020dd21 100644 --- a/Usability/Extension Properties/Exercise 2/task-info.yaml +++ b/Usability/Extension Properties/Exercise 2/task-info.yaml @@ -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 diff --git a/Usability/Extension Properties/Exercise 3/src/Task.kt b/Usability/Extension Properties/Exercise 3/src/Task.kt index 3887f77c..29af3fc3 100644 --- a/Usability/Extension Properties/Exercise 3/src/Task.kt +++ b/Usability/Extension Properties/Exercise 3/src/Task.kt @@ -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 -*/ } \ No newline at end of file diff --git a/Usability/Extension Properties/Exercise 3/task-info.yaml b/Usability/Extension Properties/Exercise 3/task-info.yaml index 2e531dc6..a06a0036 100644 --- a/Usability/Extension Properties/Exercise 3/task-info.yaml +++ b/Usability/Extension Properties/Exercise 3/task-info.yaml @@ -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 diff --git a/Usability/Extensions for Nullable Types/Exercise 3/src/Task.kt b/Usability/Extensions for Nullable Types/Exercise 3/src/Task.kt index 3d43c5da..82bb5b6f 100644 --- a/Usability/Extensions for Nullable Types/Exercise 3/src/Task.kt +++ b/Usability/Extensions for Nullable Types/Exercise 3/src/Task.kt @@ -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 -*/ } \ No newline at end of file diff --git a/Usability/Extensions for Nullable Types/Exercise 3/task-info.yaml b/Usability/Extensions for Nullable Types/Exercise 3/task-info.yaml index 660ac6ff..81323b7f 100644 --- a/Usability/Extensions for Nullable Types/Exercise 3/task-info.yaml +++ b/Usability/Extensions for Nullable Types/Exercise 3/task-info.yaml @@ -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 diff --git a/Usability/Introduction to Generics/Exercise 3/src/Task.kt b/Usability/Introduction to Generics/Exercise 3/src/Task.kt index 637cf860..e0ff1b79 100644 --- a/Usability/Introduction to Generics/Exercise 3/src/Task.kt +++ b/Usability/Introduction to Generics/Exercise 3/src/Task.kt @@ -17,7 +17,6 @@ class CountingSet { } fun main() { -/* val cs = CountingSet() 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 -*/ } \ No newline at end of file diff --git a/Usability/Introduction to Generics/Exercise 3/task-info.yaml b/Usability/Introduction to Generics/Exercise 3/task-info.yaml index 9e9e7964..3b12e0e5 100644 --- a/Usability/Introduction to Generics/Exercise 3/task-info.yaml +++ b/Usability/Introduction to Generics/Exercise 3/task-info.yaml @@ -6,5 +6,17 @@ files: - offset: 88 length: 238 placeholder_text: class CountingSet + - offset: 341 + length: 149 + placeholder_text: |- + /* + val cs = CountingSet() + 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 diff --git a/Usability/Named & Default Arguments/Exercise 1/src/Task.kt b/Usability/Named & Default Arguments/Exercise 1/src/Task.kt index 4ca99905..88a034f1 100644 --- a/Usability/Named & Default Arguments/Exercise 1/src/Task.kt +++ b/Usability/Named & Default Arguments/Exercise 1/src/Task.kt @@ -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")) } \ No newline at end of file diff --git a/Usability/Named & Default Arguments/Exercise 3/src/Task.kt b/Usability/Named & Default Arguments/Exercise 3/src/Task.kt index 67e56e31..51fb2b1e 100644 --- a/Usability/Named & Default Arguments/Exercise 3/src/Task.kt +++ b/Usability/Named & Default Arguments/Exercise 3/src/Task.kt @@ -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" } \ No newline at end of file diff --git a/Usability/Non-null Assertions/Exercise 1/src/Task.kt b/Usability/Non-null Assertions/Exercise 1/src/Task.kt index 1cf95657..bf3a1b98 100644 --- a/Usability/Non-null Assertions/Exercise 1/src/Task.kt +++ b/Usability/Non-null Assertions/Exercise 1/src/Task.kt @@ -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" } \ No newline at end of file diff --git a/Usability/Non-null Assertions/Exercise 1/task-info.yaml b/Usability/Non-null Assertions/Exercise 1/task-info.yaml index c2d247e0..fe845293 100644 --- a/Usability/Non-null Assertions/Exercise 1/task-info.yaml +++ b/Usability/Non-null Assertions/Exercise 1/task-info.yaml @@ -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 diff --git a/Usability/Non-null Assertions/Exercise 1/task.md b/Usability/Non-null Assertions/Exercise 1/task.md index 2741718b..073cbbd4 100644 --- a/Usability/Non-null Assertions/Exercise 1/task.md +++ b/Usability/Non-null Assertions/Exercise 1/task.md @@ -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`. + + 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! diff --git a/Usability/Non-null Assertions/Exercise 1/test/Tests.kt b/Usability/Non-null Assertions/Exercise 1/test/Tests.kt index ae153eb8..a5a5061b 100644 --- a/Usability/Non-null Assertions/Exercise 1/test/Tests.kt +++ b/Usability/Non-null Assertions/Exercise 1/test/Tests.kt @@ -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") } +*/ } } \ No newline at end of file diff --git a/Usability/Non-null Assertions/Exercise 2/src/Task.kt b/Usability/Non-null Assertions/Exercise 2/src/Task.kt new file mode 100644 index 00000000..e4e2b53a --- /dev/null +++ b/Usability/Non-null Assertions/Exercise 2/src/Task.kt @@ -0,0 +1,37 @@ +// NonNullAssertions/Task2.kt +package nonNullAssertionsExercise2 +import atomictest.* + +fun List.headPlusTail(): Triple = + when { + isEmpty() -> Triple(null, null, null) + size == 1 -> Triple(first(), null, first()) + else -> Triple(first(), last(), first() + last()) + } + +fun main() { + val ints = mutableListOf() + 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 + """ +} \ No newline at end of file diff --git a/Usability/Non-null Assertions/Exercise 2/task-info.yaml b/Usability/Non-null Assertions/Exercise 2/task-info.yaml new file mode 100644 index 00000000..15697f32 --- /dev/null +++ b/Usability/Non-null Assertions/Exercise 2/task-info.yaml @@ -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.headPlusTail() +- name: test/Tests.kt + visible: false diff --git a/Usability/Non-null Assertions/Exercise 2/task.md b/Usability/Non-null Assertions/Exercise 2/task.md new file mode 100644 index 00000000..4cfd7352 --- /dev/null +++ b/Usability/Non-null Assertions/Exercise 2/task.md @@ -0,0 +1,18 @@ +## Non-`null` Assertions (#2) + +Define an extension function `List.headPlusTail()` that returns a `Triple` +containing (1) the first element in the `List`, (2) the last element in the +`List`, (3) the sum of the first and last elements. If the `List` is +empty, return `null` for all three entries of the `Triple`. If the `List` +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` 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. + + 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! diff --git a/Usability/Non-null Assertions/Exercise 2/test/Tests.kt b/Usability/Non-null Assertions/Exercise 2/test/Tests.kt new file mode 100644 index 00000000..9f2eadd5 --- /dev/null +++ b/Usability/Non-null Assertions/Exercise 2/test/Tests.kt @@ -0,0 +1,10 @@ +package nonNullAssertionsExercise2 + +import org.junit.Test +import util.unimplementedTest + +class TestNonNullAssertionsExercise2 { + @Test fun testSolution() { + unimplementedTest() + } +} \ No newline at end of file diff --git a/Usability/Non-null Assertions/lesson-info.yaml b/Usability/Non-null Assertions/lesson-info.yaml index c2fc4652..c9fb967a 100644 --- a/Usability/Non-null Assertions/lesson-info.yaml +++ b/Usability/Non-null Assertions/lesson-info.yaml @@ -1,4 +1,5 @@ content: - Examples - Exercise 1 +- Exercise 2 - Exercise 3 diff --git a/Usability/Nullable Types/Exercise 2/src/Task.kt b/Usability/Nullable Types/Exercise 2/src/Task.kt index a8917071..205e0196 100644 --- a/Usability/Nullable Types/Exercise 2/src/Task.kt +++ b/Usability/Nullable Types/Exercise 2/src/Task.kt @@ -1,4 +1,10 @@ // NullableTypes/Task2.kt package nullableTypesExercise2 -// Type your solution here \ No newline at end of file +fun zilch(): String? = null +fun zilch2(): String? = zilch() + +fun main() { + var x: String? = zilch() + var y: String? = zilch2() +} \ No newline at end of file diff --git a/Usability/Nullable Types/Exercise 2/task.md b/Usability/Nullable Types/Exercise 2/task.md index 4d4ae3cb..0b5d61d6 100644 --- a/Usability/Nullable Types/Exercise 2/task.md +++ b/Usability/Nullable Types/Exercise 2/task.md @@ -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? + + 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! diff --git a/Usability/Safe Calls & the Elvis Operator/Exercise 1/src/Task.kt b/Usability/Safe Calls & the Elvis Operator/Exercise 1/src/Task.kt index da7368df..2fa2614e 100644 --- a/Usability/Safe Calls & the Elvis Operator/Exercise 1/src/Task.kt +++ b/Usability/Safe Calls & the Elvis Operator/Exercise 1/src/Task.kt @@ -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" -*/ } \ No newline at end of file diff --git a/Usability/Safe Calls & the Elvis Operator/Exercise 1/task-info.yaml b/Usability/Safe Calls & the Elvis Operator/Exercise 1/task-info.yaml index 3c8104a3..89bec6b1 100644 --- a/Usability/Safe Calls & the Elvis Operator/Exercise 1/task-info.yaml +++ b/Usability/Safe Calls & the Elvis Operator/Exercise 1/task-info.yaml @@ -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 diff --git a/Usability/break & continue/Exercise 1/src/Task.kt b/Usability/break & continue/Exercise 1/src/Task.kt index 1384857b..254c0d09 100644 --- a/Usability/break & continue/Exercise 1/src/Task.kt +++ b/Usability/break & continue/Exercise 1/src/Task.kt @@ -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 - */ \ No newline at end of file + readNumbers("1", "a", "3", "10", "0", "19") + trace eq """ + Not a number: a + Sum: 14 + """ +} \ No newline at end of file diff --git a/Usability/break & continue/Exercise 1/task-info.yaml b/Usability/break & continue/Exercise 1/task-info.yaml index 65256658..7f3cf4c0 100644 --- a/Usability/break & continue/Exercise 1/task-info.yaml +++ b/Usability/break & continue/Exercise 1/task-info.yaml @@ -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 diff --git a/Usability/break & continue/Exercise 1/test/Tests.kt b/Usability/break & continue/Exercise 1/test/Tests.kt index 552b9f4b..4042fa87 100644 --- a/Usability/break & continue/Exercise 1/test/Tests.kt +++ b/Usability/break & continue/Exercise 1/test/Tests.kt @@ -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, input: List) { + 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")) } \ No newline at end of file diff --git a/util/test/TestAllExamples.java b/util/test/TestAllExamples.java index dc1b3398..dfb3af1e 100644 --- a/util/test/TestAllExamples.java +++ b/util/test/TestAllExamples.java @@ -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