Advertisement
sleepwalker7

Reversing a Process

Apr 26th, 2020
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.47 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement