diff --git a/lab3_task1_funcation_block_arg/main.rb b/lab3_task1_funcation_block_arg/main.rb index 20c5ebe..0f80877 100644 --- a/lab3_task1_funcation_block_arg/main.rb +++ b/lab3_task1_funcation_block_arg/main.rb @@ -1,9 +1,9 @@ def indices_sorted_by_descending_values(array) - indices = array.each_with_index.to_a - sorted_indices = indices.sort_by { |(element, index)| -element } - result = sorted_indices.map { |(element, index)| index } - result + array.each_with_index.to_a + .sort_by { |(element, index)| -element } + .map { |(element, index)| index } + end diff --git a/lab3_task3_html_tree/main.rb b/lab3_task3_html_tree/main.rb new file mode 100644 index 0000000..053190f --- /dev/null +++ b/lab3_task3_html_tree/main.rb @@ -0,0 +1,95 @@ +require 'nokogiri' + +class HtmlTree + include Enumerable + + def initialize(html) + puts "Input HTML:\n#{html.inspect}" # Проверяем входной HTML + @root = parse_html(html) + raise "Parsed HTML tree is empty" if @root.nil? + end + + def each(order = :breadth_first, &block) + case order + when :breadth_first + breadth_first_traversal(&block) + when :depth_first + depth_first_traversal(&block) + else + raise ArgumentError, "Unknown order: #{order}" + end + end + + private + + def parse_html(html) + doc = Nokogiri::HTML::DocumentFragment.parse(html) + root_node = doc.at_css('body') || doc.children.find(&:element?) || doc.root + return nil if root_node.nil? || root_node.children.empty? + + build_tree(root_node) + end + + def build_tree(node) + if node.element? + children = node.children.map { |child| build_tree(child) }.compact + HtmlTag.new(node.name, node.attributes.transform_values(&:value), children) + elsif node.text? && !node.content.strip.empty? + HtmlTag.new("text", { "content" => node.content.strip }, []) + end + end + + def breadth_first_traversal + queue = [@root].compact + until queue.empty? + current = queue.shift + next if current.nil? + + yield current + queue.concat(current.children.compact) if current.has_children? + end + end + + def depth_first_traversal(node = @root, &block) + return if node.nil? + + yield node + node.children.compact.each { |child| depth_first_traversal(child, &block) } if node.has_children? + end +end + +class HtmlTag + attr_reader :name, :attributes, :children + + def initialize(name, attributes, children = []) + @name = name + @attributes = attributes + @children = children + end + + def has_children? + !@children.empty? + end + + def to_s + "#{@name} #{@attributes}" + end +end + +html = <<-HTML +
Hello, world!
+