Skip to content

Commit

Permalink
Regenerated samples & exercise descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Oct 23, 2020
1 parent 52a11e8 commit dad77e0
Showing 80 changed files with 156 additions and 201 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// BuildingMaps/ImmutableBlendMap.kt
// BuildingMaps/ReadOnlyBlendMap.kt
@file:OptIn(ExperimentalStdlibApi::class)
package buildingmaps

Original file line number Diff line number Diff line change
@@ -24,5 +24,5 @@ files:
visible: true
- name: src/ColorBlend.kt
visible: true
- name: src/ImmutableBlendMap.kt
- name: src/ReadOnlyBlendMap.kt
visible: true
2 changes: 1 addition & 1 deletion Functional Programming/Folding Lists/Exercise 4/task.md
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@
The starter code provides a `Condition` class and a function
`Condition.combine()` that combines two conditions. There's also a skeleton
for the `List<Condition>` extension function `combineAll()` that combines all
conditions in the `List`. Complete the implementation using `reduce()`,
the conditions in the `List`. Complete the implementation using `reduce()`,
assuming the `List` is non-empty.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Manipulating Lists (#3)

Reimplement the `authorBooksMap()` function from the Data Classes atom,
Reimplement the `authorBooksMap()` function from [Data Classes],
using operations for manipulating collections. `authorBooksMap()` takes a
`List<Book>` as a parameter and builds a `Map` from each `Author` to the
`Book`s they have written.
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ Bob - Charlie
`friendSuggestions()` should return Charlie for Alice, because Charlie is a
friend of Alice's friend Bob and isn't yet a friend of Alice.

The following example produces no friend suggestions for Alice because Bob and
The following example produces no friend suggestions for Alice, because Bob and
Charlie are already her friends:

```text
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Operations on Collections (#4)

`all()`, `none()` and `any()` can be used to produce identical results.
Implement `List<Int>` extension functions `allNonZero()` and `hasZero()` using
each of `all()`, `none()` and `any()`.
Implement the `List<Int>` extension functions `allNonZero()` and `hasZero()`
using each of `all()`, `none()` and `any()`.

- `allNonZero()` checks that all elements in the list are non-zero.

4 changes: 2 additions & 2 deletions Functional Programming/Recursion/Exercise 1/task.md
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@

Write a tail recursive function called `simulation()` that takes a `String`
called `group` and an `Int` called `level`. It displays `"Simulation: $group
Reality: level"`, then recursively calls itself with `level - 1` as long as
`level` is greater than zero.
Reality: level"`, then calls itself with `level - 1` as long as `level` is
greater than zero.
8 changes: 4 additions & 4 deletions Functional Programming/Recursion/Exercise 4/task.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## Recursion (#4)

The starter code defines a `City` class. Implement an extension function
The starter code provides a class `City`. Implement an extension function
`City.allReachable()` that builds a set of all cities reachable from the
current `City`. Implement it in two ways: recursive and iterative.

The direct connections for each `City` are stored in its `connections`
property. `allReachable()` should return all cities reachable from the given
city via other cities. The city is reachable from itself, so it should be also
present in the resulting set.
property. `allReachable()` should return all the cities reachable from the
given city via other cities. The city is reachable from itself, so it should be
also present in the resulting set.

For example, consider the following connections graph:

2 changes: 1 addition & 1 deletion Functional Programming/Sequences/Exercise 3/task.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@

Implement the `School` extension function `averageInstructorRating()` that
takes `Instructor` as a parameter and calculates the average rating that the
instructor was given by all students that attended his or her classes. If a
instructor was given by all the students that attended his or her classes. If a
student attended several lessons by that instructor, the ratings for individual
lessons should be treated separately.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## The Importance of Lambdas (#3)

Implement the function `other(s: String)` so it returns a `String` containing
Implement the function `other(s: String)` that returns a `String` containing
every other letter of `s`. For example, for an input of "cement" it returns
"cmn".

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Constraining Visibility (#2)

Continue developing the `Robot` class from the exercises in the previous atoms.
Use `private` on all the properties and the `crossBoundary()` function, and
verify that you can't access the `private` members outside the class.
Use `private` on all the properties and `crossBoundary()`, and verify that you
can't access the private members outside of the class.
2 changes: 1 addition & 1 deletion Introduction to Objects/Constructors/Exercise 2/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Constructors (#2)

Continue developing the `Robot` class from the exercises in the previous atom.
Convert the properties that store the size of the field and the current
Convert the properties storing the size of the field and the current
coordinates into `Robot` constructor parameters.
4 changes: 2 additions & 2 deletions Introduction to Objects/Creating Classes/Exercise 1/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Creating Classes (#1)

Create a class named `SomeClass` with three member functions: `a()` which
displays `42` on the console, `b()` which calls `a()`, and `c()` which calls
`b()` by qualifying it.
displays `42` on the console when you call it, `b()` which calls `a()`,
and `c()` which calls `b()` by qualifying it.
9 changes: 5 additions & 4 deletions Introduction to Objects/Creating Classes/Exercise 3/task.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## Creating Classes (#3)

Create a `Robot` class with the following four member functions: `right(steps:
Int)`, `left(steps: Int)`, `down(steps: Int)` and `up(steps: Int)`. Each
function should display one of the following phrases on the console:
Create a `Robot` class with the following four member functions:
`right(steps: Int)`, `left(steps: Int)`, `down(steps: Int)` and
`up(steps: Int)`. Each function should display one of the following
phrases on the console:

```
Right N steps
@@ -11,4 +12,4 @@ Down N steps
Up N steps
```

N is the provided number of steps.
where N is the provided number of steps.
2 changes: 1 addition & 1 deletion Introduction to Objects/Exceptions/Exercise 1/task.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Exceptions (#1)

Display on the console all the following `String`s that can't be converted to
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):

```
4 changes: 2 additions & 2 deletions Introduction to Objects/Lists/Examples/src/OutOfBounds.kt
Original file line number Diff line number Diff line change
@@ -5,6 +5,6 @@ fun main() {
val ints = listOf(1, 2, 3)
capture {
ints[3]
} eq "ArrayIndexOutOfBoundsException: " +
"Index 3 out of bounds for length 3"
} contains
listOf("ArrayIndexOutOfBoundsException")
}
10 changes: 5 additions & 5 deletions Introduction to Objects/Lists/Exercise 3/task.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## Lists (#3)

Write a function that determines whether two `String`s are anagrams. An anagram
Write a function to determine whether two `String`s are anagrams. An anagram
is a word formed by rearranging the letters of a different word, using all the
original letters exactly once.

<div class="hint">

Compare two sorted `Lists` of characters obtained from two `String`s. Convert a
`String` to a `List` by calling `toList()`. If the `Lists` are equal, the words
are anagrams. For example, for the two anagrams "terrain" and "trainer", the
sorted character `List` will be `[a, e, i, n, r, r, t]`.
Compare two sorted `Lists` of characters obtained from two `String`s.
Convert a `String` to a `List` by calling `toList()`. If the `Lists` are equal,
the words are anagrams. For example, for two anagrams "terrain" and "trainer"
the sorted character `List` will be `[a, e, i, n, r, r, t]`.

</div>
2 changes: 1 addition & 1 deletion Introduction to Objects/Maps/Exercise 3/task.md
Original file line number Diff line number Diff line change
@@ -6,5 +6,5 @@ constant time. With a `List`, in the worst case you must iterate over every
element.

Change the internal implementation of the `Cage` class to store elements in a
`Map` rather than a `List`. To get an element, use the `getValue()` member
`Map` rather than a `List`. To get an element use the `getValue()` member
function, which throws `NoSuchElementException` if the key is missing.
2 changes: 1 addition & 1 deletion Introduction to Objects/Properties/Exercise 1/task.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Properties (#1)

Create a class `X` containing three `Int` properties: `a` and `b` are `val`s
Create a class `X` that contains three `Int` properties: `a` and `b` are `val`s
and `c` is a `var`. Initialize `a` to 3, `b` to 42, and `c` to zero. Create an
`add()` member function that sums `a` and `b` and assigns the result to `c`,
then returns `c`. Write a `main()` to test `X`.
3 changes: 1 addition & 2 deletions Introduction to Objects/Properties/Exercise 2/task.md
Original file line number Diff line number Diff line change
@@ -11,8 +11,7 @@ is the top-left corner:
```

Moving right increases the `x` coordinate, moving down increases the `y`
coordinate, while moving left and up decrease the `x` and `y` coordinates,
respectively.
coordinate, while moving left and up decrease the `x` and `y` coordinates.

Implement `Robot`'s member functions `right()`, `left()`, `up()` and `down()`,
each of which takes a `steps` parameter. Also implement `getLocation()` which
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@ Create a class `MessageStorage` with two properties: a `private` one named
`_messages` of type `MutableList<String>` and a `public` one named `messages`
of type `List<String>`. The custom getter for `messages` returns `_messages`.

Since `_messages` is `private`, its contents can be only changed within the
Because `_messages` is `private` its contents can be only changed within the
`MessageStorage` class. Define an `addMessage()` member function that takes a
`String` parameter and adds it to the `_messages` list.
4 changes: 2 additions & 2 deletions Introduction to Objects/Summary 2/Exercise 2/task.md
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@

Create a class named `Boring2` that is just like `Boring` except it has
constructor parameters, which are all `val`s. The parameter `a` holds the value
that `a()` produces, `b` holds the value that `b()` produces, and `c` holds the
value produced by `c()`. Test `Boring2` using `atomictest`.
that `a()` produces, the parameter `b` holds the value that `b()` produces, and
`c` holds the value produced by `c()`. Test `Boring2` using `atomictest`.
26 changes: 7 additions & 19 deletions Introduction to Objects/Summary 2/Exercise 7/task.md
Original file line number Diff line number Diff line change
@@ -2,30 +2,18 @@

Convert a natural number into a number in the Roman numeral system.

| Roman | Decimal |
|-------|---------|
| I | 1 |
| IV | 4 |
| V | 5 |
| IX | 9 |
| X | 10 |
| XL | 40 |
| L | 50 |
| XC | 90 |
| C | 100 |
| CD | 400 |
| D | 500 |
| CM | 900 |
| M | 1000 |
Roman numerals:
1000 = M, 900 = CM, 500 = D, 400 = CD, 100 = C, 90 = XC,
50 = L, 40 = XL, 10 = X, 9 = IX, 5 = V, 4 = IV, 1 = I.

For example: 23 = XXIII, 44 = XLIV, 100 = C.

<div class="hint">

Perform the conversion in steps. Use an auxiliary `remainder` variable to store
the remaining part of the converted integer and a `result` variable to store
the resulting Roman numeral representation. For each step, the initial `number`
equals the sum of `remainder` and `result`.
Perform the conversion in steps. Use an auxiliary `remainder`
variable to store the remaining part of the converted integer and the `result`
variable to store the resulting Roman numeral representation. For each step,
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`:
21 changes: 11 additions & 10 deletions Introduction to Objects/Summary 2/Exercise 8/task.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
## Summary 2 (#8)

Convert from a Roman number into a natural number. For
example: XXIII is 23, XLIV is 44, and C is 100.
example: XXIII = 23, XLIV = 44, C = 100.

<div class="hint">

Iterate over each digit in the Roman number and calculate the answer. Traverse
a Roman number in reverse order, a single digit at a time (for example, `IV`
contains two digits) and store the maximum value found so far. If the next
Roman digit is greater than or equal to the current maximum value, add it to
the result. If it's less than the maximum, subtract it instead. For example, to
convert XLIV, iterate over `VILX` which is the reverse of `XLIV`. Add `V`(`5`)
and `L`(`50`), but subtract `1`(`I`) because it's less than the current maximum
`V`, and subtract `10`(`X`) because it's less than the updated maximum `X`:
Simply iterate over each digit in the Roman number and calculate the
answer. Traverse a Roman number in reverse order, a single digit at a time (for
example, `IV` contains two digits) and store the maximum value found so far. If
the next Roman digit is greater than or equal to the current maximum value, add
it to the result. If it's less than the maximum, subtract it instead. For
example, to convert `XLIV = 44`, iterate over `VILX` which is the reverse of
`XLIV`. You add `V`(`5`) and `L`(`50`), but subtract `1`(`I`) because it's less
than the current maximum `V`, and subtract `10`(`X`) because it's less than the
updated maximum `X`:

| Numeral | Current Maximum | Action |
| numeral | current maximum | action |
| ------- |-----------------|--------|
| V | 5 | + 5 |
| I | 5 | - 1 |
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

Write a function `printArgs()` with a `String` as the first parameter, and a
`vararg` parameter of `Int` as the second parameter. `printArgs()` displays its
arguments on the console: first the `String`, then the `Int`s, separated by
arguments to the console: first the `String`, then the `Int`s, separated by
commas and surrounded by square brackets.

For example, the output for `printArgs("Numbers: ", 1, 2, 3)` should be:
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Class Delegation (#2)

Exercise 1 from the Inheritance & Extensions atom uses
Exercise 1 in [Inheritance & Extensions] uses
composition to adapt `Crocodile` to work with `interactWithDuck()`. This
produces an inconsistency when using `IAmHonestlyDuck` with the
`interactWithCrocodile()` function---the composed `crocodile` must be
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Complex Constructors (#1)

Modify `Alien` to use "verbose" syntax: make `name` a constructor parameter
Modify `Alien` to use the "verbose" syntax: make `name` a constructor parameter
rather than a property, add the `val` property `myName` and assign `name` value
to the property `myName` inside the `init` section.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Complex Constructors (#3)

Show that multiple `init` sections are executed in declaration order. The starter
Show that multiple init sections are executed in declaration order. The starter
code contains `MultipleInit` class with a
`val initOrder = mutableListOf<String>()` property. Add the `String`s `"one"`,
`"two"` and `"three"` to the `initOrder` property in three different `init`
2 changes: 1 addition & 1 deletion Object-Oriented Programming/Composition/Exercise 1/task.md
Original file line number Diff line number Diff line change
@@ -2,4 +2,4 @@

The starter code contains the classes `Shape`, `Circle` and `Rectangle`.
`Circle` and `Rectangle` use composition and store an instance of `Shape`.
Modify them to use inheritance instead of composition.
Modify them to use inheritance instead.
8 changes: 4 additions & 4 deletions Object-Oriented Programming/Composition/Exercise 2/task.md
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@

The starter code contains implementations of `Stack` and `Queue` classes.

`Stack` provides last-in-first-out access to elements. You can add ("push")
new elements, and get ("pop") the last element that was added.
`Stack` provides a last-in-first-out access to elements. You can add ("push")
new elements to it, and get ("pop") the last one that was added.

`Queue` provides first-in-first-out access to elements. You can add new
elements to it, and get ("poll") returns the first element that was added.
`Queue` provides a first-in-first-out access to elements. You can add new
elements to it, and get ("poll") returns you the first one that was added.

In the starter code, both `Stack` and `Queue` extend `ArrayList`, which opens
too many methods in the public API (for example, you can get the first element
2 changes: 1 addition & 1 deletion Object-Oriented Programming/Composition/Exercise 3/task.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ Based on your solution for the previous exercise, modify the implementation of
`MutableList`. `ArrayDeque` represents a "double ended queue", so it provides
member functions to add last and remove first elements.

Note that composition allows you to change the internal implementation of the
Note how with composition you can change the internal implementation of the
class without changing the code that uses that class.

`ArrayDeque` is currently experimental in the Kotlin library, so we must add
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Inheritance & Extensions (#1)

The starter code contains `Duck` and `interactWithDuck()` declarations (we
assume they're part of a third-party library). Implement the `mimicDuck()`
assume they're part of the third-party library). Implement the `mimicDuck()`
function that dynamically adapts an object, accepting a `Crocodile` and
returning an `IAmHonestlyDuck`. `IAmHonestlyDuck` should implement `Duck` and
delegate both `Duck` member functions to `crocodile.bite()`. Is it possible to
2 changes: 1 addition & 1 deletion Object-Oriented Programming/Inheritance/Exercise 1/task.md
Original file line number Diff line number Diff line change
@@ -4,4 +4,4 @@ The starter code contains an `open` class `Cleanser` and a class `Detergent`
that inherits it. Add to the `Cleanser` class the property
`var ops: MutableList<String>` and the functions `dilute()`, `apply()` and
`scrub()` that simply add their names to `ops`. In `main`, make sure that
`Detergent` now has the same functions as `Cleanser`.
`Detergent` has now the same functions as `Cleanser`.
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ Define a standalone function `<T> traceAll(select: Selector<T>)` that uses
`select` to append all the values of `current()` to `trace` using `+=`, then
returns `trace`.

Inherit `Container` from `Iterable<T>`, and add a function called
Now make `Container` inherit from `Iterable<T>`, and add a function called
`iterator()` that returns an instance of an anonymous inner class that inherits
from `Iterator<T>`. Add a standalone function `<T> traceAll2(ib: Iterable<T>)`
that produces the same behavior as `traceAll()`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Sealed Classes (#3)

Modify `SealedSubclasses.kt` so that all subclasses of `Top` are nested
Modify `SealedSubclasses.kt` so that all the subclasses of `Top` are nested
within `Top`. Create a seeded random-number generator by defining `val rand =
Random(17)`. Use this generator to randomly select a subclass of `Top` and
display its `simpleName`.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Secondary Constructors (#3)

Replace all constructors in the `GardenItem` class with a single primary
Replace all the constructors in the `GardenItem` class with a single primary
constructor using default arguments.
4 changes: 2 additions & 2 deletions Object-Oriented Programming/Type Checking/Exercise 3/task.md
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ Modify `Insects.kt` so that `Insect` is a `sealed` class (this may require
modifications to other components). Change `basic()` to use a `when`
expression.

What does this gain you, since the `else` clauses in the `when` expressions
still make sense?
The `else` clauses in the `when` expressions still make sense, so how does this
benefit you?

<sub> This task doesn't contain automatic tests,
so it's always marked as "Correct" when you run "Check".
Loading

0 comments on commit dad77e0

Please # to comment.