Skip to content

Instantly share code, notes, and snippets.

@albertein
Created February 10, 2012 18:54
Show Gist options
  • Save albertein/1791630 to your computer and use it in GitHub Desktop.
Save albertein/1791630 to your computer and use it in GitHub Desktop.
Second challenfe for http://apply.embed.ly
# encoding: UTF-8
def standar_deviation(values)
total = 0.0
values.each { |value| total += value }
average = total / values.length
total = 0.0
deviation = values.each { |value|
total += (value - average) ** 2
}
total /= values.length
return Math.sqrt total
end
def scan_text()
stack = []
depth = []
depths = []
closing = false
tag = ""
i = 0
file = File.new("step2_input.txt", "r")
file.each_line { |line|
line.chars { |c|
if c == "<" #starting tag
stack.push c
elsif c == ">" #tag ended
if !closing and tag == "p" #Found new p stack
depths.push depth.length #Push the current length to the results
end
if closing #end tag, pop from stack to decreased current depth
depth.pop
closing = false
elsif #Push the tag name to the depth stack
depth.push tag
end
tag = ""
stack.pop
elsif c == "/" #Closing tag, set closing flag
if stack.last == "<"
closing = true
end
else #Part of the current stack, build up its name
if stack.last == "<"
tag += c
end
end
}
}
file.close
puts standar_deviation depths
end
scan_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment