This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Kotlin] #1716. Add implementation for concept exercise:
basic
- Loading branch information
Showing
17 changed files
with
617 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ Cargo.lock | |
.vscode/settings.json | ||
|
||
tmp/ | ||
|
||
# Intellij IDEA project dir | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# What's next? | ||
|
||
## Submitting your solution | ||
|
||
_Hint_: Use `Code --> Reformat` in [IntelliJ IDEA][intellij-idea-ic] to automatically format your code. | ||
|
||
## Entry point | ||
|
||
To run your Kotlin program you need to define so-called entry point: function with name `main` with or without arguments. However, in Exercism course we are using automatic tests that are using other functions defined in exercises. | ||
|
||
Entry point with arguments (you can use them for building CLI applications): | ||
|
||
```kotlin | ||
fun main(args: Array<String>) { | ||
println("Hello, Exercism!") | ||
println("Program args are: $args") | ||
} | ||
``` | ||
|
||
Or ignore arguments completely: | ||
|
||
```kotlin | ||
fun main() { | ||
println("Hello, Exercism!") | ||
} | ||
``` | ||
|
||
## `Unit` return type | ||
|
||
You've seen that some functions (like `main()` above) are not returning value. However, they are implicitly returning value that is called `Unit` (quite similar to `void` in Java/C/C++): | ||
|
||
```kotlin | ||
fun run() {} | ||
|
||
// is the same as | ||
fun run(): Unit { return Unit } | ||
``` | ||
|
||
Returning `Unit` (and using it in function declaration) is completely optional and is omitted in most of the cases. | ||
|
||
[intellij-idea-ic]: https://www.jetbrains.com/idea/download/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## General | ||
|
||
- An [integer (`Int`) value][numbers] can be defined as one or more consecutive digits. | ||
|
||
## 1. Calculate remaining baking time in minutes | ||
|
||
- You need to define a [function][functions] with a single parameter. | ||
- You need to assign a [default value][function-defaults] for the parameter. | ||
- You have to explicitly return an integer value from a method. | ||
- The method's parameter is an [integer][numbers]. | ||
- You need to use the mathematical operator for subtraction (`-`) to subtract values. | ||
|
||
## 2. Calculate the preparation time in minutes | ||
|
||
- You need to define a [function][functions] with a single parameter. | ||
- You have to explicitly return an integer value from a method. | ||
- The method's parameter is an [integer][numbers]. | ||
- You need to use the mathematical operator for addition (`+`) to add values. | ||
|
||
## 3. Calculate the elapsed time in minutes | ||
|
||
- You need to define a [function][functions] with a two parameters. | ||
- You need to assign a [default value][function-defaults] for the second parameter. | ||
- You have to explicitly return an integer value from a method. | ||
- The method's parameter is an [integer][numbers]. | ||
- You need to use the mathematical operator for addition (`+`) to add values. | ||
- You need to call already implemented functions. | ||
|
||
[numbers]: https://kotlinlang.org/docs/reference/basic-types.html#numbers | ||
[functions]: https://kotlinlang.org/docs/reference/functions.html#function-declarations | ||
[function-defaults]: https://kotlinlang.org/docs/reference/functions.html#default-arguments |
47 changes: 47 additions & 0 deletions
47
languages/kotlin/exercises/concept/basics/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Owner of your local pizza place asked you to help her to count cooking time for different pizzas. In this exercise you're going to write few functions to calculate that. | ||
|
||
You have three tasks. | ||
|
||
## 1. Calculate remaining baking time in minutes | ||
|
||
Define and implement `remainingMinutesInOven()` function that will count how many more minutes you need to keep pizza in the oven. | ||
|
||
Function should accept one **optional** parameter (with default value) for the amount of minutes that passed since you've started baking pizza. If parameter is missing - assume that you've just put pizza into the oven and **0 (zero)** minutes passed already. | ||
|
||
For the simplicity - let's assume that all types of pizzas requires exactly 40 minutes in the oven to be ready. | ||
|
||
```kotlin | ||
remainingMinutesInOven() // => 40 | ||
|
||
remainingMinutesInOven(10) // => 30 | ||
``` | ||
|
||
## 2. Calculate the preparation time in minutes | ||
|
||
Before baking pizza you need to prepare dough, roll it out and put toppings onto it. | ||
|
||
Define and implement `preparationTimeInMinutes()` function that will count how many minutes it will take to prepare pizza before putting it into the oven. | ||
|
||
Function should accept one **required** parameter (`numberOfIngredientTypes`) for the number of topping ingredient types that will be used. | ||
|
||
Assume that you need 5 minutes to make and roll out dough and 2 minutes for using **each** kind of topping. | ||
|
||
```kotlin | ||
preparationTimeInMinutes(0) // => 5 | ||
|
||
preparationTimeInMinutes(1) // => 7 | ||
``` | ||
|
||
## 3. Calculate the elapsed time in minutes | ||
|
||
Define and implement `elapsedCookingTimeInMinutes()` function that will count how many minutes it will take to prepare, bake and serve pizza. Use functions from previous steps while making the calculations. | ||
|
||
Function should accept two parameters. First one is **required** and is the number of topping ingredient types. Second one is **optional** and is the serving time in minutes (assume that is 0 if not provided). | ||
|
||
```kotlin | ||
elapsedCookingTimeInMinutes(1) // => 47 | ||
|
||
elapsedCookingTimeInMinutes(2, 3) // => 52 | ||
``` | ||
|
||
_Hint_: Use variables to give nice and readable names for your values. Remember that code readability is much more important than amount of code lines. |
94 changes: 94 additions & 0 deletions
94
languages/kotlin/exercises/concept/basics/.docs/introduction.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
Kotlin is **statically typed** programming language developed by JetBrains. This means that type of variables is defined on compile-time. | ||
|
||
## Variables | ||
|
||
Similarly to other statically-typed programming languages type of each variable should be defined in compile time. Similarly to other **modern** programming languages you can avoid explicit type declarations where they can be interfered by compiler from context. Use this ability to make your code more readable. But remember that there are cases where it's better to specify type explicitly - e.g. in public APIs. | ||
|
||
Kotlin has immutable (`val`) and mutable (`var`) variable. Immutable variable means that once it has it's value assigned - it can't be changed. Most of the time you will use this type of variable. | ||
|
||
```kotlin | ||
val robotName = "HAL-9000" | ||
userId = "T-1000" // This will not compile | ||
``` | ||
|
||
Mutable variable means that it's value can be changed one or more times: | ||
|
||
```kotlin | ||
var index = 12 | ||
print(index) // 12 | ||
|
||
index = 100 | ||
print(index) // 100 | ||
``` | ||
|
||
BTW probably you have already noticed that semicolons in Kotlin are not used (they are optional but not needed except few special cases we will explore later in other exercises). | ||
|
||
## Functions | ||
|
||
Functions in Kotlin are defined with keyword **`fun`** and are [first-class citizens][wiki-fcc] (not related to OOP). It means that you can declare (so-called `top-level functions`) them right in files (e.g. in Java you can define methods only in classes, not in files): | ||
|
||
```kotlin | ||
// This is content of the Hello.kt file | ||
|
||
fun hello() {} | ||
``` | ||
|
||
Functions can receive arguments. Each argument have a name and a type. Probably you remember that compiler is smart enough to understand type of variable In many cases, but you always need to write types for function arguments by yourself as you are declaring certain contract. Functions can have zero or more arguments: | ||
|
||
```kotlin | ||
fun hello() {} | ||
|
||
fun hello(name: String) {} | ||
|
||
fun hello(name: String, age: Int) {} | ||
``` | ||
|
||
Kotlin functions might or might not return a value: | ||
|
||
```kotlin | ||
fun min(a: Int, b: Int): Int | ||
|
||
fun countBonuses(user: User): Bonuses | ||
|
||
fun run() {} | ||
``` | ||
|
||
As you can see, to return value from function you need to use keyword **`return`**: | ||
|
||
```kotlin | ||
fun getName(): String { | ||
return "Alice" | ||
} | ||
``` | ||
|
||
Functions can have parameters with default values. These values will be used if they are omitted where function is invoked: | ||
|
||
```kotlin | ||
fun ping(host: String = "localhost") { | ||
println("PING --> $host") | ||
} | ||
|
||
ping("exercism.io") // PING --> exercism.io | ||
ping() // PING --> localhost | ||
``` | ||
|
||
## Comments | ||
|
||
Use `//` to define single-line comment: | ||
|
||
```kotlin | ||
foo() // Everything after `//` will be ignored by compiler | ||
|
||
// I will be ignored too | ||
``` | ||
|
||
or `/*` and `*/` to define multi-line comments: | ||
|
||
```kotlin | ||
/* | ||
This this an example | ||
for a multiline comment | ||
*/ | ||
``` | ||
|
||
[wiki-fcc]: https://en.wikipedia.org/wiki/First-class_citizen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"authors": [ | ||
{ | ||
"github_username": "dector", | ||
"exercism_username": "dector" | ||
} | ||
], | ||
"forked_from": ["fsharp/basics"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## Goal | ||
|
||
This exercise should teach bare minimum to help students understand Kotlin programs. | ||
|
||
## Learning objectives | ||
|
||
- Know what a variable is. | ||
- Know how to define a variable. | ||
- Know the difference between immutable and mutable variable. | ||
- Know how to update a variable. | ||
- Know how to define a method. | ||
- Know how to return a value from a method. | ||
- Know how to call a method. | ||
- Know about basic types: Int, Unit. | ||
- Know how to define single and multiline comments. | ||
|
||
## Out of scope | ||
|
||
- More advanced data types: String, Float/Double/Number, Nothing etc. | ||
- Calling function with named parameters. | ||
- Inline functions. | ||
- Classes/Objects. | ||
- Packages | ||
|
||
## Concepts | ||
|
||
The Concepts this exercise unlocks are: | ||
|
||
- `basics`: know what a variable is; know how to define a variable; know the difference between immutable and mutable variable; know how to update a variable; know how to define a method; know how to return a value from a method; know how to call a method; know that methods must be defined in classes; know about basic types: Int, Unit; know how to define single and multiline comments. | ||
|
||
## Prequisites | ||
|
||
_none_ | ||
|
||
## Resources to refer to | ||
|
||
[Kotlin docs](https://kotlinlang.org/docs/reference/basic-syntax.html) | ||
|
||
## Representer | ||
|
||
## Analyzer |
15 changes: 15 additions & 0 deletions
15
languages/kotlin/exercises/concept/basics/.meta/src/main/kotlin/Example.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fun remainingMinutesInOven(actualMinutesInOven: Int = 0): Int { | ||
val cookingTime = 40 | ||
|
||
return cookingTime - actualMinutesInOven | ||
} | ||
|
||
fun preparationTimeInMinutes(numberOfIngredientTypes: Int): Int = | ||
5 + numberOfIngredientTypes * 2 | ||
|
||
fun elapsedCookingTimeInMinutes(numberOfIngredientTypes: Int, servingTimeInMinutes: Int = 0): Int { | ||
val preparationTime = preparationTimeInMinutes(numberOfIngredientTypes) | ||
val backingTime = remainingMinutesInOven() | ||
|
||
return preparationTime + backingTime + servingTimeInMinutes | ||
} |
23 changes: 23 additions & 0 deletions
23
languages/kotlin/exercises/concept/basics/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat | ||
|
||
plugins { | ||
kotlin("jvm") version "1.3.60" | ||
} | ||
|
||
repositories { | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
compile(kotlin("stdlib")) | ||
|
||
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2") | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
testLogging { | ||
exceptionFormat = TestExceptionFormat.FULL | ||
events("passed", "failed", "skipped") | ||
} | ||
} |
Binary file added
BIN
+54.3 KB
languages/kotlin/exercises/concept/basics/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
languages/kotlin/exercises/concept/basics/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.