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

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

Към профила на Александър Антов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 10 успешни тест(а)
  • 12 неуспешни тест(а)

Код

class TodoItem
attr_accessor :status
attr_accessor :description
attr_accessor :priority
attr_accessor :categories
def initialize(arr)
self.status, self.description = [arr[0].strip!, arr[1].strip!]
self.priority = arr[2].strip!
self.categories = if arr[3].nil? then [] else arr[3].split(",") end
self.categories.map! { |iter| iter.strip! }
end
end
class Criteria
def self.status(status_param)
return lambda { |item| item.status.casecmp(status_param.to_s) == 0 }
end
def self.priority(priority_param)
return lambda { |item| item.priority.casecmp(priority_param.to_s) == 0 }
end
def self.tags(tags_param)
return lambda { |item| (tags_param & item.categories).count > 0 }
end
end
class TodoList
include Enumerable
attr_accessor :enum
def initialize
self.enum = Array.new
end
def each(&block)
enum.each(&block)
end
def self.parse(text)
todo_result = TodoList.new
text.split("\n").each do |outer_iter|
todo_result.enum.push(TodoItem.new(outer_iter.split("|")))
end
return todo_result
end
def filter(criteria)
enum.select!(&criteria)
return self
end
def tasks_todo
return enum.count{ |iter| iter.status.casecmp("todo") == 0 }
end
def tasks_in_progress
return enum.count{ |iter| iter.status.casecmp("current") == 0 }
end
def tasks_completed
return enum.count{ |iter| iter.status.casecmp("done") == 0 }
end
def completed?
return tasks_completed == self.enum.count
end
def adjoin(other_list)
other_list.enum.concat(self.enum)
return other_list
end
end

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

....FFFFFFFF....F.FF.F

Failures:

  1) 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.']
       expected collection contained:  ["Do the 5th Ruby challenge."]
       actual collection contained:    ["Do the 5th Ruby challenge.", "Grok Ruby."]
       the extra elements were:        ["Grok Ruby."]
     # /tmp/d20131107-4393-1avjb79/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)>'

  2) TodoList supports a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `&' for #<Proc:0xb9706ac0>
     # /tmp/d20131107-4393-1avjb79/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)>'

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

  4) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     TypeError:
       wrong argument type FalseClass (expected Proc)
     # /tmp/d20131107-4393-1avjb79/solution.rb:51:in `filter'
     # /tmp/d20131107-4393-1avjb79/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)>'

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

  6) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<Proc:0xb9769ea4>
     # /tmp/d20131107-4393-1avjb79/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)>'

  7) TodoList can be adjoined with another to-do list
     Failure/Error: adjoined.count.should eq 3
       
       expected: 3
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-1avjb79/spec.rb:101: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 constructs an object for each task
     Failure/Error: task.status.should      eq :todo
       
       expected: :todo
            got: "TODO"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:todo
       +"TODO"
     # /tmp/d20131107-4393-1avjb79/spec.rb:112: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 doesn't modify the to-do list when filtering
     Failure/Error: todo_list.should have(text_input.lines.count).items
       expected 9 items, got 5
     # /tmp/d20131107-4393-1avjb79/spec.rb:137: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 contains tasks with the neccessary interface
     Failure/Error: task.should respond_to :tags
       expected #<TodoItem:0xb95cb994 @status="TODO", @description="Eat spaghetti.", @priority="High", @categories=["food", "happiness"]> to respond to :tags
     # /tmp/d20131107-4393-1avjb79/spec.rb:151: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 tasks have an array of tags
     Failure/Error: todo_list.first.tags.should be_an Array
     NoMethodError:
       undefined method `tags' for #<TodoItem:0xb95c9e28>
     # /tmp/d20131107-4393-1avjb79/spec.rb:155: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 creates a new object on filter
     Failure/Error: todo_list.filter(Criteria.tags %w[wtf]).should_not equal todo_list
       
       expected not #<TodoList:-592570538> => #<TodoList:0xb95c2eac @enum=[]>
                got #<TodoList:-592570538> => #<TodoList:0xb95c2eac @enum=[]>
       
       Compared using equal?, which compares object identity.
       
       
       Diff:
     # /tmp/d20131107-4393-1avjb79/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.0387 seconds
22 examples, 12 failures

Failed examples:

rspec /tmp/d20131107-4393-1avjb79/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-1avjb79/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-1avjb79/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-1avjb79/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-1avjb79/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-1avjb79/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-1avjb79/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-1avjb79/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-1avjb79/spec.rb:135 # TodoList doesn't modify the to-do list when filtering
rspec /tmp/d20131107-4393-1avjb79/spec.rb:145 # TodoList contains tasks with the neccessary interface
rspec /tmp/d20131107-4393-1avjb79/spec.rb:154 # TodoList tasks have an array of tags
rspec /tmp/d20131107-4393-1avjb79/spec.rb:172 # TodoList creates a new object on filter

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

Александър обнови решението на 06.11.2013 00:37 (преди около 11 години)

+class TodoItem
+ attr_accessor :status
+ attr_accessor :description
+ attr_accessor :priority
+ attr_accessor :categories
+
+ def initialize(arr)
+ self.status, self.description = [arr[0].strip!, arr[1].strip!]
+ self.priority = arr[2].strip!
+ self.categories = if arr[3].nil? then [] else arr[3].split(",") end
+ self.categories.map! { |iter| iter.strip! }
+ end
+end
+
+class Criteria
+ def self.status(status_param)
+ return lambda { |item| item.status.casecmp(status_param.to_s) == 0 }
+ end
+
+ def self.priority(priority_param)
+ return lambda { |item| item.priority.casecmp(priority_param.to_s) == 0 }
+ end
+
+ def self.tags(tags_param)
+ return lambda { |item| (tags_param & item.categories).count > 0 }
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_accessor :enum
+
+ def initialize
+ self.enum = Array.new
+ end
+
+ def each(&block)
+ enum.each(&block)
+ end
+
+ def self.parse(text)
+ todo_result = TodoList.new
+ text.split("\n").each do |outer_iter|
+ todo_result.enum.push(TodoItem.new(outer_iter.split("|")))
+ end
+ return todo_result
+ end
+
+ def filter(criteria)
+ enum.select!(&criteria)
+ return self
+ end
+
+ def tasks_todo
+ return enum.count{ |iter| iter.status.casecmp("todo") == 0 }
+ end
+
+ def tasks_in_progress
+ return enum.count{ |iter| iter.status.casecmp("current") == 0 }
+ end
+
+ def tasks_completed
+ return enum.count{ |iter| iter.status.casecmp("done") == 0 }
+ end
+
+ def completed?
+ return tasks_completed == self.enum.count
+ end
+
+ def adjoin(other_list)
+ other_list.enum.concat(self.enum)
+ return other_list
+ end
+end