Решение на Втора задача от Михаил Господинов

Обратно към всички решения

Към профила на Михаил Господинов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 18 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class ArrayMethods
def self.arr_contains(array,subarray)
array.all? { |e| subarray.include?(e) }
end
end
class Criteria
attr_reader :value_check
def initialize(checker)
@value_check = checker
end
def self.status(symbol)
Criteria.new ->(record){record["status"] == symbol}
end
def self.priority(symbol)
Criteria.new ->(record){record["priority"] == symbol}
end
def self.tags(tags)
Criteria.new ->(record){
ArrayMethods.arr_contains tags,record["tags"]
}
end
def &(other)
Criteria.new ->(record){@value_check[record] and other.value_check[record]}
end
def |(other)
Criteria.new ->(record){@value_check[record] or other.value_check[record]}
end
def !
Criteria.new ->(record){not @value_check[record]}
end
end
class BaseLineResolver
def resolve_line(text_values)
hash , values = {} , line_values(text_values.split('|'))
keys.each_index { |i|
hash[keys[i]] = values[i]
}
hash
end
end
class TodoLineResolver < BaseLineResolver
def resolve_array_value(value)
if value == nil then return [] end
value.split(',').map { |arr_value|
arr_value.strip.freeze
}
end
def line_value_array(vals)
[vals[0].downcase.to_sym,
vals[1],
vals[2].downcase.to_sym,
resolve_array_value(vals[3])]
end
def line_values(vals)
line_value_array vals.map {|value|
value.strip
}
end
def keys
["status","description","priority","tags"]
end
end
class DataRecord
attr_reader :record
def initialize(record)
@record = record
end
def [](key)
@record[key]
end
end
class TodoRecord < DataRecord
def status
self["status"]
end
def description
self["description"]
end
def priority
self["priority"]
end
def tags
self["tags"]
end
end
class DataSet
include Enumerable
attr_reader :data
def initialize(data)
@data = data
end
def each &block
@data.each do |data|
yield data
end
end
def filter(criteria)
DataSet.new @data.select { |record|
criteria.value_check.call(record)
}
end
def adjoin(data_set)
DataSet.new @data + data_set.data
end
end
class TodoList < DataSet
@resolver = TodoLineResolver.new
def self.parse(list)
TodoList.new list.lines.map { |line|
TodoRecord.new @resolver.resolve_line line
}
end
def tasks_todo
(filter Criteria.status(:todo)).length
end
def tasks_in_progress
(filter Criteria.status(:current)).length
end
def tasks_completed
(filter Criteria.status(:done)).length
end
def completed?
(tasks_todo == 0) and (tasks_in_progress == 0)
end
end

Лог от изпълнението

............FFFF......

Failures:

  1) TodoList returns the number of the tasks todo
     Failure/Error: todo_list.tasks_todo.should eq 5
     NoMethodError:
       undefined method `length' for #<DataSet:0xb92b53a4>
     # /tmp/d20131107-4393-6lqkcd/solution.rb:140:in `tasks_todo'
     # /tmp/d20131107-4393-6lqkcd/spec.rb:119:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) TodoList returns the number of the tasks in progress
     Failure/Error: todo_list.tasks_in_progress.should eq 2
     NoMethodError:
       undefined method `length' for #<DataSet:0xb9295b1c>
     # /tmp/d20131107-4393-6lqkcd/solution.rb:144:in `tasks_in_progress'
     # /tmp/d20131107-4393-6lqkcd/spec.rb:123:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) TodoList returns the number of the completed tasks
     Failure/Error: todo_list.tasks_completed.should eq 2
     NoMethodError:
       undefined method `length' for #<DataSet:0xb925443c>
     # /tmp/d20131107-4393-6lqkcd/solution.rb:148:in `tasks_completed'
     # /tmp/d20131107-4393-6lqkcd/spec.rb:127:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `length' for #<DataSet:0xb9217ce4>
     # /tmp/d20131107-4393-6lqkcd/solution.rb:140:in `tasks_todo'
     # /tmp/d20131107-4393-6lqkcd/solution.rb:152:in `completed?'
     # /tmp/d20131107-4393-6lqkcd/spec.rb:131:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.04945 seconds
22 examples, 4 failures

Failed examples:

rspec /tmp/d20131107-4393-6lqkcd/spec.rb:118 # TodoList returns the number of the tasks todo
rspec /tmp/d20131107-4393-6lqkcd/spec.rb:122 # TodoList returns the number of the tasks in progress
rspec /tmp/d20131107-4393-6lqkcd/spec.rb:126 # TodoList returns the number of the completed tasks
rspec /tmp/d20131107-4393-6lqkcd/spec.rb:130 # TodoList checks if all tasks are completed

История (2 версии и 0 коментара)

Михаил обнови решението на 06.11.2013 03:17 (преди около 11 години)

