用scala中的char替换char序列?

时间:2015-04-05 17:03:41

标签: string scala dictionary replace char

我正在尝试通过用字符串替换字符来在scala中创建字符串加扰器和解扰器,然后用原始字符替换字符串以对其进行解扰。但是,这是不可能的,因为scala中的map函数需要字符而不是字符串。这是我的代码

object Main{
  def main(args: Array[String]){
    val hello = Scrambler.scramble("How about that bacon?")
    println(hello)
    val bye = Scrambler.deScrambler(hello)
    println(bye)
  }
}

object Scrambler {
  def scramble(s: String) = {
    s map {
      //Since it takes a char and replaces with a string, everything works fine
      case ' ' => "WOOPIE"
      case 'a' => "fIRST"
      case 'b' => "sECOND"
      case 'c' => "tHIRD"
      case 'd' => "fOURTH"
      case 'e' => "fITH"
      case 'f' => "sIXTH"
      case 'g' => "sEVENTH"
      case 'h' => "eIGHTH"
      case 'i' => "nINTH"
      case 'j' => "tENTH"
      case 'k' => "eLEVENTH"
      case 'l' => "tWELFTH"
      case 'm' => "tHIRTEENTH"
      case 'n' => "fOURTEENTH"
      case 'o' => "fIFTEENTH"
      case 'p' => "sIXTEENTH"
      case 'q' => "sEVENTEENTH"
      case 'r' => "eIGHTEENTH"
      case 's' => "nINETEENTH"
      case 't' => "tWENTIETH"
      case 'u' => "tWENTYFIRST"
      case 'v' => "tWENTYSECOND"
      case 'w' => "tWENTYTHIRD"
      case 'x' => "tWENTYFOURTH"
      case 'y' => "tWENTYFIFTH"
      case 'z' => "tWENTYSIXTH"

      case 'A' => "First"
      case 'B' => "Second"
      case 'C' => "Third"
      case 'D' => "Fourth"
      case 'E' => "Fifth"
      case 'F' => "Sixth"
      case 'G' => "Seventh"
      case 'H' => "Eighth"
      case 'I' => "Ninth"
      case 'J' => "Tenth"
      case 'K' => "Eleventh"
      case 'L' => "Twelfth"
      case 'M' => "Thirteenth"
      case 'N' => "Fourteenth"
      case 'O' => "Fifteenth"
      case 'P' => "Sixteenth"
      case 'Q' => "Seventeenth"
      case 'R' => "Eighteenth"
      case 'S' => "Nineteenth"
      case 'T' => "Twentieth"
      case 'U' => "Twentyfirst"
      case 'V' => "Twentysecond"
      case 'W' => "Twentythird"
      case 'X' => "Twentyfourth"
      case 'Y' => "Twentyfifth"
    }
  }.mkString

  def deScrambler(s: String) = {
    s map {
      /*Here, however, it attempts to replace 'WOOPIE' with ' '
      * which is not permitted
      */
      case "WOOPIE" => ' '
      case "fIRST" => 'a'
    }
  }
}

关于我如何做到这一点的任何建议?也许我只是没有正确地搜索我的搜索,但我找不到另类

2 个答案:

答案 0 :(得分:0)

如何在scrambler函数中的每个单词之后添加分隔符,然后根据该分隔符在deScrambler函数中拆分字符串。这是一个例子:

object Main{
  def main(args: Array[String]){
    val hello = Scrambler.scramble("hello there !")
    println(hello)
    val bye = Scrambler.deScrambler(hello)
    println(bye)
  }
}

object Scrambler {
  def scramble(s: String) = {
    s map {
      case ' ' => "WOOPIE"
      case 'e' => "fITH"
      case 'h' => "eIGHTH"
      case 'l' => "tWELFTH"
      case 'o' => "fIFTEENTH"
      case 'r' => "eIGHTEENTH"
      case 't' => "tWENTIETH"
      case _ => "uKnown"
    }
  }.mkString("-") // add a separator between scrambled words

  def deScrambler(s: String) = {
    // split the string based on the separator to get back the
    // the scrambled words
    s.split("-") map {
      case "WOOPIE" => ' '
      case "fITH" => 'e'
      case "eIGHTH" => 'h'
      case "tWELFTH" => 'l'
      case "fIFTEENTH" => 'o'
      case "eIGHTEENTH" => 'r'
      case "tWENTIETH" => 't'
      case _ => "!"
    }
  }.mkString
}

示例输出如下所示:

eIGHTH-fITH-tWELFTH-tWELFTH-fIFTEENTH-WOOPIE-tWENTIETH-eIGHTH-fITH-eIGHTEENTH-fITH-WOOPIE-uKnown
hello there !

答案 1 :(得分:0)

您可以将Decramble实现为:

object Main {
    def main(args: Array[String]) {
      val hello = Scrambler.scramble("How about that bacon?")
      println(hello)
      val bye = Scrambler.deScrambler(hello)
      println(bye)
    }
  }

  object Scrambler {
    val l = List(("WOOPIE", " "), ("fIRST", "a"), ("sECOND", "b") and so on.....)

    def fd(lt: List[(String, String)], str: String): String = {
      if (lt == Nil) str
      else fd(lt.tail, str.replaceAll(lt.head._1, lt.head._2))
    } 

    def deScrambler(s: String) = {
      fd(l, s)
    }

  }