Решение на Втора задача от Илия Ватахов

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

Към профила на Илия Ватахов

Резултати

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

Код

class Task
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags = "")
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags.split(/\s*,\s*/)
end
def get_properties
[@status, @description, @priority, @tags]
end
end
class Criteria
def initialize(criteria)
@criteria = criteria
end
def get_criteria
@criteria
end
def Criteria.status(status)
Criteria.new -> (x) { x.status == status }
end
def Criteria.priority(priority)
Criteria.new -> (x) { x.priority == priority }
end
def Criteria.tags(tags)
Criteria.new -> (x) { tags - x.tags == [] && tags != [] || tags == x.tags }
end
def &(other)
Criteria.new -> (x) { @criteria.call(x) and other.get_criteria.call(x) }
end
def |(other)
Criteria.new -> (x) { @criteria.call(x) or other.get_criteria.call(x) }
end
def !
Criteria.new -> (x) { !@criteria.call x }
end
end
class TodoList
include Enumerable
attr_reader :tasks
def initialize(tasks)
@tasks = tasks.uniq { |x| x.get_properties }
end
def TodoList.parse(text)
tasks = text.split("\n").map { |properties| properties.split(/\s*\|\s*/) }
TodoList.new tasks.map { |x| Task.new *x }
end
def filter(criteria)
TodoList.new @tasks.select { |x| criteria.get_criteria.call(x)}
end
def adjoin(other)
TodoList.new @tasks + other.tasks
end
def tasks_todo
@tasks.select { |task| task.status == :todo}.count
end
def tasks_in_progress
@tasks.select { |task| task.status == :current}.count
end
def tasks_completed
@tasks.select { |task| task.status == :done}.count
end
def each
@tasks.each { |x| yield x }
end
end

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

