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
Run Code Online (Sandbox Code Playgroud)
有人愿意尝试解释这是做什么的吗?它似乎是一个问题的答案我问了一些太聪明的代码.但是,对我来说,判断这只是一个笑话太聪明了.如果不是,我有兴趣知道它是如何工作的,如果有人在意解释.
Chu*_*uck 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
Run Code Online (Sandbox Code Playgroud)
insert方法在语法上没有效果(它在一个部分缺少一个方法名称),但就我所知,这基本上就是它所做的.该方法的混淆非常厚:
而不是仅仅做@l = whatever,它使用instance_variable_get()和instance_variable_set().更糟糕的是,它instance_variable_get()只是个别名g().
它将大部分功能包含在lambda函数中,并传递给它的名称@l.然后它用不太知名的语法调用这个函数,func[arg1, arg2]相当于func.call(arg1, arg2).
这似乎只是几行中的二叉树实现.如果我对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
Run Code Online (Sandbox Code Playgroud)
希望有所帮助......:/
小智 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
Run Code Online (Sandbox Code Playgroud)
当我缩短它时,我想我确实打破了它.九行版本不太适用.无论如何我都很开心.:P
这是我最喜欢的部分:
def initialize*d;@d,=d;end
Run Code Online (Sandbox Code Playgroud)
这实际上是利用并行分配来保存几个字符.您可以将此行展开为:
def initialize(*d)
@d = d[0]
end
Run Code Online (Sandbox Code Playgroud)
小智 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
Run Code Online (Sandbox Code Playgroud)
这应该是什么样子.
| 归档时间: |
|
| 查看次数: |
1399 次 |
| 最近记录: |