Решение на Втора задача от Антонио Николов

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

Към профила на Антонио Николов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 7 успешни тест(а)
  • 15 неуспешни тест(а)

Код

class TodoTask
attr_accessor :status
attr_accessor :description
attr_accessor :priority
attr_accessor :tags
def TodoTask.set_task(current_task)
task = TodoTask.new
unformatted_task = current_task.split("|")
TodoTask.set_task_methods(task, unformatted_task)
task
end
def TodoTask.set_task_methods(task, get_task)
task.status = get_task[0].strip.downcase.to_sym
task.description = get_task[1].strip
task.priority = get_task[2].strip.downcase.to_sym
TodoTask.set_tags(task, get_task[3])
end
def TodoTask.set_tags(task, get_tags)
task.tags = []
return task.tags if get_tags == nil
get_tags.split(",").each { |x| task.tags << x.strip }
end
end
#
class Criteria
def initialize(single, both, no)
@single = single
@both = both
@no = no
end
attr_accessor :single
attr_accessor :no
attr_accessor :both
def Criteria.status(status_kind)
Criteria.new [status_kind], [], []
end
def Criteria.priority(priority_kind)
Criteria.new [priority_kind], [], []
end
def Criteria.tags(tags_kind)
Criteria.new tags_kind, [], []
end
def |(other)
Criteria.new @single | other.single,
@both | other.both,
@no | other.no
end
def &(other)
if (@single == [] or other.single == []) then
self | other
else
Criteria.new @single & other.single,@both & other.both,@no | other.no
end
end
def !
Criteria.new @no,
@both,
@single | @both
end
def Criteria.ddd(task, task_array)
task.tags.each { |x| task_array << x }
end
end
class TodoList
include Enumerable
def each(&block)
@to_do_tasks.each { |member| block.call(member) }
end
def initialize(members)
@to_do_tasks = members
#members.each { |s| p s }
end
def TodoList.parse(list)
to_do_tasks = []
list.split("\n").select { |x| to_do_tasks << TodoTask.set_task(x) }
TodoList.new to_do_tasks
end
#
def filter(criteria_kind)
sublist, arr = [], []
each { |task| arr << task.status << task.priority | Criteria.ddd(task, arr)
filter_tasks(task, arr, criteria_kind, sublist) | arr = [] }
TodoList.new sublist
end
#
def adjoin(list)
adjoin_list = []
each { |x| adjoin_list << x }
list.each { |x| adjoin_list << x }
adjoin_list
end
def tasks_todo
select { |x| x.status == :todo }.count
end
def tasks_in_progress
select { |x| x.status == :current }.count
end
def tasks_completed
select { |x| x.status == :done }.count
end
def completed?
all? { |x| x.status == :done }
end
end

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

FFFFFFFFFFFF...FF....F

Failures:

  1) TodoList filters tasks by status
     Failure/Error: todo_list.filter(Criteria.status :done).map(&:description).should =~ [
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb94e9bd4>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:19: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 filters tasks by priority
     Failure/Error: todo_list.filter(Criteria.priority :high).map(&:description).should =~ [
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb94e717c>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:26: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 filters tasks by tag
     Failure/Error: todo_list.filter(Criteria.tags %w[food]).map(&:description).should =~ ['Eat spaghetti.']
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb94e4d78>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:36: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 filters tasks by multiple tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development ruby]).map(&:description).should =~ [
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb9476b98>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:40: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)>'

  5) TodoList filtering by multiple tags matches only tasks with all the tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development FMI]).map(&:description).should =~ ['Do the 5th Ruby challenge.']
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb947476c>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:47: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)>'

  6) TodoList supports a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb9464808>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:51: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)>'

  7) TodoList supports a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb94ddfa0>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:56: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)>'

  8) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb9493c20>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:66: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)>'

  9) TodoList supports simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb94914d4>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:76: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)>'

  10) TodoList supports complex filters combination
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) &
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb935de50>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:81: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)>'

  11) TodoList can be adjoined with another to-do list
     Failure/Error: development = todo_list.filter Criteria.tags(['development'])
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb935b3a8>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:97: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)>'

  12) TodoList constructs an object for each task
     Failure/Error: task = todo_list.filter(Criteria.tags ['health']).first
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb93590bc>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:110: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)>'

  13) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb934619c>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:132: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)>'

  14) TodoList doesn't modify the to-do list when filtering
     Failure/Error: todo_list.filter(Criteria.status :todo)
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb9343b40>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:136: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)>'

  15) TodoList creates a new object on filter
     Failure/Error: todo_list.filter(Criteria.tags %w[wtf]).should_not equal todo_list
     NoMethodError:
       undefined method `filter_tasks' for #<TodoList:0xb9327f94>
     # /tmp/d20131107-4393-18purgj/solution.rb:97:in `block in filter'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `call'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `block in each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:80:in `each'
     # /tmp/d20131107-4393-18purgj/solution.rb:96:in `filter'
     # /tmp/d20131107-4393-18purgj/spec.rb:173: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.03587 seconds
