1
1
Fork 0

Sync with the book project

This commit is contained in:
Svetlana Isakova 2021-07-21 13:58:46 +02:00
parent 1f912d4287
commit 79426a20a6
45 changed files with 78 additions and 78 deletions

View File

@ -24,7 +24,7 @@ fun List<Pet>.liveInWater(): List<Pet> =
fun List<Pet>.areAmphibious(): List<Pet> =
filter(AMPHIBIOUS::livesIn)
fun List<Pet>.partAmphibious(): Pair<List<Pet>, List<Pet>> =
fun List<Pet>.partitionAmphibious(): Pair<List<Pet>, List<Pet>> =
partition(AMPHIBIOUS::livesIn)
fun main() {
@ -38,6 +38,6 @@ fun main() {
pets.liveOnLand() eq "[Dog, Cat]"
pets.liveInWater() eq "[Goldfish]"
pets.areAmphibious() eq "[Turtle, Frog]"
pets.partAmphibious() eq
pets.partitionAmphibious() eq
"([Turtle, Frog], [Dog, Cat, Goldfish])"
}

View File

@ -12,7 +12,7 @@ files:
- offset: 515
length: 27
placeholder_text: TODO()
- offset: 607
- offset: 612
length: 30
placeholder_text: TODO()
- name: test/Tests.kt

View File

@ -10,5 +10,5 @@ The `main()` starter code creates a `List<Pet>`. Using `filter()` together with
member references, implement three functions `liveOnLand()`, `liveInWater()`
and `areAmphibious()` that discover which pets in the list live on land, in the
water, or are amphibious. Lastly, use `partition()` to implement the
`partAmphibious()` function that divides the pets into those that are
`partitionAmphibious()` function that divides the pets into those that are
amphibious and those that are not.

View File

@ -86,7 +86,7 @@ class TestMemberReferencesExercise1 {
) {
assertEquals(
expected = expected.let { (pos, neg) -> pos.toSet() to neg.toSet() },
actual = initial.partAmphibious().let { (pos, neg) -> pos.toSet() to neg.toSet() },
actual = initial.partitionAmphibious().let { (pos, neg) -> pos.toSet() to neg.toSet() },
message = "Wrong result for 'partAmphibious()' called on $initial:"
)
}

View File

@ -4,7 +4,7 @@ Write a function `oddWithout1()` that creates an infinite sequence of odd
numbers starting with `3` that do not contain the digit `1` in its decimal
representation:
```
```text
3, 5, 7, 9, 23, 25, 27, 29, 33 ...
```

View File

@ -5,7 +5,7 @@ number. For `right()`, `left()`, `down()` and `up()`, a negative or zero
`steps` argument should not update the location. Instead it should produce
console output using this `String`:
```
```text
"steps argument must be positive, is $steps"
```

View File

@ -3,7 +3,7 @@
IntelliJ IDEA allows you to extract repetitive code into a function. Select the
following code in the body of the first function:
```
```text
if (x <= 0) {
println("Incorrect input: $x should be positive")
return

View File

@ -5,7 +5,7 @@ Create a `Robot` class with the following four member functions:
`up(steps: Int)`. Each function should display one of the following
phrases on the console:
```
```text
Right N steps
Left N steps
Down N steps

View File

@ -3,7 +3,7 @@
Display to the console all of the following `String`s that can't be converted to
`Double` (that is, those where an attempt to convert it throws an exception):
```
```text
"12", "1.2", "1,2", "1.2e0", "1.2e1",
"1.2e2", "1.2e3", "1.2e10", "12.3e10",
"1.2e-1", "1.2e-10".

View File

@ -3,13 +3,13 @@
Palindromes are words or phrases that read the same forward and backward. For
example:
- "mom" is a palindrome
- "mom" is a palindrome
- "dad" is a palindrome
- "dad" is a palindrome
- "rotator" is a palindrome
- "rotator" is a palindrome
- "streets" is *not* a palindrome
- "streets" is *not* a palindrome
Write a function that checks whether a word or phrase is a palindrome.

View File

@ -5,18 +5,18 @@ The starter code defines three packages `aaa`, `bbb` and `ccc` via the files
In the first package `aaa`, define:
- `val x = 10`
- `val x = 10`
- A top-level function `fa()` that takes an `Int` parameter and returns that
- A top-level function `fa()` that takes an `Int` parameter and returns that
argument multiplied by `x`
- A class `K` with a `toString()` that returns `"K"`
- A class `K` with a `toString()` that returns `"K"`
In `package bbb`, `import` all the components from `aaa` and use them in a
function `g()`, which takes an `Int` parameter `i` and returns the following
`String` literal using members from `aaa`:
```
```text
"${K()} ${fa(i)}"
```

View File

@ -4,7 +4,7 @@ A `Robot` object starts in the `(0, 0)` cell and can move right, left, up and
down. Its location is stored as a pair of coordinates `(x, y)`, where `(0, 0)`
is the top-left corner:
```
```text
(x, y)
(0, 0) (1, 0)
(0, 1) (1, 1)

View File

@ -4,7 +4,7 @@ A `Robot` can move within a square field of size `100 x 100`. The borders of
this field are "looped": if the robot is in cell `(0, 0)` and moves up one
step, it arrives in cell `(0, 99)`.
```
```text
(0, 0)
// up(1)
(0, 99)

View File

@ -2,7 +2,7 @@
Create a `Dictionary` class that stores translations for words. It includes a
member function `addTranslations(word: String, translations: String)`. The
`translations` for `word` are delimited by whitespaces. If `word` is already
`translations` for `word` are delimited by whitespace. If `word` is already
present in the `Dictionary`, `addTranslations()` throws an
`IllegalArgumentException` with the message
`"Dictionary already has translations for '$word'"`.

View File

@ -18,10 +18,10 @@ the initial `number` equals the sum of the `remainder` and `result`.
Store the Roman numerals in a mapping from `Int` to the associated `String`
representation. For each pair `int = roman` starting from `1000 = M`:
- Divide `remainder` into `int` to discover how many times you need to repeat
`roman` in the `result`.
- Divide `remainder` into `int` to discover how many times you need to repeat
`roman` in the `result`.
- Subtract the result from `remainder` to update it:
`remainder -= remainder / int`.
- Subtract the result from `remainder` to update it:
`remainder -= remainder / int`.
</div>

View File

@ -7,7 +7,7 @@ commas and surrounded by square brackets.
For example, the output for `printArgs("Numbers: ", 1, 2, 3)` should be:
```
```text
Numbers: [1, 2, 3]
```

View File

@ -6,7 +6,7 @@ fun interface Pet {
fun speak(): String
}
class PetCreator {
object CreatePet {
fun home() = " home!"
fun dog(): Pet {
val say = "Bark"
@ -32,9 +32,8 @@ class PetCreator {
}
fun main() {
val create = PetCreator()
create.dog().speak() eq "Bark home!"
create.cat().speak() eq "Meow home!"
create.hamster().speak() eq "Squeak home!"
create.goldfish().speak() eq "Blub home!"
CreatePet.dog().speak() eq "Bark home!"
CreatePet.cat().speak() eq "Meow home!"
CreatePet.hamster().speak() eq "Squeak home!"
CreatePet.goldfish().speak() eq "Blub home!"
}

View File

@ -4,7 +4,7 @@ files:
visible: true
placeholders:
- offset: 80
length: 722
length: 706
placeholder_text: // TODO
- name: test/Tests.kt
visible: false

View File

@ -5,7 +5,7 @@ import typechecking.name
interface BeverageContainer {
fun open(): String
fun pour() = "${this.name}: Pour"
fun pour() = "$name: Pour"
fun recycle(): String
}

View File

@ -12,5 +12,5 @@ files:
visible: true
placeholders:
- offset: 116
length: 1239
length: 1232
placeholder_text: // TODO

View File

@ -2,7 +2,7 @@
Create a generic interface called `Items` with a single function `next()` that
returns an object of the generic type, or `null`. Make `Items` usable for [SAM
conversions](https://stepik.org/lesson/350600/step/1).
conversions].
Create a generic function called `itemIter()` that takes a `vararg items` of the
type parameter and returns an `Items` object produced with a SAM conversion. The

View File

@ -1,7 +1,7 @@
## Check Instructions (#3)
This exercise further explores ranges, introduced in [Looping &
Ranges](https://stepik.org/lesson/104311/step/1), and shows how they can be used with check
Ranges], and shows how they can be used with check
instructions.
Create a class `Level` with two constructor arguments: `val range: IntRange`,

View File

@ -5,7 +5,7 @@ and passes it to the base-class constructor. Write a function `fail(msg:
String)` that passes `msg` to `trace()`, and then throws a `Failure(msg)`.
Now write your own versions of `require()` and `check()` (from [Check
Instructions](https://stepik.org/lesson/350665/step/1)) that use `fail()`. The starter code in
Instructions]) that use `fail()`. The starter code in
`main()` tests your functions.
<sub> This task doesn't contain automatic tests,

View File

@ -7,7 +7,7 @@ Write a similar function for `showOr()`.
The sample output for `showAnd(true, true)` should be:
```
```text
true && true == true
```

View File

@ -1,3 +1,4 @@
// Booleans/Foo.kt
package booleansExercise4
fun foo(): Boolean = true

View File

@ -2,11 +2,11 @@
Write three functions using expression body syntax:
- `f()` takes two `Int` parameters and returns the sum.
- `f()` takes two `Int` parameters and returns the sum.
- `g()` takes two `String` parameters and returns them, concatenated.
- `g()` takes two `String` parameters and returns them, concatenated.
- `h()` calls `println()` to display `"h()"`.
- `h()` calls `println()` to display `"h()"`.
In `main()`, assign the result of each function to a `val`, and specify the
type. Then display on the console the types of all the functions `f()`, `g()`

View File

@ -3,7 +3,7 @@
Display "Hello, Kotlin!" on the console, but with "Kotlin" appearing on the
next line. You can display two separate `String`s to achieve this:
```
```text
Hello,
Kotlin!
```

View File

@ -1,7 +1,7 @@
// LoopingAndRanges/Task4.kt
package loopingAndRangesExercise4
fun printHalfPyramid(n: Int) {
fun showHalfTriangle(n: Int) {
for (i in 1..n) {
repeat(i) {
print("#")
@ -11,7 +11,7 @@ fun printHalfPyramid(n: Int) {
}
fun main() {
printHalfPyramid(4)
showHalfTriangle(4)
}
/* Output:
#

View File

@ -1,9 +1,9 @@
## Looping & Ranges (#4)
Display half of a triangle. For an argument of 6 the function
`printHalfPyramid()` should display the following:
`showHalfTriangle()` should display the following:
```
```text
#
##
###

View File

@ -12,7 +12,7 @@ class TestLoopingAndRangesExercise4 {
runAndCheckSystemOutput(
"Incorrect output for n = $n:",
expected) {
printHalfPyramid(n)
showHalfTriangle(n)
}
}

View File

@ -7,7 +7,7 @@ expands into a `for` expression. You type the expression you want to iterate
over, along with the name for the index variable. IntelliJ IDEA automatically
suggests the variable name to iterate over if it's available in the context.
```
```text
iter + tab
for (i in iterable) {

View File

@ -2,7 +2,7 @@
Check which of the following values can't be stored in an `Int` type:
- A million (10<sup>6</sup>)
- A billion (10<sup>9</sup>)
- A trillion (10<sup>12</sup>)
- A quintillion (10<sup>18</sup>)
- A million (10^6^)
- A billion (10^9^)
- A trillion (10^12^)
- A quintillion (10^18^)

View File

@ -4,11 +4,11 @@ IntelliJ IDEA can extract a `val` or `var` automatically. Select the expression
`getFooResult()` inside a string template, then do one of the following:
- Invoke <span class="control">`Find action`</span> (by pressing <span class="shortcut">&shortcut:GotoAction;</span>),
then type `Introduce variable` to find the corresponding action
then type `Extract variable` to find the corresponding action
- Press <span class="shortcut">&shortcut:IntroduceVariable;</span>;
- Choose <span class="control">`Refactor | Extract/Introduce | Variable...`</span>
- Choose <span class="control">`Refactor | Extract | Variable...`</span>
in the application menu.
Type the name of the new variable. IntelliJ IDEA tries to suggest a name

View File

@ -8,7 +8,7 @@ and the `Char` value with single quotes.
Sample output:
```
```text
i: 10
s: "abc"
c: 'a'

View File

@ -4,7 +4,7 @@ Create a function `showSnake(rows: Int, columns: Int)` that displays a table
filled with sequential numbers in a form of snake. For example, `showSnake(3,
3)` should produce the following:
```
```text
0 1 2
5 4 3
6 7 8
@ -19,6 +19,6 @@ It depends on the biggest number, the last one: put exactly one space before it.
Use the string `"%${width}d".format(value)`, where the `width` variable stores
the width of each number cell, to format the value based on the actual input
(`rows` and `columns`).
(`rows` and `columns`).
</div>

View File

@ -3,7 +3,7 @@
Display a triangle. For n = 6 the function `showTriangle()` should produce the
following:
```
```text
#
###
#####

View File

@ -2,12 +2,12 @@
Create a `data` class called `AirlineTicket` containing the following fields:
- `firstName: String`
- `firstName: String`
- `lastName: String`
- `lastName: String`
- `ticket: Int`
- `ticket: Int`
- `origin: String`
- `origin: String`
- `destination: String`
- `destination: String`

View File

@ -6,7 +6,7 @@ data class Book(val title: String, val authors: List<Author>)
data class Author(val name: String)
fun authorBooksMap(books: List<Book>): Map<Author, List<Book>> {
fun createAuthorToBooksMap(books: List<Book>): Map<Author, List<Book>> {
val result = mutableMapOf<Author, MutableList<Book>>()
for (book in books) {
for (author in book.authors) {
@ -34,7 +34,7 @@ fun main() {
Book("Kotlin in Action", listOf(Author("Dmitry Jemerov"), Author("Svetlana Isakova"))),
Book("Atomic Kotlin", listOf(Author("Bruce Eckel"), Author("Svetlana Isakova")))
)
val authorToBooksMap = authorBooksMap(books)
val authorToBooksMap = createAuthorToBooksMap(books)
authorToBooksMap.getValue(Author("Bruce Eckel")).size eq 11
authorToBooksMap.getValue(Author("Svetlana Isakova")).first().title eq "Kotlin in Action"
}

View File

@ -3,7 +3,7 @@ files:
- name: src/Task.kt
visible: true
placeholders:
- offset: 242
- offset: 250
length: 267
placeholder_text: TODO()
- name: test/Tests.kt

View File

@ -1,6 +1,6 @@
## Data Classes (#3)
The starter code contains definitions for the `data` classes `Author` and
`Book`. Write a function called `authorBooksMap()` that takes a `List`
`Book`. Write a function called `createAuthorToBooksMap()` that takes a `List`
of books as its parameter. It builds and returns a `Map` with `Author` as the
key that maps to the `List<Book>` written by that author.

View File

@ -14,7 +14,7 @@ class TestDataClassesExercise3 {
val list = listOf(book)
Assert.assertEquals("Wrong result for $list:",
mapOf(Author("Author") to listOf(book)),
authorBooksMap(list))
createAuthorToBooksMap(list))
}
@Test(timeout = TIMEOUT)
@ -24,7 +24,7 @@ class TestDataClassesExercise3 {
Assert.assertEquals("Wrong result for $list:",
mapOf(Author("first author") to listOf(book),
Author("second author") to listOf(book)),
authorBooksMap(list))
createAuthorToBooksMap(list))
}
@Test(timeout = TIMEOUT)
@ -33,7 +33,7 @@ class TestDataClassesExercise3 {
Book("second Book", listOf(Author("Author"))))
Assert.assertEquals("Wrong result for $list:",
mapOf(Author("Author") to list),
authorBooksMap(list))
createAuthorToBooksMap(list))
}
@Test(timeout = TIMEOUT)
@ -76,7 +76,7 @@ class TestDataClassesExercise3 {
dmitry to listOf(kotlinInAction),
svetlana to listOf(kotlinInAction, atomicKotlin)
)
val actual = authorBooksMap(books)
val actual = createAuthorToBooksMap(books)
Assert.assertEquals("Wrong result for sample:", expected, actual)
}
}

View File

@ -8,7 +8,7 @@ movement.
The location is stored as a pair of coordinates `(x, y)`, where `(0, 0)` is the
top-left corner:
```
```text
(x, y)
(0, 0) (1, 0)
(0, 1) (1, 1)

View File

@ -3,7 +3,7 @@
Implement a `joinComments()` function which takes a `String` parameter
containing comment text. `joinComments()` joins the lines of the comment text,
removing newlines and using a default of `;` as the line separator. It removes
the comment slashes along with any whitespace after the slashes (`// `).
the comment slashes along with any whitespace after the slashes (`//`).
<div class="hint">

View File

@ -2,7 +2,7 @@
Create a class called `Dog` with an overloaded `bark()` member function:
- `bark(n: Int)` displays `woof` repeated `n` times to the console.
- `bark(n: Int)` displays `woof` repeated `n` times to the console.
- `bark(n: Int, say: String)` displays the contents of `say` repeated `n` times
to the console.
- `bark(n: Int, say: String)` displays the contents of `say` repeated `n` times
to the console.

View File

@ -11,7 +11,7 @@ fun balanced(input: String): Boolean {
' ' -> {
}
else -> throw IllegalArgumentException("The string must consist " +
"only of parentheses and whitespaces")
"only of parentheses and whitespace")
}
if (leftUnmatched < 0) return false
}