calcexam 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Calcexam
2
+
3
+ Simple training command line application. Increasing your arithmetical ability.
4
+
5
+ ## Installation
6
+
7
+ Type in command line:
8
+
9
+ $ gem install calcexam
10
+
11
+ ## Usage
12
+
13
+ $ calcexam --operation=multiply 11..20 11..20
14
+ 17 * 11 = 187
15
+ Right!
16
+ 14 * 18 = 224
17
+ Try again
18
+ 14 * 18 = 262
19
+ Wrong!
20
+ 19 * 12 = _
21
+
22
+ $ calcexam --operation=plus --no-shuffle 2345 4567,5678
23
+ 2345 + 4567 = 6912
24
+ Right!
25
+ 2345 + 5678 = 8023
26
+ Right!
27
+ Results: 100.0% (2/2)
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rake/gempackagetask'
2
+ spec = eval(File.read('calcexam.gemspec'))
3
+ Rake::GemPackageTask.new(spec) do |pkg|
4
+ end
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "test"
9
+ t.test_files = FileList['test/tc_*.rb']
10
+ end
11
+ task :default => :test
12
+
13
+ require 'cucumber'
14
+ require 'cucumber/rake/task'
15
+ Cucumber::Rake::Task.new(:features) do |t|
16
+ t.cucumber_opts = "features --format pretty -x"
17
+ t.fork = false
18
+ end
data/bin/calcexam ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+ require 'optparse'
4
+ require 'yaml'
5
+ require 'calcexam'
6
+ require 'readline'
7
+
8
+ EXIT_COMMANDS = ['exit', 'quit']
9
+
10
+ options = {
11
+ operation: :*,
12
+ limit: 10,
13
+ shuffle: true,
14
+ x: (11..20).to_a,
15
+ y: (11..20).to_a,
16
+ }
17
+ OPERATIONS = {
18
+ multiplication: :*, multiply: :*, mult: :*, x: :*, :* => :*,
19
+ plus: :+, sum: :+, :+ => :+,
20
+ minus: :-, diff: :-, difference: :-, :- => :-
21
+ }
22
+
23
+ CONFIG_FILE = File.join(ENV['HOME'], '.calcexam.rc.yaml')
24
+ if File.exists? CONFIG_FILE
25
+ config_options = YAML.load_file(CONFIG_FILE)
26
+ options.merge!(config_options)
27
+ else
28
+ begin
29
+ File.open(CONFIG_FILE, 'w') { |file| YAML::dump(options, file) }
30
+ STDERR.puts "Initialized configuration file in #{CONFIG_FILE}"
31
+ rescue Errno::EACCES => ex
32
+ #STDERR.puts "Cannot create config file. #{ex.message}"
33
+ end
34
+ end
35
+
36
+ option_parser = OptionParser.new do |opts|
37
+ executable_name = File.basename($PROGRAM_NAME)
38
+ opts.banner = <<EOF
39
+ Simple training application. Increasing your arithmetical ability by passing small exam.
40
+
41
+ Usage: #{executable_name} [options] x1..xN y1..yM
42
+ #{executable_name} [options] x1,x2,x3 y1,y2
43
+ #{executable_name} [options] x1..xN y1
44
+
45
+ EOF
46
+
47
+ opts.on("-o OPERATION", "--operation", OPERATIONS, "Set prefered operation. Allowed: mult (multiply, multiplication), plus (sum), minus") do |operation|
48
+ options[:operation] = operation
49
+ end
50
+
51
+ opts.on("-l LIMIT", "--[no-]limit", 'Limit of iterations. Use prefix "no-" for passing all n*m iterations', Integer) do |limit|
52
+ options[:limit] = limit
53
+ end
54
+
55
+ opts.on("--no-shuffle", "--no-s", "Don't shuffle pairs (Passing iterations in order increase consistent numbers)") do
56
+ options[:shuffle] = false
57
+ end
58
+
59
+ opts.on("--no-color", "--no-c", "Don't use colors") do
60
+ options[:'no-color'] = true
61
+ end
62
+ end
63
+
64
+ begin
65
+ option_parser.parse!
66
+ Sickill::Rainbow.enabled = false if options[:'no-color']
67
+ rescue OptionParser::InvalidArgument => ex
68
+ STDERR.puts ex.message
69
+ STDERR.puts option_parser
70
+ exit 1
71
+ end
72
+
73
+ options[:x] = ARGV[0] if ARGV[0]
74
+ options[:y] = ARGV[1] if ARGV[1]
75
+ #puts options.inspect
76
+
77
+ # The exam began, silence...
78
+ exam = Calcexam::Exam.new
79
+
80
+ # Allows simple invocation
81
+ if m = /(\d+)\s*(\*|\+|-)\s*(\d+)\s*=\s*(\d+)/.match(ARGV.join)
82
+ m[1].to_i.send(m[2], m[3].to_i) == m[4].to_i ? exam.right_answer! : exam.wrong_answer!
83
+ exit 0
84
+ end
85
+
86
+ matrix = Calcexam::Matrix.new(options[:operation], options[:x], options[:y])
87
+ matrix.shuffle! if options[:shuffle]
88
+ matrix.each do |a, b|
89
+ command = Readline.readline("#{a} #{matrix.operation} #{b} = ", true)
90
+ exit(2) if command.nil? or EXIT_COMMANDS.include?(command)
91
+ number = command.to_i
92
+ if a.send(matrix.operation, b) == number
93
+ exam.right_answer!
94
+ else
95
+ exam.try_again
96
+ command = Readline.readline("#{a} #{matrix.operation} #{b} = ", true)
97
+ exit(2) if command.nil? or EXIT_COMMANDS.include?(command)
98
+ number = command.to_i
99
+ if a.send(matrix.operation, b) == number
100
+ exam.right_answer!
101
+ else
102
+ exam.wrong_answer!
103
+ end
104
+ end
105
+ break if exam.answers_count >= options[:limit]
106
+ end
107
+ exam.results
data/calcexam.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/calcexam/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "calcexam"
6
+ gem.version = Calcexam::VERSION
7
+ gem.platform = Gem::Platform::RUBY
8
+ gem.authors = ["Alex Avoiants"]
9
+ gem.email = ["shhavel@gmail.com"]
10
+ gem.homepage = "https://github.com/shhavel/calcexam"
11
+ gem.summary = %q{Simple training application. Increasing your arithmetical ability by passing small exam.}
12
+ gem.description = %q{Simple training command line application. Increasing your arithmetical ability. If you need a feature added, send me a message on Github!}
13
+ gem.files = %w(
14
+ calcexam.gemspec
15
+ bin/calcexam
16
+ lib/calcexam.rb
17
+ lib/calcexam/exam.rb
18
+ lib/calcexam/matrix.rb
19
+ lib/calcexam/version.rb
20
+ Rakefile
21
+ README.md
22
+ )
23
+ gem.test_files = %w(
24
+ test/tc_calcexam.rb
25
+ features/equality.feature
26
+ features/support/env.rb
27
+ )
28
+ gem.require_paths << 'lib'
29
+ gem.bindir = 'bin'
30
+ gem.executables << 'calcexam'
31
+ gem.add_dependency('rainbow')
32
+ gem.add_development_dependency('rake')
33
+ gem.add_development_dependency('mocha')
34
+ gem.add_development_dependency('aruba', '~> 0.4.6')
35
+ end
@@ -0,0 +1,17 @@
1
+ Feature: User can assert one equality
2
+ User invokes program for different operations
3
+ Using simple invocation - asserts equality
4
+
5
+ Scenario: Assert equality for multiply
6
+ When I successfully run `calcexam 14\*17=258`
7
+ Then the stdout should contain "Wrong!"
8
+ When I successfully run `calcexam 14\*17=238`
9
+ Then the stdout should contain "Right!"
10
+
11
+ Scenario: Assert equality for minus
12
+ When I successfully run `calcexam 25678-8762=17916`
13
+ Then the stdout should contain "Wrong!"
14
+
15
+ Scenario: Assert equality for plus
16
+ When I successfully run `calcexam 2635+2632=5267`
17
+ Then the stdout should contain "Right!"
@@ -0,0 +1 @@
1
+ require 'aruba/cucumber'
@@ -0,0 +1,41 @@
1
+ require 'rainbow'
2
+
3
+ module Calcexam
4
+ class Exam
5
+ attr_reader :answers_count, :right_answers_count
6
+
7
+ def initialize
8
+ @answers_count, @right_answers_count = 0, 0
9
+ end
10
+
11
+ def right_answer!
12
+ @answers_count += 1
13
+ @right_answers_count += 1
14
+ puts 'Right!'.bright.color(:green)
15
+ end
16
+
17
+ def wrong_answer!
18
+ @answers_count += 1
19
+ puts 'Wrong!'.bright.color(:red)
20
+ end
21
+
22
+ def try_again
23
+ puts 'Try again'.bright.color(:yellow)
24
+ end
25
+
26
+ def results
27
+ mark = (right_answers_count.to_f / [answers_count, 1].max * 100).round(1)
28
+ color = case
29
+ when mark >= 90
30
+ :green
31
+ when mark >= 70
32
+ :blue
33
+ when mark >= 50
34
+ :yellow
35
+ else
36
+ :red
37
+ end
38
+ puts "Results: " + "#{mark}%".bright.color(color) + " (#{right_answers_count}/#{answers_count})".color(color)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ module Calcexam
2
+ class Matrix
3
+ include Enumerable
4
+ attr_reader :operation, :x, :y
5
+
6
+ def initialize(operation=:*, x=[], y=[])
7
+ @operation = operation
8
+ @x, @y = parse_interval(x), parse_interval(y)
9
+ end
10
+
11
+ def each
12
+ @x.each do |x|
13
+ @y.each do |y|
14
+ yield([x, y])
15
+ end
16
+ end
17
+ end
18
+
19
+ def shuffle!
20
+ @x.shuffle!
21
+ @y.shuffle!
22
+ self
23
+ end
24
+
25
+ def size
26
+ @x.size * @y.size
27
+ end
28
+
29
+ def [](i, j=nil)
30
+ if j.nil?
31
+ j = i / @x.size
32
+ i = i % @x.size
33
+ end
34
+ [@x[i], @y[j]]
35
+ end
36
+
37
+ private
38
+ def parse_interval(interval)
39
+ case
40
+ when interval.respond_to?(:to_a)
41
+ interval.to_a
42
+ when interval.is_a?(String) && m = /^(\d+)\.\.(\d+)$/.match(interval)
43
+ (m[1].to_i..m[2].to_i).to_a
44
+ when interval.is_a?(String) && interval =~ /^\d+\s*(,\s*\d+)+$/
45
+ interval.split(',').map(&:to_i)
46
+ else
47
+ [interval.to_i]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Calcexam
2
+ VERSION = '0.0.1'
3
+ end
data/lib/calcexam.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'calcexam/version'
2
+ require 'calcexam/matrix'
3
+ require 'calcexam/exam'
@@ -0,0 +1,48 @@
1
+ require 'test/unit'
2
+ require 'calcexam'
3
+ require 'mocha'
4
+
5
+ class CalcexamTest < Test::Unit::TestCase
6
+ def teardown
7
+ STDOUT.unstub(:puts)
8
+ end
9
+
10
+ def test_interval_parsing
11
+ matrix = Calcexam::Matrix.new()
12
+ assert_equal [5,16,78], matrix.send(:parse_interval, [5,16,78])
13
+ assert_equal (23..45).to_a, matrix.send(:parse_interval, '23..45')
14
+ assert_equal [3,73,65], matrix.send(:parse_interval, '3,73,65')
15
+ assert_equal [567], matrix.send(:parse_interval, '567')
16
+ end
17
+
18
+ def test_matrix_size
19
+ matrix = Calcexam::Matrix.new(:*, [5,16,78], '23..45')
20
+ assert_equal 69, matrix.size
21
+ matrix = Calcexam::Matrix.new(:*, '3,5,7', '456..459')
22
+ assert_equal 12, matrix.size
23
+ end
24
+
25
+ def test_matrix_each
26
+ matrix = Calcexam::Matrix.new(:*, '5..7', '3,4')
27
+ count = 0
28
+ matrix.each do |a, b|
29
+ assert [5,6,7].include?(a) && [3,4].include?(b), "each() sends to the block parameters from outside of intervals"
30
+ count += 1
31
+ end
32
+ assert_equal matrix.size, count, "each() don't yields block m*n times"
33
+ end
34
+
35
+ def test_exam_results
36
+ Sickill::Rainbow.enabled = false
37
+ STDOUT.stubs(:puts)
38
+ STDOUT.expects(:puts).with 'Results: 75.0% (3/4)'
39
+
40
+ exam = Calcexam::Exam.new
41
+ exam.right_answer!
42
+ exam.right_answer!
43
+ exam.try_again
44
+ exam.wrong_answer!
45
+ exam.right_answer!
46
+ exam.results
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calcexam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Avoiants
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rainbow
16
+ requirement: &2164476120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2164476120
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2164475440 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2164475440
36
+ - !ruby/object:Gem::Dependency
37
+ name: mocha
38
+ requirement: &2164474740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2164474740
47
+ - !ruby/object:Gem::Dependency
48
+ name: aruba
49
+ requirement: &2164474140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.6
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2164474140
58
+ description: Simple training command line application. Increasing your arithmetical
59
+ ability. If you need a feature added, send me a message on Github!
60
+ email:
61
+ - shhavel@gmail.com
62
+ executables:
63
+ - calcexam
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - calcexam.gemspec
68
+ - bin/calcexam
69
+ - lib/calcexam.rb
70
+ - lib/calcexam/exam.rb
71
+ - lib/calcexam/matrix.rb
72
+ - lib/calcexam/version.rb
73
+ - Rakefile
74
+ - README.md
75
+ - test/tc_calcexam.rb
76
+ - features/equality.feature
77
+ - features/support/env.rb
78
+ homepage: https://github.com/shhavel/calcexam
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.11
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Simple training application. Increasing your arithmetical ability by passing
103
+ small exam.
104
+ test_files:
105
+ - test/tc_calcexam.rb
106
+ - features/equality.feature
107
+ - features/support/env.rb