Sha256: f942358ea4da8119d3a3e67057a4b8abd7898c52ca08f0dd1d0fa59332aa0b8f

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "open-uri"
require "nokogiri"

module Acclir
  module AtCoder
    # AtCoder problem
    class Task
      SAMPLE_INPUT_TITLE = "Sample Input"
      SAMPLE_OUTPUT_TITLE = "Sample Output"

      attr_reader :contest_id, :id

      def initialize(contest_id, id)
        @contest_id = contest_id
        @id = id
      end

      def samples
        @samples ||= extract_samples.map { |input, output| Sample.new(input, output) }
      end

      private

      def url
        @url ||= ATCODER_TASK_URL_PROC.call(contest_id, id)
      end

      def document
        @document ||= Nokogiri::HTML(URI.parse(url).open)
      end

      def extract_samples
        extract_sample_inputs.zip(extract_sample_outputs)
      end

      def extract_sample_inputs
        document.xpath("//*[@id='task-statement']//section").each_with_object([]) do |element, sample_inputs|
          sample_inputs << element.at("./pre").content if element.at("./h3").content.start_with?(SAMPLE_INPUT_TITLE)
        end
      end

      def extract_sample_outputs
        document.xpath("//*[@id='task-statement']//section").each_with_object([]) do |element, sample_outputs|
          sample_outputs << element.at("./pre").content if element.at("./h3").content.start_with?(SAMPLE_OUTPUT_TITLE)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acclir-0.1.0 lib/acclir/at_coder/task.rb