Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 跨棋盘算法的改进_Ruby_Algorithm - Fatal编程技术网

Ruby 跨棋盘算法的改进

Ruby 跨棋盘算法的改进,ruby,algorithm,Ruby,Algorithm,由于David Kahn的书,古老的密码学成为我的爱好之一,我正在尝试在Ruby类中实现,以处理诸如and之类的旧密码。对于这些,一个有用的项目是。我有以下Ruby实现,欢迎任何改进 类键是基类(如果需要,可以是虚拟类)Key#condensed是一种从给定单词中删除重复字母的方法 class SKey < Key attr_reader :full_key attr_reader :alpha, :ralpha def initialize(key) super(k

由于David Kahn的书,古老的密码学成为我的爱好之一,我正在尝试在Ruby类中实现,以处理诸如and之类的旧密码。对于这些,一个有用的项目是。我有以下Ruby实现,欢迎任何改进

类键
是基类(如果需要,可以是虚拟类)
Key#condensed
是一种从给定单词中删除重复字母的方法

class SKey < Key
  attr_reader :full_key
  attr_reader :alpha, :ralpha

  def initialize(key)
    super(key)
    @alpha = Hash.new
    @ralpha = Hash.new
    @full_key = checkboard()
    gen_rings()
  end

  # === checkboard
  #
  # Shuffle the alphabet a bit to avoid sequential allocation of the
  # code numbers
  #
  # Regular rectangle
  # -----------------
  # Key is ARABESQUE condensed into ARBESQU (len = 7) (height = 4)
  # Let word be ARBESQUCDFGHIJKLMNOPTVWXYZ/-
  #
  # First passes will generate
  #
  # A  RBESQUCDFGHIJKLMNOPTVWXYZ/-   c=0  0 x 6
  # AC  RBESQUDFGHIJKLMNOPTVWXYZ/-   c=6  1 x 6
  # ACK  RBESQUDFGHIJLMNOPTVWXYZ/-   c=12 2 x 6
  # ACKV  RBESQUDFGHIJLMNOPTWXYZ/-   c=18 3 x 6
  # ACKVR  BESQUDFGHIJLMNOPTWXYZ/-   c=0  0 x 5
  # ACKVRD  BESQUFGHIJLMNOPTWXYZ/-   c=5  1 x 5
  # ...
  # ACKVRDLWBFMXEGNYSHOZQIP/UJT-
  #
  # Irregular rectangle
  # -------------------
  # Key is SUBWAY condensed info SUBWAY (len = 6) (height = 5)
  #
  # S  UBWAYCDEFGHIJKLMNOPQRTVXZ/-   c=0  0 x 5
  # SC  UBWAYDEFGHIJKLMNOPQRTVXZ/-   c=5  1 x 5
  # SCI  UBWAYDEFGHJKLMNOPQRTVXZ/-   c=10 2 x 5
  # SCIO  UBWAYDEFGHJKLMNPQRTVXZ/-   c=15 3 x 5
  # SCIOX  UBWAYDEFGHJKLMNPQRTVZ/-   c=20 4 x 5
  # SCIOXU  BWAYDEFGHJKLMNPQRTVZ/-   c=0  0 x 4
  # ...
  # SCIOXUDJPZBEKQ/WFLR-AG  YHMNTV   c=1  1 x 1
  # SCIOXUDJPZBEKQ/WFLR-AGM  YHNTV   c=2  2 x 1
  # SCIOXUDJPZBEKQ/WFLR-AGMT  YHNV   c=3  3 x 1
  # SCIOXUDJPZBEKQ/WFLR-AGMTYHNV
  #
  def checkboard
    word = (@key + BASE).condensed.dup
    len = @key.condensed.length
    height = BASE.length / len


    # Odd rectangle
    #
    if (BASE.length % len) != 0
      height = height + 1
    end

    print "\ncheckboard size is #{len} x #{height}\n"
    res = ""
    (len - 1).downto(0) do |i|
      0.upto(height - 1) do |j|
        if word.length <= (height - 1) then
          return res + word
        else
          c = word.slice!(i * j)
          if not c.nil? then
            res = res + c.chr
          end
        end
      end
    end
    return res
  end 

  # == gen_rings
  #
  # Assign a code number for each letter. Each code number is
  # sequentially allocated from two pools, one with 0..7 and
  # the other with 80..99.
  #
  # Allocation is made on the following criterias
  # - if letter is one of ESANTIRU assign a single code number
  # - else assign of of the two letters ones
  #
  # Generate both the encoding and decoding rings.
  #
  # XXX FIXME Use of 80-99 is hardcoded
  #
  def gen_rings
    ind_u = 0
    ind_d = 80

    word = @full_key.dup
    word.scan(/./) do |c|
      if c =~ /[ESANTIRU]/
        @alpha[c] = ind_u
        @ralpha[ind_u] = c
        ind_u = ind_u + 1
      else
        @alpha[c] = ind_d
        @ralpha[ind_d] = c
        ind_d = ind_d + 1
      end
    end
  end # -- gen_rings
class SKey如果word.length也许你应该试试?那里有很多乐于助人的人。

问得好。不幸的是,没有足够的时间深入研究。:D你在寻求什么样的改进?好吧,我所实现的非常简单,也许有一种更优化的方法来生成它。。。(这在当时似乎是一个很好的问题:)只是对你的标签的一个小评论。“旧密码”是一个不常见的名字。也许你应该用“经典密码”或“经典密码”作为标记。啊,好主意。谢谢