22 examples, 15 failures

Failed examples:

rspec /tmp/d20131107-4393-18purgj/spec.rb:18 # TodoList filters tasks by status
rspec /tmp/d20131107-4393-18purgj/spec.rb:25 # TodoList filters tasks by priority
rspec /tmp/d20131107-4393-18purgj/spec.rb:35 # TodoList filters tasks by tag
rspec /tmp/d20131107-4393-18purgj/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-18purgj/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-18purgj/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-18purgj/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-18purgj/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-18purgj/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-18purgj/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-18purgj/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-18purgj/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-18purgj/spec.rb:130 # TodoList checks if all tasks are completed
rspec /tmp/d20131107-4393-18purgj/spec.rb:135 # TodoList doesn't modify the to-do list when filtering
rspec /tmp/d20131107-4393-18purgj/spec.rb:172 # TodoList creates a new object on filter

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

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

+class TodoTask
+ attr_accessor :status
+ attr_accessor :description
+ attr_accessor :priority
+ attr_accessor :tags
+
+ def TodoTask.set_task(current_task)
+ task = TodoTask.new
+ unformatted_task = current_task.split("|")
+ TodoTask.set_task_methods(task, unformatted_task)
+ task
+ end
+
+ def TodoTask.set_task_methods(task, get_task)
+ task.status = get_task[0].strip.downcase.to_sym
+ task.description = get_task[1].strip
+ task.priority = get_task[2].strip.downcase.to_sym
+ TodoTask.set_tags(task, get_task[3])
+ end
+
+ def TodoTask.set_tags(task, get_tags)
+ task.tags = []
+ return task.tags if get_tags == nil
+ get_tags.split(",").each { |x| task.tags << x.strip }
+ end
+end
+#
+class Criteria
+ def initialize(single, both, no)
+ @single = single
+ @both = both
+ @no = no
+ end
+
+ attr_accessor :single
+ attr_accessor :no
+ attr_accessor :both
+
+ def Criteria.status(status_kind)
+ Criteria.new [status_kind], [], []
+ end
+
+ def Criteria.priority(priority_kind)
+ Criteria.new [priority_kind], [], []
+ end
+
+ def Criteria.tags(tags_kind)
+ Criteria.new tags_kind, [], []
+ end
+
+ def |(other)
+ Criteria.new @single | other.single,
+ @both | other.both,
+ @no | other.no
+ end
+
+ def &(other)
+ if (@single == [] or other.single == []) then
+ self | other
+ else
+ Criteria.new @single & other.single,@both & other.both,@no | other.no
+ end
+ end
+
+ def !
+ Criteria.new @no,
+ @both,
+ @single | @both
+ end
+
+ def Criteria.ddd(task, task_array)
+ task.tags.each { |x| task_array << x }
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ def each(&block)
+ @to_do_tasks.each { |member| block.call(member) }
+ end
+
+ def initialize(members)
+ @to_do_tasks = members
+ #members.each { |s| p s }
+ end
+
+ def TodoList.parse(list)
+ to_do_tasks = []
+ list.split("\n").select { |x| to_do_tasks << TodoTask.set_task(x) }
+ TodoList.new to_do_tasks
+ end
+ #
+ def filter(criteria_kind)
+ sublist, arr = [], []
+ each { |task| arr << task.status << task.priority | Criteria.ddd(task, arr)
+ filter_tasks(task, arr, criteria_kind, sublist) | arr = [] }
+ TodoList.new sublist
+ end
+ #
+ def adjoin(list)
+ adjoin_list = []
+ each { |x| adjoin_list << x }
+ list.each { |x| adjoin_list << x }
+ adjoin_list
+ end
+
+ def tasks_todo
+ select { |x| x.status == :todo }.count
+ end
+
+ def tasks_in_progress
+ select { |x| x.status == :current }.count
+ end
+
+ def tasks_completed
+ select { |x| x.status == :done }.count
+ end
+
+ def completed?
+ all? { |x| x.status == :done }
+ end
+end