Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.13 KB | None | 0 0
  1. import java.io.File
  2. import java.lang.Exception
  3. import java.lang.StringBuilder
  4. import java.util.*
  5.  
  6. fun loadDict(): Set<String> {
  7.     val dict = mutableSetOf<String>()
  8.     Scanner(File("/home/alexander/russian-dictionary.txt").inputStream()).use {
  9.         while (it.hasNextLine())
  10.             dict.add(it.nextLine())
  11.     }
  12.     return dict
  13. }
  14.  
  15. fun String.shift(shift: Int, alphabet: List<Char>) = StringBuilder().
  16.     apply {
  17.         for (c in this@shift)
  18.             append(alphabet[(alphabet.indexOf(c) + shift) % alphabet.size])
  19.     }.toString()
  20.  
  21. fun task111() {
  22.     var decodedSuccessfully = 0
  23.     var total = 0
  24.     val dict = loadDict()
  25.     for (w in dict) {
  26.         val word = w.toLowerCase()
  27.         if (!word.all { alphabet.contains(it) }) continue
  28.         total++
  29.         var cnt = 0
  30.         for (shift in 0 until alphabet.size)
  31.             if (word.shift(shift, alphabet) in dict)
  32.                 cnt++
  33.  
  34.         if (cnt == 1)
  35.             decodedSuccessfully++
  36.     }
  37.     println("$total")
  38.     println("Decoded successfully ${(decodedSuccessfully).toDouble() / (total) * 100.0}%")
  39. }
  40.  
  41. fun main() {
  42.    task111()
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement