From c432a2afeb86f041fb44cebfcd83da6d22c2e5d0 Mon Sep 17 00:00:00 2001 From: Jernej Virag Date: Sun, 6 Feb 2022 10:43:59 +0100 Subject: [PATCH] Add Kotlin example --- Makefile | 4 ++++ README.md | 2 ++ shell.nix | 1 + src/kotlin/compress.kts | 20 ++++++++++++++++++++ 4 files changed, 27 insertions(+) create mode 100644 src/kotlin/compress.kts diff --git a/Makefile b/Makefile index 0ab430a..8bb4dc2 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,10 @@ scala: CompressionPuzzle01.class CompressionPuzzle02.class scala CompressionPuzzle01 scala CompressionPuzzle02 +# Kotlin +kotlin: + kotlinc -script src/kotlin/compress.kts + rye: rye-build ./rye-src/rye src/rye/compress_jm_rec.rye ./rye-src/rye src/rye/compress_jm_rec_steps.rye diff --git a/README.md b/README.md index a618014..1b1ae44 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ make python make ruby make rye make scala +make kotlin ``` ## Authors @@ -67,4 +68,5 @@ make scala - [Simon Belak](https://github.com/sbelak) - [Tit Petrič](https://github.com/titpetric) - [Urban Škudnik](https://github.com/uskudnik) +- [Jernej Virag](https://github.com/izacus) diff --git a/shell.nix b/shell.nix index 1f51773..4747c92 100644 --- a/shell.nix +++ b/shell.nix @@ -12,6 +12,7 @@ stdenv.mkDerivation { python3 ruby_3_0 scala + kotlin ]; shellHook = '' ''; diff --git a/src/kotlin/compress.kts b/src/kotlin/compress.kts new file mode 100644 index 0000000..c16057a --- /dev/null +++ b/src/kotlin/compress.kts @@ -0,0 +1,20 @@ +fun compress(input: String): String { + // [A, A, A, B, B, A, A, C] + return input.chunked(1) + // ["AAA", "BB", "AA", "C"] + .fold(mutableListOf(), { sequences, ch -> + sequences.add(if (!sequences.isEmpty() + && sequences.last().contains(ch)) + sequences.removeLast().plus(ch) + else + ch) + sequences + }) + // ["3A", "2B", "2A", "1C"] + .map({ it.length.toString() + it.first() }) + // "3A2B2A1C" + .joinToString("") +} + +if (compress("AAABBAAC") != "3A2B2A1C") + throw AssertionError() \ No newline at end of file