当前位置:  开发笔记 > 编程语言 > 正文

如果这段代码不是一个笑话,它究竟是如何运作的?

如何解决《如果这段代码不是一个笑话,它究竟是如何运作的?》经验,为你挑选了4个好方法。

编辑:发布原始混淆示例的人在他的答案中给出了实际的源代码.他还发布了混淆代码的更正版本,因为正如我所说,即使你删除了时髦的语法,其中一些也没有意义.

这是一些很好的混淆代码.与大多数混淆代码一样,它主要是许多三元运算符,并且顽固地拒绝将正常人放入空格.这里写的基本上是相同的东西:

class Tree
  def initialize(*d)
    @d,  = d # the comma is for multiple return values,
             # but since there's nothing after it,
             # all but the first are discarded.
  end
  def to_s
    @l || @r ? ",>" : @d
  end
  def total
    total = @d.is_a?(Numeric) ? @d : 0
    total += @l.total if @l
    total += @r.total if @r
  end
  def insert(arg)
    if @d
      if @l
        @l.insert(arg)
      else
        @l = Tree.new(arg)
      end
    else
      @d = arg
    end
  end
end

insert方法在语法上没有效果(它在一个部分缺少一个方法名称),但就我所知,这基本上就是它所做的.该方法的混淆非常厚:

    而不是仅仅做@l = whatever,它使用instance_variable_get()instance_variable_set().更糟糕的是,它instance_variable_get()只是个别名g().

    它将大部分功能包含在lambda函数中,并传递给它的名称@l.然后它用不太知名的语法调用这个函数,func[arg1, arg2]相当于func.call(arg1, arg2).


dustyburwell.. 9

这似乎只是几行中的二叉树实现.如果我对ruby语法的理解有限,我深表歉意:

class Tree                    // defining the class Tree

    def initialize *d;        // defines the initializer
        @d = d;               // sets the node value
    end

    def to_s;                 // defines the to_s(tring) function
        @l || @r ? ",>" : @d; // conditional operator. Can't tell exactly what this 
                              // function is intending. Would think it should make a
                              // recursive call or two if it's trying to do to_string
    end

    def total;                // defines the total (summation of all nodes) function
        @d.is_a ? (Numeric)   // conditional operator.  Returns
            ? @d              // @d if the data is numeric
            : 0               // or zero
        + (@l ? @l.total : 0) // plus the total for the left branch
        + (@r ? @r.total : 0) // plus the total for the right branch
    end

    def insert d              // defines an insert function
        ??                    // but I'm not going to try to parse it...yuck
    end

希望有所帮助......:/



1> Chuck..:

编辑:发布原始混淆示例的人在他的答案中给出了实际的源代码.他还发布了混淆代码的更正版本,因为正如我所说,即使你删除了时髦的语法,其中一些也没有意义.

这是一些很好的混淆代码.与大多数混淆代码一样,它主要是许多三元运算符,并且顽固地拒绝将正常人放入空格.这里写的基本上是相同的东西:

class Tree
  def initialize(*d)
    @d,  = d # the comma is for multiple return values,
             # but since there's nothing after it,
             # all but the first are discarded.
  end
  def to_s
    @l || @r ? ",>" : @d
  end
  def total
    total = @d.is_a?(Numeric) ? @d : 0
    total += @l.total if @l
    total += @r.total if @r
  end
  def insert(arg)
    if @d
      if @l
        @l.insert(arg)
      else
        @l = Tree.new(arg)
      end
    else
      @d = arg
    end
  end
end

insert方法在语法上没有效果(它在一个部分缺少一个方法名称),但就我所知,这基本上就是它所做的.该方法的混淆非常厚:

    而不是仅仅做@l = whatever,它使用instance_variable_get()instance_variable_set().更糟糕的是,它instance_variable_get()只是个别名g().

    它将大部分功能包含在lambda函数中,并传递给它的名称@l.然后它用不太知名的语法调用这个函数,func[arg1, arg2]相当于func.call(arg1, arg2).



2> dustyburwell..:

这似乎只是几行中的二叉树实现.如果我对ruby语法的理解有限,我深表歉意:

class Tree                    // defining the class Tree

    def initialize *d;        // defines the initializer
        @d = d;               // sets the node value
    end

    def to_s;                 // defines the to_s(tring) function
        @l || @r ? ",>" : @d; // conditional operator. Can't tell exactly what this 
                              // function is intending. Would think it should make a
                              // recursive call or two if it's trying to do to_string
    end

    def total;                // defines the total (summation of all nodes) function
        @d.is_a ? (Numeric)   // conditional operator.  Returns
            ? @d              // @d if the data is numeric
            : 0               // or zero
        + (@l ? @l.total : 0) // plus the total for the left branch
        + (@r ? @r.total : 0) // plus the total for the right branch
    end

    def insert d              // defines an insert function
        ??                    // but I'm not going to try to parse it...yuck
    end

希望有所帮助......:/



3> 小智..:

它开始于此:

class Tree
  include Comparable

  attr_reader :data

  # Create a new node with one initial data element
  def initialize(data=nil)
    @data = data
  end

  # Spaceship operator. Comparable uses this to generate
  #   <, <=, ==, =>, >, and between?
  def <=>(other)
    @data.to_s <=> other.data.to_s
  end

  # Insert an object into the subtree including and under this Node.
  # First choose whether to insert into the left or right subtree,
  # then either create a new node or insert into the existing node at
  # the head of that subtree.
  def insert(data)
    if !@data
      @data = data
    else
      node = (data.to_s < @data.to_s) ? :@left : :@right
      create_or_insert_node(node, data)
    end
  end

  # Sum all the numerical values in this tree. If this data object is a
  # descendant of Numeric, add @data to the sum, then descend into both subtrees.
  def total
    sum = 0
    sum += @data if (@data.is_a? Numeric)
    sum += [@left, @right].map{|e| e.total rescue 0}.inject(0){|a,v|a+v}
    sum
  end

  # Convert this subtree to a String.
  # Format is: \.
  # Non-existant Nodes are printed as \<>.
  def to_s
    subtree = lambda do |tree|
      tree.to_s.empty? ? "<>" : tree
    end
    "<#{@data},#{subtree[@left]},#{subtree[@right]}>"
  end

  private ############################################################
  # Given a variable-as-symbol, insert data into the subtree incl. and under this node.
  def create_or_insert_node(nodename, data)
    if instance_variable_get(nodename).nil?
      instance_variable_set(nodename, Tree.new(data))
    else
      instance_variable_get(nodename).insert(data)
    end
  end

end

当我缩短它时,我想我确实打破了它.九行版本不太适用.无论如何我都很开心.:P

这是我最喜欢的部分:

def initialize*d;@d,=d;end

这实际上是利用并行分配来保存几个字符.您可以将此行展开为:

def initialize(*d)
  @d = d[0]
end



4> 小智..:

我发布了原始代码.对不起,但我没有费心去检查我是否做得对,而且由于标志不足,一堆东西被剥夺了.

class Tree
  def initialize*d;@d,=d;end
  def to_s;@l||@r?"<#{@d},<#{@l}>,<#{@r}>>":@d;end
  def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end
  def insert d
    alias g instance_variable_get
    p=lambda{|s,o|d.to_s.send(o,@d.to_s)&&
      (g(s).nil??instance_variable_set(s,Tree.new(d)):g(s).insert(d))}
    @d?p[:@l,:<]||p[:@r,:>]:@d=d
  end
end

这应该是什么样子.

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有