-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
185 additions
and
74 deletions.
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
object Acronym { | ||
fun generate(phrase: String) : String { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun generate(phrase: String) = phrase.trim() | ||
.split(" ", "-") | ||
.map { str -> str.filter { it.isLetter() } } | ||
.takeIf { it.isNotEmpty() } | ||
?.mapNotNull { it.firstOrNull() } | ||
?.map { it.uppercaseChar() } | ||
?.joinToString("") | ||
?: "" | ||
} |
21 changes: 9 additions & 12 deletions
21
exercises/practice/difference-of-squares/src/main/kotlin/DifferenceOfSquares.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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
class Squares { | ||
//TODO: implement proper constructor | ||
class Squares(val square: Int) { | ||
|
||
fun sumOfSquares() { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun sumOfSquares() = (1..square) | ||
.map { it * it } | ||
.sum() | ||
|
||
fun squareOfSum() { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun squareOfSum() = (1..square) | ||
.sum() | ||
.let { it * it } | ||
|
||
fun difference() { | ||
TODO("Implement the function to complete the task") | ||
} | ||
} | ||
fun difference() = squareOfSum() - sumOfSquares() | ||
} |
29 changes: 16 additions & 13 deletions
29
exercises/practice/dnd-character/src/main/kotlin/DndCharacter.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 |
---|---|---|
@@ -1,22 +1,25 @@ | ||
import java.lang.Math.floorDiv | ||
import kotlin.random.Random | ||
|
||
class DndCharacter { | ||
|
||
val strength: Int = TODO("Initialize value to complete the task") | ||
val dexterity: Int = TODO("Initialize value to complete the task") | ||
val constitution: Int = TODO("Initialize value to complete the task") | ||
val intelligence: Int = TODO("Initialize value to complete the task") | ||
val wisdom: Int = TODO("Initialize value to complete the task") | ||
val charisma: Int = TODO("Initialize value to complete the task") | ||
val hitpoints: Int = TODO("Initialize value to complete the task") | ||
val strength = ability() | ||
val dexterity = ability() | ||
val constitution = ability() | ||
val intelligence = ability() | ||
val wisdom = ability() | ||
val charisma = ability() | ||
val hitpoints = 10 + modifier(constitution) | ||
|
||
companion object { | ||
|
||
fun ability(): Int { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun ability() = (1..4) | ||
.map { Random.nextInt(1, 6) } | ||
.sorted() | ||
.drop(1) | ||
.sum() | ||
|
||
fun modifier(score: Int): Int { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun modifier(score: Int) = floorDiv(score - 10, 2) | ||
} | ||
|
||
} |
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
12 changes: 9 additions & 3 deletions
12
exercises/practice/flatten-array/src/main/kotlin/Flattener.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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
object Flattener { | ||
fun flatten(source: Collection<Any?>): List<Any> { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun flatten(source: Collection<Any?>): List<Any> = source | ||
.filterNotNull() | ||
.flatMap { | ||
if (it is Collection<*>) { | ||
return@flatMap flatten(it) | ||
} else { | ||
listOf(it) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import java.time.LocalDateTime | ||
import java.time.LocalDate | ||
|
||
class Gigasecond { | ||
class Gigasecond(val ldt: LocalDateTime) { | ||
|
||
// TODO: Implement proper constructor | ||
constructor(ld: LocalDate): this(ld.atTime(0, 0)) | ||
|
||
val date: LocalDateTime = TODO("Implement this getter to complete the task") | ||
val date = ldt.plusSeconds(1_000_000_000) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
object Hamming { | ||
|
||
fun compute(leftStrand: String, rightStrand: String): Int { | ||
TODO("Implement this function to complete the task") | ||
if (leftStrand.length != rightStrand.length) throw IllegalArgumentException("left and right strands must be of equal length") | ||
|
||
return leftStrand | ||
.zip(rightStrand) { a, b -> a != b } | ||
.count { it } | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
fun hello(): String { | ||
return "Goodbye, Mars!" | ||
} | ||
fun hello() = "Hello, World!" |
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 |
---|---|---|
@@ -1,6 +1,21 @@ | ||
|
||
object Luhn { | ||
|
||
fun isValid(candidate: String): Boolean { | ||
TODO("Implement this function to complete the task") | ||
val stripped = candidate.replace(" ", "") | ||
if (!stripped.all { it.isDigit() }) return false | ||
if (stripped.length <= 1) return false | ||
|
||
val sumOdd = (stripped.length - 2 downTo 0 step 2) | ||
.map { stripped[it].digitToInt() } | ||
.sumOf { | ||
val sum = it + it | ||
if (sum > 9) sum - 9 | ||
else sum | ||
} | ||
val sumEven = (stripped.length - 1 downTo 0 step 2) | ||
.sumOf { stripped[it].digitToInt() } | ||
|
||
return (sumOdd + sumEven) % 10 == 0 | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
class Matrix(private val matrixAsString: String) { | ||
|
||
fun column(colNr: Int): List<Int> { | ||
TODO("Implement this to complete the task") | ||
} | ||
private val matrix = matrixAsString.split("\n") | ||
.withIndex() | ||
.mapNotNull { indexedRow -> | ||
indexedRow.value.split(" ") | ||
.map { it.toInt() } | ||
.takeIf { it.isNotEmpty() } | ||
?.let { it[0] to it } | ||
} | ||
|
||
fun row(rowNr: Int): List<Int> { | ||
TODO("Implement this to complete the task") | ||
} | ||
fun column(colNr: Int) = matrix.map { it.second } | ||
.mapNotNull { it.getOrNull(colNr - 1) } | ||
|
||
fun row(rowNr: Int) = matrix.getOrNull(rowNr - 1) | ||
?.second | ||
?: emptyList() | ||
} |
23 changes: 17 additions & 6 deletions
23
exercises/practice/nucleotide-count/src/main/kotlin/Dna.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 |
---|---|---|
@@ -1,9 +1,20 @@ | ||
class Dna { | ||
class Dna(dna: String) { | ||
|
||
// TODO: Implement proper constructor | ||
private val map = mutableMapOf( | ||
'A' to 0, | ||
'C' to 0, | ||
'T' to 0, | ||
'G' to 0, | ||
) | ||
|
||
val nucleotideCounts: Map<Char, Int> | ||
get() { | ||
TODO("Implement this function to complete the task") | ||
init { | ||
if (dna.isNotEmpty()) { | ||
require(dna.all { map.keys.contains(it) }) | ||
} | ||
} | ||
dna.forEach { | ||
map[it] = map.getOrDefault(it, 0) + 1 | ||
} | ||
} | ||
|
||
val nucleotideCounts = map | ||
} |
24 changes: 20 additions & 4 deletions
24
exercises/practice/phone-number/src/main/kotlin/PhoneNumber.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 |
---|---|---|
@@ -1,6 +1,22 @@ | ||
class PhoneNumber { | ||
|
||
// TODO: Implement proper constructor | ||
class PhoneNumber(plainPhoneNumber: String) { | ||
|
||
val number: String? = TODO("Implement this getter to complete the task") | ||
} | ||
var number: String | ||
|
||
init { | ||
val nRange = 2..9 | ||
|
||
number = plainPhoneNumber.filter { it.isDigit() } | ||
.takeIf { | ||
it.length == 11 && it[0] == '1' | ||
|| it.length == 10 | ||
} | ||
?.takeLast(10) | ||
?.takeIf { | ||
it[0].digitToInt() in nRange | ||
&& it[3].digitToInt() in nRange | ||
} | ||
?: throw IllegalArgumentException() | ||
} | ||
|
||
} |
10 changes: 9 additions & 1 deletion
10
exercises/practice/rna-transcription/src/main/kotlin/RnaTranscription.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 |
---|---|---|
@@ -1 +1,9 @@ | ||
fun transcribeToRna(dna: String): String = "" | ||
fun transcribeToRna(dna: String) = dna.map { | ||
when (it) { | ||
'A' -> 'U' | ||
'G' -> 'C' | ||
'C' -> 'G' | ||
'T' -> 'A' | ||
else -> it | ||
} | ||
}.joinToString("") |
38 changes: 32 additions & 6 deletions
38
exercises/practice/scrabble-score/src/main/kotlin/ScrabbleScore.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 |
---|---|---|
@@ -1,10 +1,36 @@ | ||
object ScrabbleScore { | ||
|
||
fun scoreLetter(c: Char): Int { | ||
TODO("Implement this function to complete the task") | ||
} | ||
val scores = mapOf ( | ||
'a' to 1, | ||
'e' to 1, | ||
'i' to 1, | ||
'o' to 1, | ||
'u' to 1, | ||
'l' to 1, | ||
'n' to 1, | ||
'r' to 1, | ||
's' to 1, | ||
't' to 1, | ||
'd' to 2, | ||
'g' to 2, | ||
'b' to 3, | ||
'c' to 3, | ||
'm' to 3, | ||
'p' to 3, | ||
'f' to 4, | ||
'h' to 4, | ||
'v' to 4, | ||
'w' to 4, | ||
'y' to 4, | ||
'k' to 5, | ||
'j' to 8, | ||
'x' to 8, | ||
'q' to 10, | ||
'z' to 10, | ||
) | ||
|
||
fun scoreWord(word: String): Int { | ||
TODO("Implement this function to complete the task") | ||
} | ||
|
||
fun scoreLetter(c: Char) = scores.getOrDefault(c.lowercaseChar(), 0) | ||
|
||
fun scoreWord(word: String) = word.sumOf { scoreLetter(it) } | ||
} |
9 changes: 7 additions & 2 deletions
9
exercises/practice/secret-handshake/src/main/kotlin/HandshakeCalculator.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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
object HandshakeCalculator { | ||
|
||
fun calculateHandshake(number: Int): List<Signal> { | ||
TODO("Implement the function to complete the task") | ||
val signals = Signal.values() | ||
.filter { number shr it.ordinal and 1 == 1 } | ||
|
||
return if (number shr 4 and 1 == 1) signals.asReversed() | ||
else signals | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import java.lang.IllegalArgumentException | ||
|
||
class Triangle<out T : Number>(val a: T, val b: T, val c: T) { | ||
|
||
// TODO: Implement proper constructor | ||
init { | ||
require(a > 0 && b > 0 && c > 0) { "Sides must be > 0" } | ||
require(a + b >= c && b + c >= a && c + a >= b) { "Sides must satisfy triangle inequality" } | ||
} | ||
|
||
val isEquilateral: Boolean = a == b && b == c | ||
val isIsosceles: Boolean = a == b || b == c || a == c | ||
val isScalene: Boolean = a != b && b != c && a != c | ||
|
||
val isEquilateral: Boolean = TODO("Implement this getter to complete the task") | ||
val isIsosceles: Boolean = TODO("Implement this getter to complete the task") | ||
val isScalene: Boolean = TODO("Implement this getter to complete the task") | ||
private infix operator fun <T: Number> T.compareTo(other: T): Int = this.toDouble().compareTo(other.toDouble()) | ||
private infix operator fun <T: Number> T.plus(b: T): Double = this.toDouble().plus(b.toDouble()) | ||
} |
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
fun twofer(name: String): String { | ||
TODO("Implement the function to complete the task") | ||
} | ||
fun twofer(name: String? = null) = "One for ${name ?: "you"}, one for me." |