算法实现与应用:纸牌密码与英文数字排序
一、纸牌密码(Solitaire Cipher)
1. 加密与解密差异
加密和解密主要有两个小差异。一是解密时文本已规范化,无需此步骤,但规范化已规范化的文本也无害,所以可忽略此差异。二是加密时字母相加,解密时字母相减,通过一个简单的操作符参数解决。
2. 字母牌组对象
在解决了 Cipher 对象后,需要一个表示牌组的密钥流对象 CipherDeck 。为避免过度抽象增加代码量,将牌视为数字。以下是测试代码:
# solitaire_cipher/tc_cipher_deck.rb
#!/usr/local/bin/ruby -w
require "test/unit"
require "cipher_deck"
class TestCipherDeck < Test::Unit::TestCase
def setup
@deck = CipherDeck.new do |deck|
loop do
deck.move_down("A")
2.times { deck.move_down("B") }
deck.triple_cut
deck.count_cut
letter = deck.count_to_letter
break letter if letter != :skip
end
end
end
def test
超级会员免费看
订阅专栏 解锁全文
14

被折叠的 条评论
为什么被折叠?



