Решение на Втора задача от Петър Добрев

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

Към профила на Петър Добрев

Резултати

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

Код

class Todo
attr_accessor :status, :description, :priority, :tags
def initialize(state, descr, prior, tag)
@status = state
@description = descr
@priority = prior
@tags = tag
end
def to_a
[@status] + [@description] + [@priority] + [@tags]
end
end
DECODE = {'TODO' => :todo, 'CURRENT' => :current, 'DONE' => :done,
'High' => :high, 'Normal' => :normal, 'Low' => :low}
class TodoList
include Enumerable
attr_accessor :tasks
def self.parse(text)
new_todo, new_todo.tasks = TodoList.new , []
new_todo.format_text(text).each do |x|
new_todo.tasks.push(Todo.new(x[0], x[1], x[2], x[3].split(', ')))
end
new_todo
end
def each
current = 0
while current < @tasks.size
yield @tasks[current]
current += 1
end
end
def format_text(text)
todos = text.each_line.to_a.map { |x| x.split("|",4) }
todos.each do |x|
x[0], x[1] = DECODE[x[0].strip], x[1].strip
x[2] , x[3] = DECODE[x[2].strip], x[3].strip
end
end
def filter(criteria)
new_todo, new_todo.tasks = TodoList.new , @tasks
new_todo.tasks = @tasks.select {|x| Criteria.pass? x.to_a, criteria.to_a}
new_todo
end
def adjoin(todo_list)
new_todo, new_todo.tasks = TodoList.new , @tasks
todo_list.tasks.each { |x| new_todo.tasks.push(x)}
new_todo
end
def tasks_todo
self.select { |x| x.status == :todo}.size
end
def tasks_in_progress
self.select { |x| x.status == :current}.size
end
def tasks_completed
self.select { |x| x.status == :done}.size
end
end
class TodoCriteria
attr_accessor :states, :priorities, :tags
def initialize(state, prior, tag)
@states = state
@priorities = prior
@tags = tag
end
def to_a
[@states] + [@priorities] + [@tags]
end
end
class Criteria
class << self
def status(state)
TodoCriteria.new state, nil, nil
end
def priority(priorty_type)
TodoCriteria.new nil, priorty_type, nil
end
def tags(tag)
TodoCriteria.new nil, nil, tag
end
def pass?(todo, crit)
value = true
value = crit[0] == todo[0] unless crit[0] == nil
value = crit[1] == todo[1] unless crit[1] == nil
value = (todo[3] & crit[2]).size == crit[2].size unless crit[2] == nil
end
end
end

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

FF...FFFFF.....F......

Failures:

  1) TodoList filters tasks by status
     Failure/Error: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Have some tea."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Have some tea."]
     # /tmp/d20131107-4393-1feew9v/spec.rb:22: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: ]
       expected collection contained:  ["Destroy Facebook and Google.", "Do the 5th Ruby challenge.", "Eat spaghetti.", "Grok Ruby.", "Occupy Sofia University."]
       actual collection contained:    []
       the missing elements were:      ["Destroy Facebook and Google.", "Do the 5th Ruby challenge.", "Eat spaghetti.", "Grok Ruby.", "Occupy Sofia University."]
     # /tmp/d20131107-4393-1feew9v/spec.rb:32: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 supports a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `&' for #<TodoCriteria:0xb95a6db0>
     # /tmp/d20131107-4393-1feew9v/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)>'

  4) TodoList supports a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NoMethodError:
       undefined method `|' for #<TodoCriteria:0xb95a4b28>
     # /tmp/d20131107-4393-1feew9v/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)>'

  5) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NoMethodError:
       undefined method `to_a' for false:FalseClass
     # /tmp/d20131107-4393-1feew9v/solution.rb:49:in `block in filter'
     # /tmp/d20131107-4393-1feew9v/solution.rb:49:in `select'
     # /tmp/d20131107-4393-1feew9v/solution.rb:49:in `filter'
     # /tmp/d20131107-4393-1feew9v/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)>'

  6) TodoList supports simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NoMethodError:
       undefined method `&' for #<TodoCriteria:0xb959bdac>
     # /tmp/d20131107-4393-1feew9v/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)>'

  7) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<TodoCriteria:0xb9599ad4>
     # /tmp/d20131107-4393-1feew9v/spec.rb:82: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 checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `completed?' for #<TodoList:0xb957aa6c>
     # /tmp/d20131107-4393-1feew9v/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.04592 seconds
