module Mint::Generator
- autoload :Utilities, 'generator/utilities/comman_utils.rb'
+ autoload :Utilities, 'generator/utilities/comman_utils.rb'
+ autoload :Base, 'generator/base'
+ autoload :Arithmetic, 'generator/arithmetic_base'
+ autoload :HighOrderExpression, 'generator/high_order_expression_base'
end
-require 'generator/base'
-
generators = File.dirname(__FILE__) + '/generator/*.rb'
Dir[generators].each do |path|
+ next if /base/ =~ path
require path
end
DEFAULT_OPERATORS = %w[+ - * div]
- private
+ private
- include Utilities
+ include Utilities
def do_generate(setting)
- defaults = {:term_number => 2, :operators => DEFAULT_OPERATORS}.merge(setting)
- @options = options(defaults)
+ defaults = {:term_number => 2, :operators => DEFAULT_OPERATORS}.merge(setting)
+ @options = options(defaults)
expression.gsub(%r! (-[0-9./]+)!, ' (\\1)')
- end
+ end
- def operand
- raise 'need to overwrite'
- end
+ def operand
+ raise 'need to overwrite'
+ end
def expression
result = []
end while numerator_part == denominator_part
"#{numerator_part}/#{denominator_part}"
end
- end
+ end
end
+
class DecimalArithmetic < Arithmetic
- private
+ private
- def generate_problem
+ def generate_problem
defaults = { :minus => true, :min => 0, :max => 100 }
- do_generate(defaults)
- end
+ do_generate(defaults)
+ end
- def operand
- decimal
- end
- end
+ def operand
+ decimal
+ end
+ end
end
--- /dev/null
+# -*- coding: utf-8 -*-
+
+module Mint::Generator
+
+ class Expansion < HighOrderExpression
+
+ private
+ def generate_problem
+ defaults = {
+ :order_min => 2, :order_max => 3,
+ :factor_minus => true,
+ :factor_min => 1,
+ :factor_max => 100
+ }
+ do_generate(defaults)
+ end
+
+ def expression
+ expansion
+ end
+ end
+end
+
--- /dev/null
+# -*- coding: utf-8 -*-
+
+module Mint::Generator
+
+ class Factorization < HighOrderExpression
+
+ private
+ def generate_problem
+ defaults = {
+ :order_min => 2, :order_max => 3,
+ :factor_minus => true,
+ :factor_min => 1,
+ :factor_max => 100
+ }
+ do_generate(defaults)
+ end
+
+ def expression
+ factorization
+ end
+ end
+end
+
class FractionalArithmetic < Arithmetic
- private
+ private
- def generate_problem
+ def generate_problem
defaults = { :minus => true, :min => 0, :max => 100 }
- do_generate(defaults)
- end
+ do_generate(defaults)
+ end
- def operand
- fraction
- end
- end
+ def operand
+ fraction
+ end
+ end
end
include Utilities
- # TODO 係数ごとに設定できるようにする
- def generate_problem
- easy_factorization = {
- :order_min => 2, :order_max => 3, :x => %w[x],
- :factor_minus => true, :factor_min =>1, :factor_max => 100
- }
- factorization = {
- :order_min => 2, :order_max => 3, :x => ['x'],
- :factor_minus => true, :factor_min =>1, :factor_max => 100
- }
- expansion = {
- :order_min => 2, :order_max => 3, :x => ['x'],
- :factor_minus => true, :factor_min =>1, :factor_max => 100
- }
- options = options(:factorization => factorization, :expansion => expansion)
- expression(options)
- end
-
- def expression(options)
- result = []
- if options[:factorization]
- result << factorization(options[:factorization])
- end
- if options[:expansion]
- result << expansion(options[:expansion])
- end
- result.sample
+ def do_generate(options)
+ defaults = { :x => ['x'] }.merge(options)
+ @options = options(defaults)
+ expression
end
# 判別式
b**2 - 4 * a * c
end
- def factorization(options)
+ def factorization
order = create_integer(options[:order_min], options[:order_max], false)
case order
- when 2 then easy_factorization2(options)
- when 3 then easy_factorization3(options)
+ when 2 then easy_factorization2
+ when 3 then easy_factorization3
else
raise NotImplementedError
end
end
- def easy_factorization2(options)
+ def easy_factorization2
alpha = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
beta = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
a = 1
factorization2(a, b, c, x)
end
- def easy_factorization3(options)
+ def easy_factorization3
alpha = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
beta = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
gamma = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
factorization3(a, b, c, d, x)
end
- def old_factorization2(options)
+ def old_factorization2
begin
a, b, c = Array.new(3){ factor(options) }
end while a == 0 || (b == 0 && c == 0) || discriminant(a, b, c) < 0
factorization2(a, b, c, x)
end
- def old_factorization3(options)
+ def old_factorization3
begin
a, b, c, d = Array.new(4){ factor(options) }
# 一階微分した式 = 0 とした方程式の判別式 < 0 ならば元の三次方程式は実数解なし
gsub(%r!\+ -!, '- ').gsub(/\A1#{x}|([ -])1#{x}/, '\1'+x)
end
- def expansion(options)
+ def expansion
result = []
order = create_integer(options[:order_min], options[:order_max], false)
x = options[:x].sample
- order.times{ result << single(_factor(options), _factor(options), x) }
+ order.times{ result << single(_factor, _factor, x) }
result.join("")
end
- def _factor(options)
+ def _factor
factor(options, 'factor')
end
end
class OrdinaryArithmetic < Arithmetic
- private
+ private
- def generate_problem
+ def generate_problem
defaults = { :minus => true, :min => 0, :max => 100 }
- do_generate(defaults)
- end
+ do_generate(defaults)
+ end
- def operand
- integer
- end
- end
+ def operand
+ integer
+ end
+ end
end
--- /dev/null
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+module Mint::Generator
+ describe Expansion do
+ subject{ Expansion.new }
+ context "order 2" do
+ before do
+ options = {
+ :x => ['x', 'y', 'z'],
+ :order_min => 2, :order_max => 2,
+ :factor_min => 1, :factor_max => 10
+ }
+ @problems = subject.generate(options)
+ end
+ it{ @problems.should have(1).problem }
+ it{ @problems.first.scan(%r!\(.+?\)!).should have(2).terms }
+ it{ @problems.first.should_not match(/(?:\d)1[xyz]/) }
+ end
+ end
+end
+
--- /dev/null
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+module Mint::Generator
+
+ describe Factorization do
+
+ context "order 2" do
+
+ subject{ Factorization.new }
+
+ before do
+ options = {
+ :order_min => 2, :order_max => 2, :x => ['x', 'y', 'z'],
+ :factor_min => 1, :factor_max => 10
+ }
+ @problems = subject.generate(options)
+ end
+ it{ @problems.should have(1).problem }
+ it{ @problems.first.split(%r! [+-] !).should have_at_most(3).terms }
+ it{ @problems.first.should match(%r!(?:\d+)?[xyz]\^2 [+-] (?:\d+)?[xyz] [+-] \d+!) }
+ end
+ context "order 2 when factor may be 0" do
+ subject{ Factorization.new(10) }
+ before do
+ options = {
+ :order_min => 2, :order_max => 2,
+ :factor_min => 0, :factor_max => 10
+ }
+ @problems = subject.generate(options)
+ end
+ it{ @problems.should have(10).problem }
+ it "have 3 terms at most" do
+ @problems.each do |problem|
+ problem.split(%r! [+-] !).should have_at_most(3).terms
+ end
+ end
+ it "have 2 terms at least" do
+ @problems.each do |problem|
+ problem.split(%r! [+-] !).should have_at_least(2).terms
+ end
+ end
+ it "match format" do
+ @problems.each do |problem|
+ problem.should match(%r!(?:\d+)?x\^2(?: [+-] (?:\d+)?x)?(?: [+-] \d+)?!)
+ end
+ end
+ it "does not match '1x'" do
+ @problems.each do |problem|
+ problem.should_not match(%r!\A1x|[ -]1x!)
+ end
+ end
+ end
+ context "order 3" do
+
+ subject { Factorization.new }
+
+ before do
+ options = {
+ :order_min => 3, :order_max => 3,
+ :factor_min => 1, :factor_max => 10
+ }
+ @problems = subject.generate(options)
+ end
+ it{ @problems.should have(1).problem }
+ it{ @problems.first.split(%r! [+-] !).should have_at_most(4).terms }
+ it{ @problems.first.should match(%r!(?:\d+)?x\^3 [+-] (?:\d+)?x\^2 [+-] (?:\d+)?x [+-] \d+!) }
+ it{ @problems.first.should_not match(%r!\A1x|[ -]1x!) }
+ end
+ end
+end
+
--- /dev/null
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+module Mint::Generator
+ describe HighOrderExpression do
+ it 'need to write spec'
+ end
+end
+
+++ /dev/null
-# -*- coding: utf-8 -*-
-
-require File.dirname(__FILE__) + '/../spec_helper.rb'
-
-module Mint
- describe Generator::HighOrderExpression do
- context "generate factorization" do
- context "order 2" do
- subject{ Generator::HighOrderExpression.new }
- before do
- factorization = {
- :order_min => 2, :order_max => 2, :x => ['x', 'y', 'z'],
- :factor_minus => true, :factor_min => 1, :factor_max => 10
- }
- @problems = subject.generate(:factorization => factorization, :expansion => nil)
- end
- it{ @problems.should have(1).problem }
- it{ @problems.first.split(%r! [+-] !).should have_at_most(3).terms }
- it{ @problems.first.should match(%r!(?:\d+)?[xyz]\^2 [+-] (?:\d+)?[xyz] [+-] \d+!) }
- end
- context "order 2 when factor may be 0" do
- subject{ Generator::HighOrderExpression.new(10) }
- before do
- factorization = {
- :order_min => 2, :order_max => 2, :x => ['x'],
- :factor_minus => true, :factor_min => 0, :factor_max => 10
- }
- @problems = subject.generate(:factorization => factorization, :expansion => nil)
- end
- it{ @problems.should have(10).problem }
- it "have 3 terms at most" do
- @problems.each do |problem|
- problem.split(%r! [+-] !).should have_at_most(3).terms
- end
- end
- it "have 2 terms at least" do
- @problems.each do |problem|
- problem.split(%r! [+-] !).should have_at_least(2).terms
- end
- end
- it "match format" do
- @problems.each do |problem|
- problem.should match(%r!(?:\d+)?x\^2(?: [+-] (?:\d+)?x)?(?: [+-] \d+)?!)
- end
- end
- it "does not match '1x'" do
- @problems.each do |problem|
- problem.should_not match(%r!\A1x|[ -]1x!)
- end
- end
- end
- context "order 3" do
- subject{ Generator::HighOrderExpression.new }
- before do
- factorization = {
- :order_min => 3, :order_max => 3, :x => ['x'],
- :factor_minus => true, :factor_min => 1, :factor_max => 10
- }
- @problems = subject.generate(:factorization => factorization, :expansion => nil)
- end
- it{ @problems.should have(1).problem }
- it{ @problems.first.split(%r! [+-] !).should have_at_most(4).terms }
- it{ @problems.first.should match(%r!(?:\d+)?x\^3 [+-] (?:\d+)?x\^2 [+-] (?:\d+)?x [+-] \d+!) }
- it{ @problems.first.should_not match(%r!\A1x|[ -]1x!) }
- end
- end
-
- context "generate expansion" do
- subject{ Generator::HighOrderExpression.new }
- context "order 2" do
- before do
- expansion = {
- :order_min => 2, :order_max => 2, :x => ['x', 'y', 'z'],
- :factor_minus => true, :factor_min => 1, :factor_max => 10
- }
- @problems = subject.generate(:factorization => nil, :expansion => expansion)
- end
- it{ @problems.should have(1).problem }
- it{ @problems.first.scan(%r!\(.+?\)!).should have(2).terms }
- it{ @problems.first.should_not match(/(?:\d)1[xyz]/) }
- end
- end
- end
-end