Sha256: 4078262005ea0a67a71e838a97fe1bc98b88a7c479798533b0a4c68f77c4ebfd

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require_relative "../question_info"

module Katagen
  module Strategy
    # :reek:U
    class InvalidIndex < StandardError
      def initialize(input) = super("#{input} is not a valid index")
    end

    # :reek:U
    class QuestionNotExist < StandardError
      def initialize(input) = super("question #{input} does not exist")
    end

    #
    # Generator for leetcode questions
    #
    class LeetCode
      def initialize(id)
        @id = id.to_i
        raise InvalidIndex.new(@id) unless @id.positive?
      end

      def build_question_info
        summary = questions_summary.find { |q| q["id"] == @id }
        raise QuestionNotExist.new(@id) unless summary
        topic, difficulty = summary.values_at("topic", "difficulty")

        QuestionInfo.new(
          "#{difficulty}/#{@id}.#{topic}",
          "https:/leetcode.com/problems/#{topic}"
        )
      end

      private

      #
      # @return [Array<Hash{String=>String}>] array of question summary
      #
      def questions_summary
        @summary ||= begin
          path = File.join(File.dirname(__FILE__), "../data/leetcode.json")
          raw_summary_text = File.read(path)
          JSON.parse(raw_summary_text)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katagen-1.0.1 lib/katagen/strategy/leetcode.rb