22 examples, 8 failures

Failed examples:

rspec /tmp/d20131107-4393-1feew9v/spec.rb:18 # TodoList filters tasks by status
rspec /tmp/d20131107-4393-1feew9v/spec.rb:25 # TodoList filters tasks by priority
rspec /tmp/d20131107-4393-1feew9v/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-1feew9v/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-1feew9v/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-1feew9v/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-1feew9v/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-1feew9v/spec.rb:130 # TodoList checks if all tasks are completed

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

Петър обнови решението на 06.11.2013 16:45 (преди около 11 години)

+class Todo
+ attr_accessor :status, :description, :priority, :tags
+
+ def initialize(state, descr, prior, tag)
+ @status = state
+ @description = descr
+ @priority = prior
+ @tags = tag
+ end
+
+ def to_a
+ [@status] + [@description] + [@priority] + [@tags]
+ end
+end
+
+DECODE = {'TODO' => :todo, 'CURRENT' => :current, 'DONE' => :done,
+ 'High' => :high, 'Normal' => :normal, 'Low' => :low}
+
+class TodoList
+ include Enumerable
+ attr_accessor :tasks
+
+ def self.parse(text)
+ new_todo, new_todo.tasks = TodoList.new , []
+ new_todo.format_text(text).each do |x|
+ new_todo.tasks.push(Todo.new(x[0], x[1], x[2], x[3].split(', ')))
+ end
+ new_todo
+ end
+
+ def each
+ current = 0
+ while current < @tasks.size
+ yield @tasks[current]
+ current += 1
+ end
+ end
+
+ def format_text(text)
+ todos = text.each_line.to_a.map { |x| x.split("|",4) }
+ todos.each do |x|
+ x[0], x[1] = DECODE[x[0].strip], x[1].strip
+ x[2] , x[3] = DECODE[x[2].strip], x[3].strip
+ end
+ end
+
+ def filter(criteria)
+ new_todo, new_todo.tasks = TodoList.new , @tasks
+ new_todo.tasks = @tasks.select {|x| Criteria.pass? x.to_a, criteria.to_a}
+ new_todo
+ end
+
+ def adjoin(todo_list)
+ new_todo, new_todo.tasks = TodoList.new , @tasks
+ todo_list.tasks.each { |x| new_todo.tasks.push(x)}
+ new_todo
+ end
+
+ def tasks_todo
+ self.select { |x| x.status == :todo}.size
+ end
+
+ def tasks_in_progress
+ self.select { |x| x.status == :current}.size
+ end
+
+ def tasks_completed
+ self.select { |x| x.status == :done}.size
+ end
+end
+
+class TodoCriteria
+ attr_accessor :states, :priorities, :tags
+
+ def initialize(state, prior, tag)
+ @states = state
+ @priorities = prior
+ @tags = tag
+ end
+
+ def to_a
+ [@states] + [@priorities] + [@tags]
+ end
+
+end
+
+class Criteria
+ class << self
+ def status(state)
+ TodoCriteria.new state, nil, nil
+ end
+
+ def priority(priorty_type)
+ TodoCriteria.new nil, priorty_type, nil
+ end
+
+ def tags(tag)
+ TodoCriteria.new nil, nil, tag
+ end
+ def pass?(todo, crit)
+ value = true
+ value = crit[0] == todo[0] unless crit[0] == nil
+ value = crit[1] == todo[1] unless crit[1] == nil
+ value = (todo[3] & crit[2]).size == crit[2].size unless crit[2] == nil
+ end
+ end
+end