diff --git a/src/i_introduction/_0_Hello_World/n00Start.kt b/src/i_introduction/_0_Hello_World/n00Start.kt index 352ca5bfc..1850528c2 100644 --- a/src/i_introduction/_0_Hello_World/n00Start.kt +++ b/src/i_introduction/_0_Hello_World/n00Start.kt @@ -24,6 +24,4 @@ fun todoTask0(): Nothing = TODO( references = { task0(); "OK" } ) -fun task0(): String { - return todoTask0() -} \ No newline at end of file +fun task0() = "OK" \ No newline at end of file diff --git a/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt b/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt index ef12d8127..235766dc2 100644 --- a/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt +++ b/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt @@ -18,6 +18,8 @@ fun todoTask10(): Nothing = TODO( fun task10(): List { val arrayList = arrayListOf(1, 5, 2) - Collections.sort(arrayList, todoTask10()) + Collections.sort(arrayList, object : Comparator { + override fun compare(x: Int, y: Int) = y - x + }) return arrayList } \ No newline at end of file diff --git a/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt b/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt index 5f021921c..68a3dd5bc 100644 --- a/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt +++ b/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt @@ -15,6 +15,6 @@ fun todoTask11(): Nothing = TODO( fun task11(): List { val arrayList = arrayListOf(1, 5, 2) - Collections.sort(arrayList, { x, y -> todoTask11() }) + Collections.sort(arrayList, { x, y -> y - x }) return arrayList } diff --git a/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt b/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt index ae453a63b..dac9162a2 100644 --- a/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt +++ b/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt @@ -17,7 +17,6 @@ fun todoTask12(): Nothing = TODO( ) fun task12(): List { - todoTask12() - return arrayListOf(1, 5, 2) + return arrayListOf(1, 5, 2).sortedDescending() } diff --git a/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt b/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt index f7a40f5e8..bcab8face 100644 --- a/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt +++ b/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt @@ -14,5 +14,16 @@ fun todoTask1(collection: Collection): Nothing = TODO( fun task1(collection: Collection): String { - todoTask1(collection) + val sb = StringBuilder() + sb.append("{") + val iterator = collection.iterator() + while (iterator.hasNext()) { + val element = iterator.next() + sb.append(element) + if (iterator.hasNext()) { + sb.append(", ") + } + } + sb.append("}") + return sb.toString() } \ No newline at end of file diff --git a/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt b/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt index 11fc442bb..b1dd036f4 100644 --- a/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt +++ b/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt @@ -24,6 +24,5 @@ fun todoTask2(): Nothing = TODO( references = { collection: Collection -> task1(collection); collection.joinToString() }) fun task2(collection: Collection): String { - todoTask2() - return collection.joinToString() + return collection.joinToString(prefix = "{", postfix = "}") } \ No newline at end of file diff --git a/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt b/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt index b7adbd670..25100c70b 100644 --- a/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt +++ b/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt @@ -14,12 +14,12 @@ fun todoTask3(): Nothing = TODO( documentation = doc2(), references = { name: String -> JavaCode3().foo(name); foo(name) }) -fun foo(name: String): String = todoTask3() +fun foo(name: String, number: Int = 42, toUpperCase: Boolean = false) = + (if (toUpperCase) name.toUpperCase() else name) + number fun task3(): String { - todoTask3() -// return (foo("a") + -// foo("b", number = 1) + -// foo("c", toUpperCase = true) + -// foo(name = "d", number = 2, toUpperCase = true)) + return (foo("a") + + foo("b", number = 1) + + foo("c", toUpperCase = true) + + foo(name = "d", number = 2, toUpperCase = true)) } \ No newline at end of file diff --git a/src/i_introduction/_4_Lambdas/n04Lambdas.kt b/src/i_introduction/_4_Lambdas/n04Lambdas.kt index 3c174fc77..4a3d7e070 100644 --- a/src/i_introduction/_4_Lambdas/n04Lambdas.kt +++ b/src/i_introduction/_4_Lambdas/n04Lambdas.kt @@ -22,4 +22,4 @@ fun todoTask4(collection: Collection): Nothing = TODO( documentation = doc4(), references = { JavaCode4().task4(collection) }) -fun task4(collection: Collection): Boolean = todoTask4(collection) \ No newline at end of file +fun task4(collection: Collection): Boolean = collection.any { it % 2 == 0 } \ No newline at end of file diff --git a/src/i_introduction/_5_String_Templates/n05StringTemplates.kt b/src/i_introduction/_5_String_Templates/n05StringTemplates.kt index 7b7f9b625..05dbaf086 100644 --- a/src/i_introduction/_5_String_Templates/n05StringTemplates.kt +++ b/src/i_introduction/_5_String_Templates/n05StringTemplates.kt @@ -35,4 +35,4 @@ fun todoTask5(): Nothing = TODO( documentation = doc5(), references = { getPattern(); month }) -fun task5(): String = todoTask5() +fun task5(): String = """\d{2} $month \d{4}""" \ No newline at end of file diff --git a/src/i_introduction/_6_Data_Classes/n06DataClasses.kt b/src/i_introduction/_6_Data_Classes/n06DataClasses.kt index 1ce6b8796..6b5a46f9a 100644 --- a/src/i_introduction/_6_Data_Classes/n06DataClasses.kt +++ b/src/i_introduction/_6_Data_Classes/n06DataClasses.kt @@ -15,10 +15,10 @@ fun todoTask6(): Nothing = TODO( references = { JavaCode6.Person("Alice", 29) } ) -class Person +data class Person(val name: String, val age: Int) fun task6(): List { - todoTask6() - return listOf(/*Person("Alice", 29), Person("Bob", 31)*/) +// todoTask6() + return listOf(Person("Alice", 29), Person("Bob", 31)) } diff --git a/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt b/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt index 1d0109436..77a79a3e8 100644 --- a/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt +++ b/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt @@ -25,7 +25,13 @@ fun todoTask7(client: Client?, message: String?, mailer: Mailer): Nothing = TODO fun sendMessageToClient( client: Client?, message: String?, mailer: Mailer ) { - todoTask7(client, message, mailer) +// todoTask7(client, message, mailer) + val email = client?.personalInfo?.email + + if (email != null && message != null) { + + mailer.sendMessage(email, message) + } } class Client(val personalInfo: PersonalInfo?) diff --git a/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt b/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt index 6f3a6675e..03ad236fe 100644 --- a/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt +++ b/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt @@ -12,8 +12,8 @@ class Sum(val left: Expr, val right: Expr) : Expr() fun eval(e: Expr): Int = when (e) { - is Num -> todoTask8(e) - is Sum -> todoTask8(e) + is Num -> e.value + is Sum -> eval(e.left) + eval(e.right) } fun todoTask8(expr: Expr): Nothing = TODO( diff --git a/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt b/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt index 75506f232..dc831e29e 100644 --- a/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt +++ b/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt @@ -29,7 +29,7 @@ fun todoTask9(): Nothing = TODO( data class RationalNumber(val numerator: Int, val denominator: Int) -fun Int.r(): RationalNumber = todoTask9() -fun Pair.r(): RationalNumber = todoTask9() +fun Int.r(): RationalNumber = RationalNumber(this, 1) +fun Pair.r(): RationalNumber = RationalNumber(this.first, this.second) diff --git a/src/ii_collections/n13Introduction.kt b/src/ii_collections/n13Introduction.kt index 6a67983c8..32f8fe4b0 100644 --- a/src/ii_collections/n13Introduction.kt +++ b/src/ii_collections/n13Introduction.kt @@ -18,7 +18,6 @@ fun example0(list: List) { fun Shop.getSetOfCustomers(): Set { // Return a set containing all the customers of this shop - todoCollectionTask() -// return this.customers + return customers.toSet() } diff --git a/src/ii_collections/n14FilterMap.kt b/src/ii_collections/n14FilterMap.kt index c547b44b6..4a45002e3 100644 --- a/src/ii_collections/n14FilterMap.kt +++ b/src/ii_collections/n14FilterMap.kt @@ -10,12 +10,12 @@ fun example1(list: List) { fun Shop.getCitiesCustomersAreFrom(): Set { // Return the set of cities the customers are from - todoCollectionTask() + return customers.map { it.city }.toSet() } fun Shop.getCustomersFrom(city: City): List { // Return a list of the customers who live in the given city - todoCollectionTask() + return customers.filter { it.city == city } } diff --git a/src/ii_collections/n15AllAnyAndOtherPredicates.kt b/src/ii_collections/n15AllAnyAndOtherPredicates.kt index 15b417317..c3e7ae128 100644 --- a/src/ii_collections/n15AllAnyAndOtherPredicates.kt +++ b/src/ii_collections/n15AllAnyAndOtherPredicates.kt @@ -15,25 +15,26 @@ fun example2(list: List) { fun Customer.isFrom(city: City): Boolean { // Return true if the customer is from the given city - todoCollectionTask() + return this.city == city } fun Shop.checkAllCustomersAreFrom(city: City): Boolean { // Return true if all customers are from the given city - todoCollectionTask() + return customers.all { it.isFrom(city) } } fun Shop.hasCustomerFrom(city: City): Boolean { // Return true if there is at least one customer from the given city - todoCollectionTask() + return customers.any { it.isFrom(city) } } fun Shop.countCustomersFrom(city: City): Int { // Return the number of customers from the given city - todoCollectionTask() + return customers.count { it.isFrom(city) } } fun Shop.findFirstCustomerFrom(city: City): Customer? { // Return the first customer who lives in the given city, or null if there is none - todoCollectionTask() + return customers.firstOrNull { it.isFrom(city) } } + diff --git a/src/ii_collections/n16FlatMap.kt b/src/ii_collections/n16FlatMap.kt index 22f9b92f4..73c1c1445 100644 --- a/src/ii_collections/n16FlatMap.kt +++ b/src/ii_collections/n16FlatMap.kt @@ -10,11 +10,11 @@ fun example() { val Customer.orderedProducts: Set get() { // Return all products this customer has ordered - todoCollectionTask() + return orders.flatMap { it.products }.toSet() } val Shop.allOrderedProducts: Set get() { // Return all products that were ordered by at least one customer - todoCollectionTask() + return customers.flatMap { it.orderedProducts }.toSet() } diff --git a/src/ii_collections/n17MaxMin.kt b/src/ii_collections/n17MaxMin.kt index 78b09060b..01feb93d4 100644 --- a/src/ii_collections/n17MaxMin.kt +++ b/src/ii_collections/n17MaxMin.kt @@ -7,10 +7,10 @@ fun example4() { fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? { // Return a customer whose order count is the highest among all customers - todoCollectionTask() + return customers.maxBy { it.orders.size } } fun Customer.getMostExpensiveOrderedProduct(): Product? { // Return the most expensive product which has been ordered - todoCollectionTask() + return orderedProducts.maxBy { it.price } } diff --git a/src/ii_collections/n18Sort.kt b/src/ii_collections/n18Sort.kt index 1bbbe4887..4c3a92aad 100644 --- a/src/ii_collections/n18Sort.kt +++ b/src/ii_collections/n18Sort.kt @@ -8,5 +8,5 @@ fun example5() { fun Shop.getCustomersSortedByNumberOfOrders(): List { // Return a list of customers, sorted by the ascending number of orders they made - todoCollectionTask() + return customers.sortedBy { it.orders.size } } diff --git a/src/ii_collections/n19Sum.kt b/src/ii_collections/n19Sum.kt index bfd89c56b..53a19b0d0 100644 --- a/src/ii_collections/n19Sum.kt +++ b/src/ii_collections/n19Sum.kt @@ -8,5 +8,5 @@ fun example6() { fun Customer.getTotalOrderPrice(): Double { // Return the sum of prices of all products that a customer has ordered. // Note: a customer may order the same product several times. - todoCollectionTask() + return orders.flatMap { it.products }.sumByDouble { it.price } } diff --git a/src/ii_collections/n20GroupBy.kt b/src/ii_collections/n20GroupBy.kt index dd364ea93..5fcdfed32 100644 --- a/src/ii_collections/n20GroupBy.kt +++ b/src/ii_collections/n20GroupBy.kt @@ -8,5 +8,5 @@ fun example7() { fun Shop.groupCustomersByCity(): Map> { // Return a map of the customers living in each city - todoCollectionTask() + return customers.groupBy { it.city } } diff --git a/src/ii_collections/n21Partition.kt b/src/ii_collections/n21Partition.kt index f95b0da35..031d6fefe 100644 --- a/src/ii_collections/n21Partition.kt +++ b/src/ii_collections/n21Partition.kt @@ -12,5 +12,9 @@ fun example8() { fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set { // Return customers who have more undelivered orders than delivered - todoCollectionTask() + return customers.filter { + val (delivered, undelivered) = it.orders.partition { it.isDelivered } + undelivered.size > delivered.size + }.toSet() } + diff --git a/src/ii_collections/n22Fold.kt b/src/ii_collections/n22Fold.kt index 2fd2f2fcb..cfe1d4952 100644 --- a/src/ii_collections/n22Fold.kt +++ b/src/ii_collections/n22Fold.kt @@ -15,6 +15,6 @@ fun whatFoldDoes(): Int { fun Shop.getSetOfProductsOrderedByEachCustomer(): Set { // Return the set of products that were ordered by each of the customers return customers.fold(allOrderedProducts, { orderedByAll, customer -> - todoCollectionTask() + orderedByAll.intersect(customer.orderedProducts) }) } diff --git a/src/ii_collections/n23CompoundTasks.kt b/src/ii_collections/n23CompoundTasks.kt index 35bd7eb7b..0ab5d18bd 100644 --- a/src/ii_collections/n23CompoundTasks.kt +++ b/src/ii_collections/n23CompoundTasks.kt @@ -2,17 +2,17 @@ package ii_collections fun Shop.getCustomersWhoOrderedProduct(product: Product): Set { // Return the set of customers who ordered the specified product - todoCollectionTask() + return customers.filter { it.orderedProducts.contains(product) }.toSet() } fun Customer.getMostExpensiveDeliveredProduct(): Product? { // Return the most expensive product among all delivered products // (use the Order.isDelivered flag) - todoCollectionTask() + return orders.filter { it.isDelivered }.flatMap { it.products }.maxBy { it.price } } fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int { // Return the number of times the given product was ordered. // Note: a customer may order the same product for several times. - todoCollectionTask() + return customers.flatMap { it.orders }.flatMap { it.products }.count { it == product } } diff --git a/src/ii_collections/n24ExtensionsOnCollections.kt b/src/ii_collections/n24ExtensionsOnCollections.kt index 3188c180e..dc064821d 100644 --- a/src/ii_collections/n24ExtensionsOnCollections.kt +++ b/src/ii_collections/n24ExtensionsOnCollections.kt @@ -12,8 +12,8 @@ fun todoTask24(): Nothing = TODO( ) fun doSomethingStrangeWithCollection(collection: Collection): Collection? { - val groupsByLength = collection.groupBy { s -> todoTask24() } + val groupsByLength = collection.groupBy { s -> s.length } - return groupsByLength.values.maxBy { group -> todoTask24() } + return groupsByLength.values.maxBy { group -> group.size } } diff --git a/src/iii_conventions/MyDate.kt b/src/iii_conventions/MyDate.kt index 147cc3e78..fc51e4a47 100644 --- a/src/iii_conventions/MyDate.kt +++ b/src/iii_conventions/MyDate.kt @@ -1,8 +1,32 @@ package iii_conventions -data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) +data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int): Comparable { + override fun compareTo(other: MyDate) = when { + year != other.year -> year - other.year + month != other.month -> month - other.month + else -> dayOfMonth - other.dayOfMonth + } +} + +operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other) + +class DateRange( + override val start: MyDate, + override val endInclusive: MyDate +) : ClosedRange, Iterable { + override fun iterator(): Iterator = DateIterator(this) + override fun contains(value: MyDate): Boolean = start <= value && value <= endInclusive +} -operator fun MyDate.rangeTo(other: MyDate): DateRange = todoTask27() +class DateIterator(val dateRange: DateRange) : Iterator { + var current: MyDate = dateRange.start + override fun next(): MyDate { + val result = current + current = current.nextDay() + return result + } + override fun hasNext(): Boolean = current <= dateRange.endInclusive +} enum class TimeInterval { DAY, @@ -10,4 +34,8 @@ enum class TimeInterval { YEAR } -class DateRange(val start: MyDate, val endInclusive: MyDate) +class RepeatedTimeInterval(val timeInterval: TimeInterval, val number: Int) +operator fun TimeInterval.times(number: Int) = RepeatedTimeInterval(this, number) + +operator fun MyDate.plus(timeInterval: TimeInterval) = addTimeIntervals(timeInterval, 1) +operator fun MyDate.plus(timeIntervals: RepeatedTimeInterval) = addTimeIntervals(timeIntervals.timeInterval, timeIntervals.number) diff --git a/src/iii_conventions/n25Comparison.kt b/src/iii_conventions/n25Comparison.kt index 70d5bec60..402b21cdb 100644 --- a/src/iii_conventions/n25Comparison.kt +++ b/src/iii_conventions/n25Comparison.kt @@ -16,7 +16,7 @@ fun todoTask25(): Nothing = TODO( ) fun task25(date1: MyDate, date2: MyDate): Boolean { - todoTask25() -// return date1 < date2 +// todoTask25() + return date1 < date2 } diff --git a/src/iii_conventions/n26InRange.kt b/src/iii_conventions/n26InRange.kt index 5a57d2dab..7fa9082a3 100644 --- a/src/iii_conventions/n26InRange.kt +++ b/src/iii_conventions/n26InRange.kt @@ -18,6 +18,6 @@ fun todoTask26_(): Nothing = TODO( ) fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean { - todoTask26_() -// return date in DateRange(first, last) +// todoTask26_() + return date in DateRange(first, last) } diff --git a/src/iii_conventions/n27RangeTo.kt b/src/iii_conventions/n27RangeTo.kt index bfc4f96c5..72b29571c 100644 --- a/src/iii_conventions/n27RangeTo.kt +++ b/src/iii_conventions/n27RangeTo.kt @@ -15,6 +15,6 @@ fun todoTask27(): Nothing = TODO( ) fun checkInRange2(date: MyDate, first: MyDate, last: MyDate): Boolean { - todoTask27() -// return date in first..last +// todoTask27() + return date in first..last } diff --git a/src/iii_conventions/n28ForLoop.kt b/src/iii_conventions/n28ForLoop.kt index 1f4369566..d4181cba1 100644 --- a/src/iii_conventions/n28ForLoop.kt +++ b/src/iii_conventions/n28ForLoop.kt @@ -37,8 +37,8 @@ fun todoTask28(): Nothing = TODO( fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) { - todoTask28() -// for (date in firstDate..secondDate) { -// handler(date) -// } +// todoTask28() + for (date in firstDate..secondDate) { + handler(date) + } } diff --git a/src/iii_conventions/n29OperatorsOverloading.kt b/src/iii_conventions/n29OperatorsOverloading.kt index 3f96b416f..69b6ca432 100644 --- a/src/iii_conventions/n29OperatorsOverloading.kt +++ b/src/iii_conventions/n29OperatorsOverloading.kt @@ -20,12 +20,12 @@ fun todoTask29(): Nothing = TODO( }) fun task29_1(today: MyDate): MyDate { - todoTask29() -// return today + YEAR + WEEK +// todoTask29() + return today + YEAR + WEEK } fun task29_2(today: MyDate): MyDate { - todoTask29() -// return today + YEAR * 2 + WEEK * 3 + DAY * 5 +// todoTask29() + return today + YEAR * 2 + WEEK * 3 + DAY * 5 } diff --git a/src/iii_conventions/n30DestructuringDeclarations.kt b/src/iii_conventions/n30DestructuringDeclarations.kt index ed5b757bb..b3811bcb6 100644 --- a/src/iii_conventions/n30DestructuringDeclarations.kt +++ b/src/iii_conventions/n30DestructuringDeclarations.kt @@ -11,14 +11,14 @@ fun todoTask30(): Nothing = TODO( documentation = doc30() ) -class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) +data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) fun isLeapDay(date: MyDate): Boolean { - todoTask30() -// val (year, month, dayOfMonth) = date -// -// // 29 February of a leap year -// return isLeapYear(year) && month == 1 && dayOfMonth == 29 +// todoTask30() + val (year, month, dayOfMonth) = date + + // 29 February of a leap year + return isLeapYear(year) && month == 1 && dayOfMonth == 29 } // Years which are multiples of four (with the exception of years divisible by 100 but not by 400) diff --git a/src/iii_conventions/n31Invoke.kt b/src/iii_conventions/n31Invoke.kt index 7feb8a21e..51ea4f5ef 100644 --- a/src/iii_conventions/n31Invoke.kt +++ b/src/iii_conventions/n31Invoke.kt @@ -3,7 +3,15 @@ package iii_conventions import util.TODO -class Invokable +class Invokable(private var invocations: Int = 0) { + operator fun invoke(): Invokable { + invocations++ + return this + } + + fun getNumberOfInvocations() = invocations +} + fun todoTask31(): Nothing = TODO( """ @@ -14,6 +22,6 @@ fun todoTask31(): Nothing = TODO( references = { invokable: Invokable -> }) fun task31(invokable: Invokable): Int { - todoTask31() -// return invokable()()()().getNumberOfInvocations() +// todoTask31() + return invokable()()()().getNumberOfInvocations() } diff --git a/src/iv_properties/n32Properties.kt b/src/iv_properties/n32Properties.kt index 3f2b51f37..fb04eba27 100644 --- a/src/iv_properties/n32Properties.kt +++ b/src/iv_properties/n32Properties.kt @@ -5,7 +5,12 @@ import util.doc32 class PropertyExample() { var counter = 0 - var propertyWithCounter: Int? = todoTask32() + var propertyWithCounter: Int? = null + set(v) { + field = v + counter++ + } + } fun todoTask32(): Nothing = TODO( diff --git a/src/iv_properties/n33LazyProperty.kt b/src/iv_properties/n33LazyProperty.kt index 2da09cb24..cc9c12198 100644 --- a/src/iv_properties/n33LazyProperty.kt +++ b/src/iv_properties/n33LazyProperty.kt @@ -3,7 +3,16 @@ package iv_properties import util.TODO class LazyProperty(val initializer: () -> Int) { - val lazy: Int = todoTask33() + private var lazyValue: Int? = null + get() { + if (field == null) field = initializer() + return field + } + val lazy: Int + get() = lazyValue!! + + + } fun todoTask33(): Nothing = TODO( diff --git a/src/iv_properties/n34DelegatesExamples.kt b/src/iv_properties/n34DelegatesExamples.kt index 11ad7d481..c306b8374 100644 --- a/src/iv_properties/n34DelegatesExamples.kt +++ b/src/iv_properties/n34DelegatesExamples.kt @@ -4,7 +4,7 @@ import util.TODO import util.doc34 class LazyPropertyUsingDelegates(val initializer: () -> Int) { - val lazyValue: Int by todoTask34() + val lazyValue: Int by lazy(initializer) } fun todoTask34(): Lazy = TODO( diff --git a/src/iv_properties/n35HowDelegatesWork.kt b/src/iv_properties/n35HowDelegatesWork.kt index 0462f5556..2f4ff4de4 100644 --- a/src/iv_properties/n35HowDelegatesWork.kt +++ b/src/iv_properties/n35HowDelegatesWork.kt @@ -29,8 +29,8 @@ class D { class EffectiveDate : ReadWriteProperty { var timeInMillis: Long? = null - operator override fun getValue(thisRef: R, property: KProperty<*>): MyDate = todoTask35() - operator override fun setValue(thisRef: R, property: KProperty<*>, value: MyDate) = todoTask35() + operator override fun getValue(thisRef: R, property: KProperty<*>): MyDate = timeInMillis!!.toDate() + operator override fun setValue(thisRef: R, property: KProperty<*>, value: MyDate) { timeInMillis = value.toMillis() } } fun MyDate.toMillis(): Long { diff --git a/src/v_builders/n36ExtensionFunctionLiterals.kt b/src/v_builders/n36ExtensionFunctionLiterals.kt index 066c44a26..b1ea35e37 100644 --- a/src/v_builders/n36ExtensionFunctionLiterals.kt +++ b/src/v_builders/n36ExtensionFunctionLiterals.kt @@ -14,8 +14,8 @@ fun todoTask36(): Nothing = TODO( ) fun task36(): List { - val isEven: Int.() -> Boolean = { todoTask36() } - val isOdd: Int.() -> Boolean = { todoTask36() } + val isEven: Int.() -> Boolean = { this % 2 == 0 } + val isOdd: Int.() -> Boolean = { !isEven() } return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven()) } diff --git a/src/v_builders/n37StringAndMapBuilders.kt b/src/v_builders/n37StringAndMapBuilders.kt index 76e554f5a..96b0b8ba9 100644 --- a/src/v_builders/n37StringAndMapBuilders.kt +++ b/src/v_builders/n37StringAndMapBuilders.kt @@ -27,12 +27,18 @@ fun todoTask37(): Nothing = TODO( """ ) +fun buildMap(build: MutableMap.() -> Unit): Map { + val map = HashMap() + map.build() + return map +} + fun task37(): Map { - todoTask37() -// return buildMap { -// put(0, "0") -// for (i in 1..10) { -// put(i, "$i") -// } -// } +// todoTask37() + return buildMap { + put(0, "0") + for (i in 1..10) { + put(i, "$i") + } + } } diff --git a/src/v_builders/n38TheFunctionApply.kt b/src/v_builders/n38TheFunctionApply.kt index 61a677850..90686b3e4 100644 --- a/src/v_builders/n38TheFunctionApply.kt +++ b/src/v_builders/n38TheFunctionApply.kt @@ -9,7 +9,8 @@ fun todoTask38(): Nothing = TODO( ) fun T.myApply(f: T.() -> Unit): T { - todoTask38() + f() + return this } fun buildString(): String { diff --git a/src/v_builders/n39HtmlBuilders.kt b/src/v_builders/n39HtmlBuilders.kt index 578d50521..0547e75d9 100644 --- a/src/v_builders/n39HtmlBuilders.kt +++ b/src/v_builders/n39HtmlBuilders.kt @@ -22,7 +22,7 @@ fun todoTask39(): Nothing = TODO( fun renderProductTable(): String { return html { table { - tr { + tr (color = getTitleColor()) { td { text("Product") } @@ -34,7 +34,20 @@ fun renderProductTable(): String { } } val products = getProducts() - todoTask39() +// todoTask39() + for ((index, product) in products.withIndex()) { + tr { + td (color = getCellColor(index, 0)) { + text(product.description) + } + td (color = getCellColor(index, 1)) { + text(product.price) + } + td (color = getCellColor(index, 2)) { + text(product.popularity) + } + } + } } }.toString() } diff --git a/src/v_builders/n40BuildersHowItWorks.kt b/src/v_builders/n40BuildersHowItWorks.kt index 95a8a705e..023ed20bf 100644 --- a/src/v_builders/n40BuildersHowItWorks.kt +++ b/src/v_builders/n40BuildersHowItWorks.kt @@ -32,7 +32,7 @@ fun task40() = linkedMapOf( b. function declaration c. function invocation */ - 1 to insertAnswerHere(), + 1 to c, /* 2. In the Kotlin code @@ -49,7 +49,7 @@ fun task40() = linkedMapOf( b. argument name c. argument value */ - 2 to insertAnswerHere(), + 2 to b, /* 3. The block @@ -62,7 +62,7 @@ from the previous question is: c. something mysterious */ - 3 to insertAnswerHere(), + 3 to b, /* 4. For the code @@ -84,5 +84,5 @@ which of the following is true: } } */ - 4 to insertAnswerHere() + 4 to c ) diff --git a/src/vi_generics/n41GenericFunctions.kt b/src/vi_generics/n41GenericFunctions.kt index 38d333c9c..f12bdc66b 100644 --- a/src/vi_generics/n41GenericFunctions.kt +++ b/src/vi_generics/n41GenericFunctions.kt @@ -19,12 +19,23 @@ fun task41(): Nothing = TODO( } ) +fun > Collection.partitionTo(first: C, second: C, predicate: (T) -> Boolean): Pair { + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + fun List.partitionWordsAndLines(): Pair, List> { - task41() -// return partitionTo(ArrayList(), ArrayList()) { s -> !s.contains(" ") } +// task41() + return partitionTo(ArrayList(), ArrayList()) { s -> !s.contains(" ") } } fun Set.partitionLettersAndOtherSymbols(): Pair, Set> { - task41() -// return partitionTo(HashSet(), HashSet()) { c -> c in 'a'..'z' || c in 'A'..'Z'} +// task41() + return partitionTo(HashSet(), HashSet()) { c -> c in 'a'..'z' || c in 'A'..'Z'} } \ No newline at end of file