Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 655e6e0

Browse files
committed
Resolutions
1 parent 36d3c33 commit 655e6e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+200
-100
lines changed

src/i_introduction/_0_Hello_World/n00Start.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ fun todoTask0(): Nothing = TODO(
2424
references = { task0(); "OK" }
2525
)
2626

27-
fun task0(): String {
28-
return todoTask0()
29-
}
27+
fun task0() = "OK"

src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ fun todoTask10(): Nothing = TODO(
1818

1919
fun task10(): List<Int> {
2020
val arrayList = arrayListOf(1, 5, 2)
21-
Collections.sort(arrayList, todoTask10())
21+
Collections.sort(arrayList, object : Comparator<Int> {
22+
override fun compare(x: Int, y: Int) = y - x
23+
})
2224
return arrayList
2325
}

src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fun todoTask11(): Nothing = TODO(
1515

1616
fun task11(): List<Int> {
1717
val arrayList = arrayListOf(1, 5, 2)
18-
Collections.sort(arrayList, { x, y -> todoTask11() })
18+
Collections.sort(arrayList, { x, y -> y - x })
1919
return arrayList
2020
}

src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fun todoTask12(): Nothing = TODO(
1717
)
1818

1919
fun task12(): List<Int> {
20-
todoTask12()
21-
return arrayListOf(1, 5, 2)
20+
return arrayListOf(1, 5, 2).sortedDescending()
2221
}
2322

src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,16 @@ fun todoTask1(collection: Collection<Int>): Nothing = TODO(
1414

1515

1616
fun task1(collection: Collection<Int>): String {
17-
todoTask1(collection)
17+
val sb = StringBuilder()
18+
sb.append("{")
19+
val iterator = collection.iterator()
20+
while (iterator.hasNext()) {
21+
val element = iterator.next()
22+
sb.append(element)
23+
if (iterator.hasNext()) {
24+
sb.append(", ")
25+
}
26+
}
27+
sb.append("}")
28+
return sb.toString()
1829
}

src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ fun todoTask2(): Nothing = TODO(
2424
references = { collection: Collection<Int> -> task1(collection); collection.joinToString() })
2525

2626
fun task2(collection: Collection<Int>): String {
27-
todoTask2()
28-
return collection.joinToString()
27+
return collection.joinToString(prefix = "{", postfix = "}")
2928
}

src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ fun todoTask3(): Nothing = TODO(
1414
documentation = doc2(),
1515
references = { name: String -> JavaCode3().foo(name); foo(name) })
1616

17-
fun foo(name: String): String = todoTask3()
17+
fun foo(name: String, number: Int = 42, toUpperCase: Boolean = false) =
18+
(if (toUpperCase) name.toUpperCase() else name) + number
1819

1920
fun task3(): String {
20-
todoTask3()
21-
// return (foo("a") +
22-
// foo("b", number = 1) +
23-
// foo("c", toUpperCase = true) +
24-
// foo(name = "d", number = 2, toUpperCase = true))
21+
return (foo("a") +
22+
foo("b", number = 1) +
23+
foo("c", toUpperCase = true) +
24+
foo(name = "d", number = 2, toUpperCase = true))
2525
}

src/i_introduction/_4_Lambdas/n04Lambdas.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ fun todoTask4(collection: Collection<Int>): Nothing = TODO(
2222
documentation = doc4(),
2323
references = { JavaCode4().task4(collection) })
2424

25-
fun task4(collection: Collection<Int>): Boolean = todoTask4(collection)
25+
fun task4(collection: Collection<Int>): Boolean = collection.any { it % 2 == 0 }

src/i_introduction/_5_String_Templates/n05StringTemplates.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ fun todoTask5(): Nothing = TODO(
3535
documentation = doc5(),
3636
references = { getPattern(); month })
3737

38-
fun task5(): String = todoTask5()
38+
fun task5(): String = """\d{2} $month \d{4}"""

src/i_introduction/_6_Data_Classes/n06DataClasses.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ fun todoTask6(): Nothing = TODO(
1515
references = { JavaCode6.Person("Alice", 29) }
1616
)
1717

18-
class Person
18+
data class Person(val name: String, val age: Int)
1919

2020
fun task6(): List<Person> {
21-
todoTask6()
22-
return listOf(/*Person("Alice", 29), Person("Bob", 31)*/)
21+
// todoTask6()
22+
return listOf(Person("Alice", 29), Person("Bob", 31))
2323
}
2424

src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ fun todoTask7(client: Client?, message: String?, mailer: Mailer): Nothing = TODO
2525
fun sendMessageToClient(
2626
client: Client?, message: String?, mailer: Mailer
2727
) {
28-
todoTask7(client, message, mailer)
28+
// todoTask7(client, message, mailer)
29+
val email = client?.personalInfo?.email
30+
31+
if (email != null && message != null) {
32+
33+
mailer.sendMessage(email, message)
34+
}
2935
}
3036

3137
class Client(val personalInfo: PersonalInfo?)

src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Sum(val left: Expr, val right: Expr) : Expr()
1212

1313
fun eval(e: Expr): Int =
1414
when (e) {
15-
is Num -> todoTask8(e)
16-
is Sum -> todoTask8(e)
15+
is Num -> e.value
16+
is Sum -> eval(e.left) + eval(e.right)
1717
}
1818

1919
fun todoTask8(expr: Expr): Nothing = TODO(

src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fun todoTask9(): Nothing = TODO(
2929

3030
data class RationalNumber(val numerator: Int, val denominator: Int)
3131

32-
fun Int.r(): RationalNumber = todoTask9()
33-
fun Pair<Int, Int>.r(): RationalNumber = todoTask9()
32+
fun Int.r(): RationalNumber = RationalNumber(this, 1)
33+
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(this.first, this.second)
3434

3535

src/ii_collections/n13Introduction.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ fun example0(list: List<Int>) {
1818

1919
fun Shop.getSetOfCustomers(): Set<Customer> {
2020
// Return a set containing all the customers of this shop
21-
todoCollectionTask()
22-
// return this.customers
21+
return customers.toSet()
2322
}
2423

src/ii_collections/n14FilterMap.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fun example1(list: List<Int>) {
1010

1111
fun Shop.getCitiesCustomersAreFrom(): Set<City> {
1212
// Return the set of cities the customers are from
13-
todoCollectionTask()
13+
return customers.map { it.city }.toSet()
1414
}
1515

1616
fun Shop.getCustomersFrom(city: City): List<Customer> {
1717
// Return a list of the customers who live in the given city
18-
todoCollectionTask()
18+
return customers.filter { it.city == city }
1919
}
2020

2121

src/ii_collections/n15AllAnyAndOtherPredicates.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ fun example2(list: List<Int>) {
1515

1616
fun Customer.isFrom(city: City): Boolean {
1717
// Return true if the customer is from the given city
18-
todoCollectionTask()
18+
return this.city == city
1919
}
2020

2121
fun Shop.checkAllCustomersAreFrom(city: City): Boolean {
2222
// Return true if all customers are from the given city
23-
todoCollectionTask()
23+
return customers.all { it.isFrom(city) }
2424
}
2525

2626
fun Shop.hasCustomerFrom(city: City): Boolean {
2727
// Return true if there is at least one customer from the given city
28-
todoCollectionTask()
28+
return customers.any { it.isFrom(city) }
2929
}
3030

3131
fun Shop.countCustomersFrom(city: City): Int {
3232
// Return the number of customers from the given city
33-
todoCollectionTask()
33+
return customers.count { it.isFrom(city) }
3434
}
3535

3636
fun Shop.findFirstCustomerFrom(city: City): Customer? {
3737
// Return the first customer who lives in the given city, or null if there is none
38-
todoCollectionTask()
38+
return customers.firstOrNull { it.isFrom(city) }
3939
}
40+

src/ii_collections/n16FlatMap.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ fun example() {
1010
val Customer.orderedProducts: Set<Product>
1111
get() {
1212
// Return all products this customer has ordered
13-
todoCollectionTask()
13+
return orders.flatMap { it.products }.toSet()
1414
}
1515

1616
val Shop.allOrderedProducts: Set<Product>
1717
get() {
1818
// Return all products that were ordered by at least one customer
19-
todoCollectionTask()
19+
return customers.flatMap { it.orderedProducts }.toSet()
2020
}

src/ii_collections/n17MaxMin.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ fun example4() {
77

88
fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? {
99
// Return a customer whose order count is the highest among all customers
10-
todoCollectionTask()
10+
return customers.maxBy { it.orders.size }
1111
}
1212

1313
fun Customer.getMostExpensiveOrderedProduct(): Product? {
1414
// Return the most expensive product which has been ordered
15-
todoCollectionTask()
15+
return orderedProducts.maxBy { it.price }
1616
}

src/ii_collections/n18Sort.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ fun example5() {
88

99
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> {
1010
// Return a list of customers, sorted by the ascending number of orders they made
11-
todoCollectionTask()
11+
return customers.sortedBy { it.orders.size }
1212
}

src/ii_collections/n19Sum.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ fun example6() {
88
fun Customer.getTotalOrderPrice(): Double {
99
// Return the sum of prices of all products that a customer has ordered.
1010
// Note: a customer may order the same product several times.
11-
todoCollectionTask()
11+
return orders.flatMap { it.products }.sumByDouble { it.price }
1212
}

src/ii_collections/n20GroupBy.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ fun example7() {
88

99
fun Shop.groupCustomersByCity(): Map<City, List<Customer>> {
1010
// Return a map of the customers living in each city
11-
todoCollectionTask()
11+
return customers.groupBy { it.city }
1212
}

src/ii_collections/n21Partition.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ fun example8() {
1212

1313
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> {
1414
// Return customers who have more undelivered orders than delivered
15-
todoCollectionTask()
15+
return customers.filter {
16+
val (delivered, undelivered) = it.orders.partition { it.isDelivered }
17+
undelivered.size > delivered.size
18+
}.toSet()
1619
}
20+

src/ii_collections/n22Fold.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fun whatFoldDoes(): Int {
1515
fun Shop.getSetOfProductsOrderedByEachCustomer(): Set<Product> {
1616
// Return the set of products that were ordered by each of the customers
1717
return customers.fold(allOrderedProducts, { orderedByAll, customer ->
18-
todoCollectionTask()
18+
orderedByAll.intersect(customer.orderedProducts)
1919
})
2020
}

src/ii_collections/n23CompoundTasks.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package ii_collections
22

33
fun Shop.getCustomersWhoOrderedProduct(product: Product): Set<Customer> {
44
// Return the set of customers who ordered the specified product
5-
todoCollectionTask()
5+
return customers.filter { it.orderedProducts.contains(product) }.toSet()
66
}
77

88
fun Customer.getMostExpensiveDeliveredProduct(): Product? {
99
// Return the most expensive product among all delivered products
1010
// (use the Order.isDelivered flag)
11-
todoCollectionTask()
11+
return orders.filter { it.isDelivered }.flatMap { it.products }.maxBy { it.price }
1212
}
1313

1414
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int {
1515
// Return the number of times the given product was ordered.
1616
// Note: a customer may order the same product for several times.
17-
todoCollectionTask()
17+
return customers.flatMap { it.orders }.flatMap { it.products }.count { it == product }
1818
}

src/ii_collections/n24ExtensionsOnCollections.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fun todoTask24(): Nothing = TODO(
1212
)
1313

1414
fun doSomethingStrangeWithCollection(collection: Collection<String>): Collection<String>? {
15-
val groupsByLength = collection.groupBy { s -> todoTask24() }
15+
val groupsByLength = collection.groupBy { s -> s.length }
1616

17-
return groupsByLength.values.maxBy { group -> todoTask24() }
17+
return groupsByLength.values.maxBy { group -> group.size }
1818
}
1919

src/iii_conventions/MyDate.kt

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
package iii_conventions
22

3-
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)
3+
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int): Comparable<MyDate> {
4+
override fun compareTo(other: MyDate) = when {
5+
year != other.year -> year - other.year
6+
month != other.month -> month - other.month
7+
else -> dayOfMonth - other.dayOfMonth
8+
}
9+
}
10+
11+
operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other)
12+
13+
class DateRange(
14+
override val start: MyDate,
15+
override val endInclusive: MyDate
16+
) : ClosedRange<MyDate>, Iterable<MyDate> {
17+
override fun iterator(): Iterator<MyDate> = DateIterator(this)
18+
override fun contains(value: MyDate): Boolean = start <= value && value <= endInclusive
19+
}
420

5-
operator fun MyDate.rangeTo(other: MyDate): DateRange = todoTask27()
21+
class DateIterator(val dateRange: DateRange) : Iterator<MyDate> {
22+
var current: MyDate = dateRange.start
23+
override fun next(): MyDate {
24+
val result = current
25+
current = current.nextDay()
26+
return result
27+
}
28+
override fun hasNext(): Boolean = current <= dateRange.endInclusive
29+
}
630

731
enum class TimeInterval {
832
DAY,
933
WEEK,
1034
YEAR
1135
}
1236

13-
class DateRange(val start: MyDate, val endInclusive: MyDate)
37+
class RepeatedTimeInterval(val timeInterval: TimeInterval, val number: Int)
38+
operator fun TimeInterval.times(number: Int) = RepeatedTimeInterval(this, number)
39+
40+
operator fun MyDate.plus(timeInterval: TimeInterval) = addTimeIntervals(timeInterval, 1)
41+
operator fun MyDate.plus(timeIntervals: RepeatedTimeInterval) = addTimeIntervals(timeIntervals.timeInterval, timeIntervals.number)

src/iii_conventions/n25Comparison.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fun todoTask25(): Nothing = TODO(
1616
)
1717

1818
fun task25(date1: MyDate, date2: MyDate): Boolean {
19-
todoTask25()
20-
// return date1 < date2
19+
// todoTask25()
20+
return date1 < date2
2121
}
2222

src/iii_conventions/n26InRange.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ fun todoTask26_(): Nothing = TODO(
1818
)
1919

2020
fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
21-
todoTask26_()
22-
// return date in DateRange(first, last)
21+
// todoTask26_()
22+
return date in DateRange(first, last)
2323
}

src/iii_conventions/n27RangeTo.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fun todoTask27(): Nothing = TODO(
1515
)
1616

1717
fun checkInRange2(date: MyDate, first: MyDate, last: MyDate): Boolean {
18-
todoTask27()
19-
// return date in first..last
18+
// todoTask27()
19+
return date in first..last
2020
}

src/iii_conventions/n28ForLoop.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fun todoTask28(): Nothing = TODO(
3737

3838

3939
fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) {
40-
todoTask28()
41-
// for (date in firstDate..secondDate) {
42-
// handler(date)
43-
// }
40+
// todoTask28()
41+
for (date in firstDate..secondDate) {
42+
handler(date)
43+
}
4444
}

0 commit comments

Comments
 (0)