F....FFF.F.FFFFF......

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-kfpsxy/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 supports a conjuction of filters
     Failure/Error: filtered.map(&:description).should =~ ['Eat spaghetti.', 'Destroy Facebook and Google.', 'Occupy Sofia University.']
       expected collection contained:  ["Destroy Facebook and Google.", "Eat spaghetti.", "Occupy Sofia University."]
       actual collection contained:    []
       the missing elements were:      ["Destroy Facebook and Google.", "Eat spaghetti.", "Occupy Sofia University."]
     # /tmp/d20131107-4393-kfpsxy/spec.rb:52: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: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Find missing socks.", "Get 8 hours of sleep.", "Have some tea."]
       actual collection contained:    ["Find missing socks.", "Get 8 hours of sleep."]
       the missing elements were:      ["Do the 5th Ruby challenge.", "Have some tea."]
     # /tmp/d20131107-4393-kfpsxy/spec.rb:62: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: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Grok Ruby.", "Have some tea.", "Party animal."]
       actual collection contained:    ["Destroy Facebook and Google.", "Do the 5th Ruby challenge.", "Eat spaghetti.", "Find missing socks.", "Get 8 hours of sleep.", "Grok Ruby.", "Have some tea.", "Occupy Sofia University.", "Party animal."]
       the extra elements were:        ["Destroy Facebook and Google.", "Eat spaghetti.", "Find missing socks.", "Get 8 hours of sleep.", "Occupy Sofia University."]
     # /tmp/d20131107-4393-kfpsxy/spec.rb:72: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 complex filters combination
     Failure/Error: ]
       expected collection contained:  ["Destroy Facebook and Google.", "Eat spaghetti.", "Have some tea.", "Occupy Sofia University.", "Party animal."]
       actual collection contained:    ["Have some tea.", "Party animal."]
       the missing elements were:      ["Destroy Facebook and Google.", "Eat spaghetti.", "Occupy Sofia University."]
     # /tmp/d20131107-4393-kfpsxy/spec.rb:92: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 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-kfpsxy/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)>'

  7) TodoList returns the number of the tasks todo
     Failure/Error: todo_list.tasks_todo.should eq 5
       
       expected: 5
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-kfpsxy/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)>'

  8) TodoList returns the number of the tasks in progress
     Failure/Error: todo_list.tasks_in_progress.should eq 2
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-kfpsxy/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)>'

  9) TodoList returns the number of the completed tasks
     Failure/Error: todo_list.tasks_completed.should eq 2
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-kfpsxy/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)>'

  10) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `completed?' for #<TodoList:0xb93aab88>
     # /tmp/d20131107-4393-kfpsxy/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.04445 seconds
22 examples, 10 failures

Failed examples:

rspec /tmp/d20131107-4393-kfpsxy/spec.rb:18 # TodoList filters tasks by status
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:118 # TodoList returns the number of the tasks todo
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:122 # TodoList returns the number of the tasks in progress
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:126 # TodoList returns the number of the completed tasks
rspec /tmp/d20131107-4393-kfpsxy/spec.rb:130 # TodoList checks if all tasks are completed

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

Илия обнови решението на 06.11.2013 00:05 (преди над 10 години)

+class Task
+ attr_reader :status, :description, :priority, :tags
+
+ def initialize(status, description, priority, tags = "")
+ @status = status.downcase.to_sym
+ @description = description
+ @priority = priority.downcase.to_sym
+ @tags = tags.split(/\s*,\s*/)
+ end
+end
+
+class Criteria
+ def initialize(criteria)
+ @criteria = criteria
+ end
+
+ def get_criteria
+ @criteria
+ end
+
+ def Criteria.status(status)
+ Criteria.new -> (x) { x.status == status }
+ end
+
+ def Criteria.priority(priority)
+ Criteria.new -> (x) { x.priority == priority }
+ end
+
+ def Criteria.tags(tags)
+ Criteria.new -> (x) { tags - x.tags == [] }
+ end
+
+ def &(other)
+ Criteria.new -> (x) { @criteria.call(x) and other.get_criteria.call(x) }
+ end
+
+ def |(other)
+ Criteria.new -> (x) { @criteria.call(x) or other.get_criteria.call(x) }
+ end
+
+ def !
+ Criteria.new -> (x) { !@criteria.call x }
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_reader :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def TodoList.parse(text)
+ tasks = text.split("\n").map { |properties| properties.split(/\s*\|\s*/) }
+ TodoList.new tasks.map { |x| Task.new *x }
+ end
+
+ def filter(criteria)
+ TodoList.new @tasks.select { |x| criteria.get_criteria.call(x)}
+ end
+
+ def adjoin(other)
+ TodoList.new @tasks + other.tasks
+ end
+
+ def tasks_todo
+ @tasks.select { |task| task.status == :todo}.count
+ end
+
+ def tasks_in_progress
+ @tasks.select { |task| task.status == :current}.count
+ end
+
+ def tasks_completed
+ @tasks.select { |task| task.status == :done}.count
+ end
+
+ def each
+ @tasks.each { |x| yield x }
+ end
+end

Илия обнови решението на 06.11.2013 09:50 (преди над 10 години)

class Task
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags = "")
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags.split(/\s*,\s*/)
end
end
class Criteria
def initialize(criteria)
@criteria = criteria
end
def get_criteria
@criteria
end
- def Criteria.status(status)
+ def Criteria.status(status = nil)
Criteria.new -> (x) { x.status == status }
end
- def Criteria.priority(priority)
+ def Criteria.priority(priority = nil)
Criteria.new -> (x) { x.priority == priority }
end
- def Criteria.tags(tags)
+ def Criteria.tags(tags = nil)
Criteria.new -> (x) { tags - x.tags == [] }
end
def &(other)
Criteria.new -> (x) { @criteria.call(x) and other.get_criteria.call(x) }
end
def |(other)
Criteria.new -> (x) { @criteria.call(x) or other.get_criteria.call(x) }
end
def !
Criteria.new -> (x) { !@criteria.call x }
end
end
class TodoList
include Enumerable
attr_reader :tasks
def initialize(tasks)
@tasks = tasks
end
def TodoList.parse(text)
tasks = text.split("\n").map { |properties| properties.split(/\s*\|\s*/) }
TodoList.new tasks.map { |x| Task.new *x }
end
def filter(criteria)
TodoList.new @tasks.select { |x| criteria.get_criteria.call(x)}
end
def adjoin(other)
TodoList.new @tasks + other.tasks
end
def tasks_todo
@tasks.select { |task| task.status == :todo}.count
end
def tasks_in_progress
@tasks.select { |task| task.status == :current}.count
end
def tasks_completed
@tasks.select { |task| task.status == :done}.count
end
def each
@tasks.each { |x| yield x }
end
end

Илия обнови решението на 06.11.2013 10:29 (преди над 10 години)

class Task
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags = "")
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags.split(/\s*,\s*/)
end
end
class Criteria
def initialize(criteria)
@criteria = criteria
end
def get_criteria
@criteria
end
- def Criteria.status(status = nil)
+ def Criteria.status(status)
Criteria.new -> (x) { x.status == status }
end
- def Criteria.priority(priority = nil)
+ def Criteria.priority(priority)
Criteria.new -> (x) { x.priority == priority }
end
- def Criteria.tags(tags = nil)
- Criteria.new -> (x) { tags - x.tags == [] }
+ def Criteria.tags(tags)
+ Criteria.new -> (x) { tags - x.tags == [] && tags != [] || tags == x.tags }
end
def &(other)
Criteria.new -> (x) { @criteria.call(x) and other.get_criteria.call(x) }
end
def |(other)
Criteria.new -> (x) { @criteria.call(x) or other.get_criteria.call(x) }
end
def !
Criteria.new -> (x) { !@criteria.call x }
end
end
class TodoList
include Enumerable
attr_reader :tasks
def initialize(tasks)
@tasks = tasks
end
def TodoList.parse(text)
tasks = text.split("\n").map { |properties| properties.split(/\s*\|\s*/) }
TodoList.new tasks.map { |x| Task.new *x }
end
def filter(criteria)
TodoList.new @tasks.select { |x| criteria.get_criteria.call(x)}
end
def adjoin(other)
TodoList.new @tasks + other.tasks
end
def tasks_todo
@tasks.select { |task| task.status == :todo}.count
end
def tasks_in_progress
@tasks.select { |task| task.status == :current}.count
end
def tasks_completed
@tasks.select { |task| task.status == :done}.count
end
def each
@tasks.each { |x| yield x }
end
end

Илия обнови решението на 06.11.2013 10:49 (преди над 10 години)

class Task
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags = "")
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags.split(/\s*,\s*/)
end
+
+ def get_properties
+ [@status, @description, @priority, @tags]
+ end
end
class Criteria
def initialize(criteria)
@criteria = criteria
end
def get_criteria
@criteria
end
def Criteria.status(status)
Criteria.new -> (x) { x.status == status }
end
def Criteria.priority(priority)
Criteria.new -> (x) { x.priority == priority }
end
def Criteria.tags(tags)
Criteria.new -> (x) { tags - x.tags == [] && tags != [] || tags == x.tags }
end
def &(other)
Criteria.new -> (x) { @criteria.call(x) and other.get_criteria.call(x) }
end
def |(other)
Criteria.new -> (x) { @criteria.call(x) or other.get_criteria.call(x) }
end
def !
Criteria.new -> (x) { !@criteria.call x }
end
end
class TodoList
include Enumerable
attr_reader :tasks
def initialize(tasks)
- @tasks = tasks
+ @tasks = tasks.uniq { |x| x.get_properties }
end
def TodoList.parse(text)
tasks = text.split("\n").map { |properties| properties.split(/\s*\|\s*/) }
TodoList.new tasks.map { |x| Task.new *x }
end
def filter(criteria)
TodoList.new @tasks.select { |x| criteria.get_criteria.call(x)}
end
def adjoin(other)
TodoList.new @tasks + other.tasks
end
def tasks_todo
@tasks.select { |task| task.status == :todo}.count
end
def tasks_in_progress
@tasks.select { |task| task.status == :current}.count
end
def tasks_completed
@tasks.select { |task| task.status == :done}.count
end
def each
@tasks.each { |x| yield x }
end
end

Илия обнови решението на 06.11.2013 10:51 (преди над 10 години)

class Task
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags = "")
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags.split(/\s*,\s*/)
end
def get_properties
[@status, @description, @priority, @tags]
end
end
class Criteria
def initialize(criteria)
@criteria = criteria
end
def get_criteria
@criteria
end
def Criteria.status(status)
Criteria.new -> (x) { x.status == status }
end
def Criteria.priority(priority)
Criteria.new -> (x) { x.priority == priority }
end
def Criteria.tags(tags)
Criteria.new -> (x) { tags - x.tags == [] && tags != [] || tags == x.tags }
end
def &(other)
Criteria.new -> (x) { @criteria.call(x) and other.get_criteria.call(x) }
end
def |(other)
Criteria.new -> (x) { @criteria.call(x) or other.get_criteria.call(x) }
end
def !
Criteria.new -> (x) { !@criteria.call x }
end
end
class TodoList
- include Enumerable
+ include Enumerable
attr_reader :tasks
def initialize(tasks)
@tasks = tasks.uniq { |x| x.get_properties }
end
def TodoList.parse(text)
tasks = text.split("\n").map { |properties| properties.split(/\s*\|\s*/) }
TodoList.new tasks.map { |x| Task.new *x }
end
def filter(criteria)
TodoList.new @tasks.select { |x| criteria.get_criteria.call(x)}
end
def adjoin(other)
TodoList.new @tasks + other.tasks
end
def tasks_todo
@tasks.select { |task| task.status == :todo}.count
end
def tasks_in_progress
@tasks.select { |task| task.status == :current}.count
end
def tasks_completed
@tasks.select { |task| task.status == :done}.count
end
def each
@tasks.each { |x| yield x }
end
end