Skip to content

Commit

Permalink
Add Kotlin example
Browse files Browse the repository at this point in the history
  • Loading branch information
izacus committed Feb 6, 2022
1 parent f697e23 commit c432a2a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ make python
make ruby
make rye
make scala
make kotlin
```

## Authors
Expand All @@ -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)

1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ stdenv.mkDerivation {
python3
ruby_3_0
scala
kotlin
];
shellHook = ''
'';
Expand Down
20 changes: 20 additions & 0 deletions src/kotlin/compress.kts
Original file line number Diff line number Diff line change
@@ -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<String>(), { 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()

0 comments on commit c432a2a

Please sign in to comment.