sleepwalker7

Reversing a Process

Apr 26th, 2020
578
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import scala.collection.Map
  2. import scala.util.Try
  3.  
  4. object Decode {
  5.   val pattern = "(\\d+)(\\w+)".r
  6.  
  7.   def encoding(num: Int): Map[Char, Char] =
  8.     ('a' to 'z')
  9.         .map(c => (((c - 'a') * num % 26 + 'a').toChar, c))
  10.         .toMap
  11.  
  12.   def decode(r: String): String = r match {
  13.     case pattern(num, s) =>
  14.       val enc = encoding(num.toInt)
  15.       if (enc.size == 26) s.map(enc)
  16.       else "Impossible to decode"
  17.      
  18.     case _ => "Invalid pattern"
  19.   }
  20. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×