6
class Tree
  def initialize*d;@d,=d;end
  def to_s;@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,:]:@d=d
  end
end

有人愿意尝试解释这是做什么的吗?它作为我询问的关于代码太聪明的问题的答案出现了。但我太聪明了,无法判断这是否只是一个玩笑。如果不是,我很想知道它是如何工作的,如果有人愿意解释的话。

4

4 回答 4

15

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

那是一些很好的混淆代码。与大多数混淆代码一样,它主要是大量的三元运算符,并且顽固地拒绝在正常人的情况下放入空格。这里基本上是更正常的写法:

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 方法在语法上无效(它的一部分缺少方法名称),但据我所知,这基本上就是它的作用。该方法中的混淆非常严重:

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

  2. 它将大部分功能包装在一个 lambda 函数中,并将@l. 然后它用鲜为人知的语法调用这个函数func[arg1, arg2],它等价于func.call(arg1, arg2).

于 2009-04-03T23:04:11.870 回答
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

希望对一些人有所帮助......:/

于 2009-04-03T22:40:42.597 回答
7

一开始是这样的:

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: <tt>\<data,left_subtree,right_subtree></tt>.
  # Non-existant Nodes are printed as <tt>\<></tt>.
  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
于 2009-04-03T23:45:12.413 回答
6

我发布了原始代码。对不起,但我没有费心去检查我是否做得对,而且由于少于符号,一堆东西被剥离了。

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

这就是它应该的样子。

于 2009-04-04T00:05:10.337 回答