Notes on using Kotlin for competitive programming

Правка en4, от Spheniscine, 2019-11-02 05:17:49

I pretty much exclusively use Kotlin for competitive programming, mostly because it's the language I'm currently most comfortable with. Here are some scattered notes and tidbits about my experience which I think might be useful to others; if you have any tips/suggestions, feel free to let me know.

Useful features

  • A lot less boilerplate than Java. Type inference means a lot less "Pokémon speak". Variables and functions can be declared straight in the top-level of the file. (basically the equivalent of static functions)

  • data classes – basically custom tuples. Allows convenient destructuring declarations too.

  • Has access to the data structures in the Java standard library (TreeMap, HashMap, PriorityQueue etc.), and also can use BigInteger and BigDecimal if needed

  • Functional idioms for collection manipulation; map, fold, filter, etc.

  • inline classes – allows the creation of a new type that wraps over a base type, but that is represented by an underlying type at runtime. Especially useful for "modulo $$$10^9 + 7$$$" problems, as I keep code for a ModInt class that overloads the arithmetic operators appropriately, but is represented as a plain int in JVM runtime. Keep in mind that they are experimental as of Kotlin 1.3, but that's fine for CP in my opinion

  • unsigned integer types in the standard library that use the inline class feature.

  • run function – great way to write code that needs to shortcut (e.g. return@run "NO") without having to write a new function and pass every relevant argument

  • functions in functions – functions can be defined within e.g. the main function, so again, no having to pass lots of arguments or global variables

Potential pitfalls

  • Generic wrappers for JVM primitive types can cause TLE for some problems. Use primitive arrays (IntArray etc.) whenever possible, but see next point

  • Inherits the hack-vulnerable quicksort from Java for primitive arrays. Easiest solution is to use generic arrays or lists instead, but due to the performance benefit of primitive arrays, I've took the trouble to write code that shuffles them randomly before sorting

Теги kotlin

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en17 Английский Spheniscine 2019-11-04 06:34:04 483 Tiny change: 'rapper: \n```\ncla' -> 'rapper: \n\n```\ncla'
en16 Английский Spheniscine 2019-11-04 05:59:47 3 Added new pitfall due to buggy asJavaRandom() function in STL (saved to drafts) (published)
en15 Английский Spheniscine 2019-11-04 05:59:09 949 Added new pitfall due to buggy asJavaRandom() function in STL (saved to drafts)
en14 Английский Spheniscine 2019-11-02 09:06:17 11 Tiny change: 'ke string declarations, e.g. `"' -> 'ke string templates, e.g. `"'
en13 Английский Spheniscine 2019-11-02 07:56:59 6 Tiny change: '\n- `run` function ' -> '\n- `run` block function '
en12 Английский Spheniscine 2019-11-02 07:03:11 1 Tiny change: 'suggested here is on' -> 'suggested there is on'
en11 Английский Spheniscine 2019-11-02 06:59:28 54
en10 Английский Spheniscine 2019-11-02 06:52:38 114
en9 Английский Spheniscine 2019-11-02 06:44:36 11 Tiny change: ' the hack-vulnerable quicksor' -> ' the hack-prone quicksor'
en8 Английский Spheniscine 2019-11-02 06:14:52 41
en7 Английский Spheniscine 2019-11-02 06:01:00 278 Tiny change: 're sorting\n' -> 're sorting.\n' (published)
en6 Английский Spheniscine 2019-11-02 05:50:09 1566 Tiny change: 'the `main`() function ' -> 'the `main` function '
en5 Английский Spheniscine 2019-11-02 05:34:38 911
en4 Английский Spheniscine 2019-11-02 05:17:49 455
en3 Английский Spheniscine 2019-11-02 05:12:20 203
en2 Английский Spheniscine 2019-11-02 05:08:48 584 Tiny change: 'shortcut (`return@ru' -> 'shortcut (e.g. `return@ru'
en1 Английский Spheniscine 2019-11-02 05:01:55 1106 Initial revision (saved to drafts)