2015-04-05 45 views
0

我想通過用一個字符串替換字符,然後用原始字符替換該字符串以解密它,來在scala中創建一個字符串擾頻器和解擾器。但是,這是不可能的,因爲scala中的map函數需要字符而不是字符串。這裏是我的代碼用scala中的char替換char序列?

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' 
    } 
    } 
} 

有關我如何做到這一點的任何建議?也許我只是沒有正確說出我的搜索,但我找不到替代方法

+0

你爲什麼不以'解擾器使用'replace' 「無論如何?作爲一個字符串是一個字符序列,邏輯上''地圖'採取一個字符作爲輸入,而不是一個字符串。 – ale64bit 2015-04-05 17:55:15

+0

「由於它需要一個字符並用一個字符串替換,所以一切工作正常」。事實並非如此。該地圖產生一個List [String]。但是mkString把它變成一個單一的字符串。在你可以調用解擾器之前(或者在你調用地圖之前),你必須將字符串分割成單獨的單詞 – 2015-04-05 17:55:20

+0

如果你在製作字符串時使用任何分隔符會更好,這很容易解密你的字符串,不會產生任何不需要的行爲。 – curious 2015-04-05 18:10:02

回答

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 ! 
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) 
    } 

    }