Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.Map
- import scala.util.Try
- object Decode {
- val pattern = "(\\d+)(\\w+)".r
- def encoding(num: Int): Map[Char, Char] =
- ('a' to 'z')
- .map(c => (((c - 'a') * num % 26 + 'a').toChar, c))
- .toMap
- def decode(r: String): String = r match {
- case pattern(num, s) =>
- val enc = encoding(num.toInt)
- if (enc.size == 26) s.map(enc)
- else "Impossible to decode"
- case _ => "Invalid pattern"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement