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

Commit

Permalink
Resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Feb 19, 2019
1 parent 41c044e commit 912a865
Show file tree
Hide file tree
Showing 43 changed files with 200 additions and 100 deletions.
4 changes: 1 addition & 3 deletions src/i_introduction/_0_Hello_World/n00Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ fun todoTask0(): Nothing = TODO(
references = { task0(); "OK" }
)

fun task0(): String {
return todoTask0()
}
fun task0() = "OK"
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ fun todoTask10(): Nothing = TODO(

fun task10(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, todoTask10())
Collections.sort(arrayList, object : Comparator<Int> {
override fun compare(x: Int, y: Int) = y - x
})
return arrayList
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fun todoTask11(): Nothing = TODO(

fun task11(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, { x, y -> todoTask11() })
Collections.sort(arrayList, { x, y -> y - x })
return arrayList
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fun todoTask12(): Nothing = TODO(
)

fun task12(): List<Int> {
todoTask12()
return arrayListOf(1, 5, 2)
return arrayListOf(1, 5, 2).sortedDescending()
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ fun todoTask1(collection: Collection<Int>): Nothing = TODO(


fun task1(collection: Collection<Int>): 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()
}
3 changes: 1 addition & 2 deletions src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ fun todoTask2(): Nothing = TODO(
references = { collection: Collection<Int> -> task1(collection); collection.joinToString() })

fun task2(collection: Collection<Int>): String {
todoTask2()
return collection.joinToString()
return collection.joinToString(prefix = "{", postfix = "}")
}
12 changes: 6 additions & 6 deletions src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion src/i_introduction/_4_Lambdas/n04Lambdas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ fun todoTask4(collection: Collection<Int>): Nothing = TODO(
documentation = doc4(),
references = { JavaCode4().task4(collection) })

fun task4(collection: Collection<Int>): Boolean = todoTask4(collection)
fun task4(collection: Collection<Int>): Boolean = collection.any { it % 2 == 0 }
Original file line number Diff line number Diff line change
Expand Up @@ -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}"""
6 changes: 3 additions & 3 deletions src/i_introduction/_6_Data_Classes/n06DataClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> {
todoTask6()
return listOf(/*Person("Alice", 29), Person("Bob", 31)*/)
// todoTask6()
return listOf(Person("Alice", 29), Person("Bob", 31))
}

8 changes: 7 additions & 1 deletion src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
4 changes: 2 additions & 2 deletions src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun todoTask9(): Nothing = TODO(

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

fun Int.r(): RationalNumber = todoTask9()
fun Pair<Int, Int>.r(): RationalNumber = todoTask9()
fun Int.r(): RationalNumber = RationalNumber(this, 1)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(this.first, this.second)


3 changes: 1 addition & 2 deletions src/ii_collections/n13Introduction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fun example0(list: List<Int>) {

fun Shop.getSetOfCustomers(): Set<Customer> {
// Return a set containing all the customers of this shop
todoCollectionTask()
// return this.customers
return customers.toSet()
}

4 changes: 2 additions & 2 deletions src/ii_collections/n14FilterMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fun example1(list: List<Int>) {

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

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


11 changes: 6 additions & 5 deletions src/ii_collections/n15AllAnyAndOtherPredicates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@ fun example2(list: List<Int>) {

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) }
}

4 changes: 2 additions & 2 deletions src/ii_collections/n16FlatMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ fun example() {
val Customer.orderedProducts: Set<Product>
get() {
// Return all products this customer has ordered
todoCollectionTask()
return orders.flatMap { it.products }.toSet()
}

val Shop.allOrderedProducts: Set<Product>
get() {
// Return all products that were ordered by at least one customer
todoCollectionTask()
return customers.flatMap { it.orderedProducts }.toSet()
}
4 changes: 2 additions & 2 deletions src/ii_collections/n17MaxMin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
2 changes: 1 addition & 1 deletion src/ii_collections/n18Sort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ fun example5() {

fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> {
// Return a list of customers, sorted by the ascending number of orders they made
todoCollectionTask()
return customers.sortedBy { it.orders.size }
}
2 changes: 1 addition & 1 deletion src/ii_collections/n19Sum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
2 changes: 1 addition & 1 deletion src/ii_collections/n20GroupBy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ fun example7() {

fun Shop.groupCustomersByCity(): Map<City, List<Customer>> {
// Return a map of the customers living in each city
todoCollectionTask()
return customers.groupBy { it.city }
}
6 changes: 5 additions & 1 deletion src/ii_collections/n21Partition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ fun example8() {

fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> {
// 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()
}

2 changes: 1 addition & 1 deletion src/ii_collections/n22Fold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fun whatFoldDoes(): Int {
fun Shop.getSetOfProductsOrderedByEachCustomer(): Set<Product> {
// Return the set of products that were ordered by each of the customers
return customers.fold(allOrderedProducts, { orderedByAll, customer ->
todoCollectionTask()
orderedByAll.intersect(customer.orderedProducts)
})
}
6 changes: 3 additions & 3 deletions src/ii_collections/n23CompoundTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package ii_collections

fun Shop.getCustomersWhoOrderedProduct(product: Product): Set<Customer> {
// 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 }
}
4 changes: 2 additions & 2 deletions src/ii_collections/n24ExtensionsOnCollections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fun todoTask24(): Nothing = TODO(
)

fun doSomethingStrangeWithCollection(collection: Collection<String>): Collection<String>? {
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 }
}

34 changes: 31 additions & 3 deletions src/iii_conventions/MyDate.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
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<MyDate> {
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<MyDate>, Iterable<MyDate> {
override fun iterator(): Iterator<MyDate> = 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<MyDate> {
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,
WEEK,
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)
4 changes: 2 additions & 2 deletions src/iii_conventions/n25Comparison.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun todoTask25(): Nothing = TODO(
)

fun task25(date1: MyDate, date2: MyDate): Boolean {
todoTask25()
// return date1 < date2
// todoTask25()
return date1 < date2
}

4 changes: 2 additions & 2 deletions src/iii_conventions/n26InRange.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions src/iii_conventions/n27RangeTo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions src/iii_conventions/n28ForLoop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading

0 comments on commit 912a865

Please sign in to comment.