+class ArrayMethods
+ def self.arr_contains(array,subarray)
+ array.all? { |e| subarray.include?(e) }
+ end
+end
+
+class Criteria
+ attr_reader :value_check
+
+ def initialize(checker)
+ @value_check = checker
+ end
+
+ def self.status(symbol)
+ Criteria.new ->(record){record["status"] == symbol}
+ end
+
+ def self.priority(symbol)
+ Criteria.new ->(record){record["priority"] == symbol}
+ end
+
+ def self.tags(tags)
+ Criteria.new ->(record){
+ ArrayMethods.arr_contains tags,record["tags"]
+ }
+ end
+
+ def &(other)
+ Criteria.new ->(record){@value_check[record] and other.value_check[record]}
+ end
+
+ def |(other)
+ Criteria.new ->(record){@value_check[record] or other.value_check[record]}
+ end
+
+ def !
+ Criteria.new ->(record){not @value_check[record]}
+ end
+end
+
+class BaseLineResolver
+ def resolve_line(text_values)
+ hash , values = {} , line_values(text_values.split('|'))
+ keys.each_index { |i|
+ hash[keys[i]] = values[i]
+ }
+ hash
+ end
+end
+
+class TodoLineResolver < BaseLineResolver
+ def resolve_array_value(value)
+ if value == nil then return [] end
+ value.split(',').map { |arr_value|
+ arr_value.strip.freeze
+ }
+ end
+
+ def line_value_array(vals)
+ [vals[0].downcase.to_sym,
+ vals[1],
+ vals[2].downcase.to_sym,
+ resolve_array_value(vals[3])]
+ end
+
+ def line_values(vals)
+ line_value_array vals.map {|value|
+ value.strip
+ }
+ end
+
+ def keys
+ ["status","description","priority","tags"]
+ end
+end
+
+class DataSet
+ include Enumerable
+ attr_reader :data
+
+ def initialize(data)
+ @data = data
+ end
+
+ def each &block
+ @data.each do |data|
+ block_given? block.call data: data
+ end
+ end
+end
+
+class TodoList < DataSet
+ @resolver = TodoLineResolver.new
+ def self.parse(list)
+ TodoList.new list.lines.map { |line|
+ @resolver.resolve_line line
+ }
+ end
+
+ def filter(criteria)
+ @data.select { |record|
+ criteria.value_check.call(record)
+ }
+ end
+
+ def tasks_todo
+ (filter Criteria.status(:todo)).length
+ end
+
+ def tasks_in_progress
+ (filter Criteria.status(:current)).length
+ end
+
+ def tasks_completed
+ (filter Criteria.status(:done)).length
+ end
+
+ def completed?
+ (tasks_todo == 0) and (tasks_in_progress == 0)
+ end
+end

Михаил обнови решението на 06.11.2013 15:30 (преди около 11 години)

class ArrayMethods
def self.arr_contains(array,subarray)
array.all? { |e| subarray.include?(e) }
end
end
class Criteria
attr_reader :value_check
def initialize(checker)
@value_check = checker
end
def self.status(symbol)
Criteria.new ->(record){record["status"] == symbol}
end
def self.priority(symbol)
Criteria.new ->(record){record["priority"] == symbol}
end
def self.tags(tags)
Criteria.new ->(record){
ArrayMethods.arr_contains tags,record["tags"]
}
end
def &(other)
Criteria.new ->(record){@value_check[record] and other.value_check[record]}
end
def |(other)
Criteria.new ->(record){@value_check[record] or other.value_check[record]}
end
def !
Criteria.new ->(record){not @value_check[record]}
end
end
class BaseLineResolver
def resolve_line(text_values)
hash , values = {} , line_values(text_values.split('|'))
keys.each_index { |i|
hash[keys[i]] = values[i]
}
hash
end
end
class TodoLineResolver < BaseLineResolver
def resolve_array_value(value)
if value == nil then return [] end
value.split(',').map { |arr_value|
arr_value.strip.freeze
}
end
def line_value_array(vals)
[vals[0].downcase.to_sym,
vals[1],
vals[2].downcase.to_sym,
resolve_array_value(vals[3])]
end
def line_values(vals)
line_value_array vals.map {|value|
value.strip
}
end
def keys
["status","description","priority","tags"]
end
end
+class DataRecord
+ attr_reader :record
+ def initialize(record)
+ @record = record
+ end
+
+ def [](key)
+ @record[key]
+ end
+end
+
+class TodoRecord < DataRecord
+ def status
+ self["status"]
+ end
+
+ def description
+ self["description"]
+ end
+
+ def priority
+ self["priority"]
+ end
+
+ def tags
+ self["tags"]
+ end
+end
+
class DataSet
include Enumerable
attr_reader :data
def initialize(data)
@data = data
end
def each &block
@data.each do |data|
- block_given? block.call data: data
+ yield data
end
end
+
+ def filter(criteria)
+ DataSet.new @data.select { |record|
+ criteria.value_check.call(record)
+ }
+ end
+
+ def adjoin(data_set)
+ DataSet.new @data + data_set.data
+ end
end
class TodoList < DataSet
@resolver = TodoLineResolver.new
def self.parse(list)
TodoList.new list.lines.map { |line|
- @resolver.resolve_line line
+ TodoRecord.new @resolver.resolve_line line
}
end
- def filter(criteria)
- @data.select { |record|
- criteria.value_check.call(record)
- }
- end
-
def tasks_todo
(filter Criteria.status(:todo)).length
end
def tasks_in_progress
(filter Criteria.status(:current)).length
end
def tasks_completed
(filter Criteria.status(:done)).length
end
def completed?
(tasks_todo == 0) and (tasks_in_progress == 0)
end
-end
+end