5
#class Array; def sum; inject( 0 ) { |sum,x| sum+x }; end; end
9
res = Float(node.child.content) rescue 0 if (node.child and node.child.text?())
11
node.each_attr do |attr|
12
res = Float(attr.child.content) rescue 0
22
node.children.each { |cur_node|
26
cond = if (i>3); 1 else; nil end
28
node.children.each { |cur_node|
33
res += siblings(cur_node)
6
41
parser = XML::Parser.file(xml)
45
# This is slowest 330 ms
46
# sum = siblings(doc.root)
49
# doc.find("//*[(@* or text()) and (count(../child::*)>3)]").each do |node|
51
doc.find("//*[(number(@*) or number(text())) and (count(../child::*)>3)]").each do |node|
55
#Fastest approach ~ 177ms
57
res = Float(node.child.content) rescue 0 if (node.child and node.child.text?())
61
node.each_attr do |attr|
62
res = Float(attr.child.content) rescue 0
72
sum = doc.find("//*[(number(@*) or number(text())) and (count(../child::*)>3)]").map { |node|
73
res = Float(node.child.content) rescue nil if (node.child and node.child.text?())
74
next res if ((res)&&(res!=0))
76
node.each_attr do |attr|
77
res = Float(attr.child.content) rescue nil
78
break res if ((res)&&(res!=0))
84
sum = doc.find("//*[(number(@*) or number(text())) and (count(../child::*)>3)]").inject(0) { |sum, node|
85
res = Float(node.child.content) rescue nil if (node.child and node.child.text?())
86
next sum+res if ((res)&&(res!=0))
88
node.each_attr do |attr|
89
res = Float(attr.child.content) rescue nil
90
break res if ((res)&&(res!=0))
95
# print "Sum: #{sum}\n"
99
walk_tree = ENV['walk_tree']
11
101
if ($*.length > 0)
12
102
iterations = $*[0].to_i
20
110
filename = lambda { |i| "../xml.tmp/#{i}.xml" }
23
parse(filename.call(0))
113
parse(filename.call(0), walk_tree)
25
115
iterations.times { |i|
26
parse(filename.call(i+1))
116
parse(filename.call(i+1), walk_tree)