Решение на Втора задача от Росен Рачев

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

Към профила на Росен Рачев

Резултати

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

Код

module Parser
def self.format(list)
list.map do |item|
item[0] = item[0].downcase.to_sym
item[2] = item[2].downcase.to_sym
item[3] = item[3].gsub(/, +/, ',').strip.split(",")
end
end
def self.create_tasks(list)
format(list)
tasks = []
1.upto(list.length).each { tasks << Task.new }
tasks.each_with_index { |item, index| item.task = list[index] }
end
end
module Filter
def filter(norm, filtered = [])
@list.each_with_object(norm.first) do |task, crit|
filtered << Filter.filtered_task(task, crit)
end
filtered = TodoList.new(filtered.uniq.compact)
end
def self.filtered_task(task, crit)
if Filter.contains? task.task, crit
task
end
end
def self.contains?(task, other)
return task.flatten.include? other if not other.kind_of? Array
return true if other.empty?
return contains? task, other.drop(1) if contains? task, other.first
false
end
end
module Statistics
def tasks_todo
reduce(0) { |memo, item| memo += item.count(:todo) }
end
def tasks_in_progress
reduce(0) { |memo, item| memo += item.count(:current) }
end
def tasks_completed
reduce(0) { |memo, item| memo += item.count(:done) }
end
def completed?
completed = true
each { |item| completed = item.task[0] == :done }
completed
end
end
class TodoList
include Enumerable
include Filter
include Statistics
attr_accessor :list
def initialize(value = [])
@list = value
end
def each
@list.each { |item| yield item }
end
def self.parse(text)
list = []
text.each_line { |line| list << line.gsub(/[ ]*[|][ ]*/, '|').split("|") }
TodoList.new(Parser.create_tasks(list))
end
def adjoin(other)
TodoList.new((@list + other.list).uniq { |item| item.task[1] })
end
end
class Criteria
include Enumerable
attr_accessor :criteria
def initialize(value)
@criteria = value
end
def self.status(value)
Criteria.new([value]) if [:todo, :current, :done].include? value
end
def self.priority(value)
Criteria.new([value]) if [:low, :normal, :high].include? value
end
def self.tags(value)
Criteria.new([value])
end
def |(other)
Criteria.new([@criteria] + [other.criteria])
end
def &(other)
Criteria.new([@criteria + other.criteria])
end
def !
if Filter.contains? [:todo, :current, :done], @criteria
return Criteria.new(([:todo, :current, :done] - @criteria).product)
end
if Filter.contains? [:low, :normal, :high], @criteria
return Criteria.new(([:low, :normal, :high] - @criteria).product)
end
end
def each
@criteria.each { |item| yield item }
end
end
class Task
include Enumerable
attr_accessor :task
def each
@task.each { |item| yield item }
end
def status
task[0]
end
def description
task[1]
end
def priority
task[2]
end
def tags
task[3]
end
end

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

F....FFFFF.FFFF.......

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-160bbe6/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-160bbe6/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:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Find missing socks.", "Get 8 hours of sleep.", "Have some tea."]
     # /tmp/d20131107-4393-160bbe6/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:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Grok Ruby.", "Have some tea.", "Party animal."]
     # /tmp/d20131107-4393-160bbe6/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 simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NoMethodError:
       undefined method `criteria' for nil:NilClass
     # /tmp/d20131107-4393-160bbe6/solution.rb:110:in `&'
     # /tmp/d20131107-4393-160bbe6/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.tags(['development'])
     NoMethodError:
       undefined method `criteria' for nil:NilClass
     # /tmp/d20131107-4393-160bbe6/solution.rb:110:in `&'
     # /tmp/d20131107-4393-160bbe6/spec.rb:84: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 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-160bbe6/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)>'

  8) 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-160bbe6/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)>'

  9) 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-160bbe6/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)>'

  10) 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-160bbe6/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)>'

Finished in 0.04618 seconds
22 examples, 10 failures

Failed examples:

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

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

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

+module Parser
+ def self.format(list)
+ list.map do |item|
+ item[0] = item[0].downcase.to_sym
+ item[2] = item[2].downcase.to_sym
+ item[3] = item[3].gsub(/, +/, ',').strip.split(",")
+ end
+ end
+
+ def self.create_tasks(list)
+ format(list)
+ tasks = []
+ 1.upto(list.length).each { tasks << Task.new }
+ tasks.each_with_index { |item, index| item.task = list[index] }
+ end
+end
+
+module Filter
+ def filter(norm)
+ # filter = []
+ # @list.each do |task|
+ # norm.each { |crit| filter << task if Filter.contains? task.task, crit }
+ # end
+ # TodoList.new(filter.uniq)
+ end
+
+ def self.contains?(task, other)
+ return true if other.empty?
+ return task.flatten.include? other if not other.instance_of? Array
+ return contains? task, other.drop(1) if task.flatten.include? other.first
+ false
+ end
+end
+
+module Statistics
+ def tasks_todo
+ count = 0
+ each { |item| count += item.count(:todo) }
+ count
+ end
+
+ def tasks_in_progress
+ count = 0
+ each { |item| count += item.count(:current) }
+ count
+ end
+
+ def tasks_completed
+ count = 0
+ each { |item| count += item.count(:done) }
+ count
+ end
+
+ def completed?
+ completed = true
+ each { |item| completed = item.task[0] == :done }
+ completed
+ end
+end
+
+class TodoList
+ include Enumerable
+ include Filter
+ include Statistics
+ attr_accessor :list
+
+ def initialize(value = [])
+ @list = value
+ end
+
+ def each
+ @list.each { |item| yield item }
+ end
+
+ def self.parse(text)
+ list = []
+ text.each_line { |line| list << line.gsub(/[ ]*[|][ ]*/, '|').split("|") }
+ TodoList.new(Parser.create_tasks(list))
+ end
+
+ def adjoin(other)
+ TodoList.new((@list + other.list).uniq { |item| item.task[1] })
+ end
+end
+
+class Criteria
+ include Enumerable
+ attr_accessor :criteria
+
+ def initialize(value)
+ @criteria = value
+ end
+
+ def Criteria.status(value)
+ Criteria.new([value]) if [:todo, :current, :done].include? value
+ end
+
+ def Criteria.priority(value)
+ Criteria.new([value]) if [:low, :normal, :high].include? value
+ end
+
+ def Criteria.tags(value)
+ Criteria.new(value)
+ end
+
+ def |(other)
+ Criteria.new([@criteria] + [other.criteria])
+ end
+
+ def &(other)
+ Criteria.new([@criteria + other.criteria])
+ end
+
+ def !
+ puts "wow such ! much filter so criteria"
+ end
+
+ def each
+ @criteria.each { |item| yield item }
+ end
+end
+
+class Task
+ include Enumerable
+ attr_accessor :task
+
+ def each
+ @task.each { |item| yield item }
+ end
+
+ def status
+ task[0]
+ end
+
+ def description
+ task[1]
+ end
+
+ def priority
+ task[2]
+ end
+
+ def tags
+ task[3]
+ end
+end

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

module Parser
def self.format(list)
list.map do |item|
item[0] = item[0].downcase.to_sym
item[2] = item[2].downcase.to_sym
item[3] = item[3].gsub(/, +/, ',').strip.split(",")
end
end
def self.create_tasks(list)
format(list)
tasks = []
1.upto(list.length).each { tasks << Task.new }
tasks.each_with_index { |item, index| item.task = list[index] }
end
end
module Filter
def filter(norm)
# filter = []
# @list.each do |task|
# norm.each { |crit| filter << task if Filter.contains? task.task, crit }
# end
# TodoList.new(filter.uniq)
end
def self.contains?(task, other)
return true if other.empty?
return task.flatten.include? other if not other.instance_of? Array
return contains? task, other.drop(1) if task.flatten.include? other.first
false
end
end
module Statistics
def tasks_todo
- count = 0
- each { |item| count += item.count(:todo) }
- count
+ reduce(0) { |memo, item| memo += item.count(:todo) }
end
def tasks_in_progress
- count = 0
- each { |item| count += item.count(:current) }
- count
+ reduce(0) { |memo, item| memo += item.count(:current) }
end
def tasks_completed
- count = 0
- each { |item| count += item.count(:done) }
- count
+ reduce(0) { |memo, item| memo += item.count(:done) }
end
def completed?
completed = true
each { |item| completed = item.task[0] == :done }
completed
end
end
class TodoList
include Enumerable
include Filter
include Statistics
attr_accessor :list
def initialize(value = [])
@list = value
end
def each
@list.each { |item| yield item }
end
def self.parse(text)
list = []
text.each_line { |line| list << line.gsub(/[ ]*[|][ ]*/, '|').split("|") }
TodoList.new(Parser.create_tasks(list))
end
def adjoin(other)
TodoList.new((@list + other.list).uniq { |item| item.task[1] })
end
end
class Criteria
include Enumerable
attr_accessor :criteria
def initialize(value)
@criteria = value
end
def Criteria.status(value)
Criteria.new([value]) if [:todo, :current, :done].include? value
end
def Criteria.priority(value)
Criteria.new([value]) if [:low, :normal, :high].include? value
end
def Criteria.tags(value)
Criteria.new(value)
end
def |(other)
Criteria.new([@criteria] + [other.criteria])
end
def &(other)
Criteria.new([@criteria + other.criteria])
end
def !
- puts "wow such ! much filter so criteria"
+ puts "Work in progress..."
end
def each
@criteria.each { |item| yield item }
end
end
class Task
include Enumerable
attr_accessor :task
def each
@task.each { |item| yield item }
end
def status
task[0]
end
def description
task[1]
end
def priority
task[2]
end
def tags
task[3]
end
end

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

module Parser
def self.format(list)
list.map do |item|
item[0] = item[0].downcase.to_sym
item[2] = item[2].downcase.to_sym
item[3] = item[3].gsub(/, +/, ',').strip.split(",")
end
end
def self.create_tasks(list)
format(list)
tasks = []
1.upto(list.length).each { tasks << Task.new }
tasks.each_with_index { |item, index| item.task = list[index] }
end
end
module Filter
def filter(norm)
# filter = []
# @list.each do |task|
# norm.each { |crit| filter << task if Filter.contains? task.task, crit }
# end
# TodoList.new(filter.uniq)
end
def self.contains?(task, other)
return true if other.empty?
return task.flatten.include? other if not other.instance_of? Array
return contains? task, other.drop(1) if task.flatten.include? other.first
false
end
end
module Statistics
def tasks_todo
reduce(0) { |memo, item| memo += item.count(:todo) }
end
def tasks_in_progress
reduce(0) { |memo, item| memo += item.count(:current) }
end
def tasks_completed
reduce(0) { |memo, item| memo += item.count(:done) }
end
def completed?
completed = true
each { |item| completed = item.task[0] == :done }
completed
end
end
class TodoList
include Enumerable
include Filter
include Statistics
attr_accessor :list
def initialize(value = [])
@list = value
end
def each
@list.each { |item| yield item }
end
def self.parse(text)
list = []
text.each_line { |line| list << line.gsub(/[ ]*[|][ ]*/, '|').split("|") }
TodoList.new(Parser.create_tasks(list))
end
def adjoin(other)
TodoList.new((@list + other.list).uniq { |item| item.task[1] })
end
end
class Criteria
include Enumerable
attr_accessor :criteria
def initialize(value)
@criteria = value
end
- def Criteria.status(value)
+ def self.status(value)
Criteria.new([value]) if [:todo, :current, :done].include? value
end
- def Criteria.priority(value)
+ def self.priority(value)
Criteria.new([value]) if [:low, :normal, :high].include? value
end
- def Criteria.tags(value)
+ def self.tags(value)
Criteria.new(value)
end
def |(other)
Criteria.new([@criteria] + [other.criteria])
end
def &(other)
Criteria.new([@criteria + other.criteria])
end
def !
puts "Work in progress..."
end
def each
@criteria.each { |item| yield item }
end
end
class Task
include Enumerable
attr_accessor :task
def each
@task.each { |item| yield item }
end
def status
task[0]
end
def description
task[1]
end
def priority
task[2]
end
def tags
task[3]
end
end

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

module Parser
def self.format(list)
list.map do |item|
item[0] = item[0].downcase.to_sym
item[2] = item[2].downcase.to_sym
item[3] = item[3].gsub(/, +/, ',').strip.split(",")
end
end
def self.create_tasks(list)
format(list)
tasks = []
1.upto(list.length).each { tasks << Task.new }
tasks.each_with_index { |item, index| item.task = list[index] }
end
end
module Filter
- def filter(norm)
- # filter = []
- # @list.each do |task|
- # norm.each { |crit| filter << task if Filter.contains? task.task, crit }
- # end
- # TodoList.new(filter.uniq)
+ def filter(norm, filtered = [])
+ @list.each_with_object(norm.first) do |task, crit|
+ filtered << Filter.filtered_task(task, crit)
+ end
+ filtered = TodoList.new(filtered.uniq.compact)
end
+ def self.filtered_task(task, crit)
+ if Filter.contains? task.task, crit
+ task
+ end
+ end
+
def self.contains?(task, other)
+ return task.flatten.include? other if not other.kind_of? Array
return true if other.empty?
- return task.flatten.include? other if not other.instance_of? Array
- return contains? task, other.drop(1) if task.flatten.include? other.first
+ return contains? task, other.drop(1) if contains? task, other.first
false
end
end
module Statistics
def tasks_todo
reduce(0) { |memo, item| memo += item.count(:todo) }
end
def tasks_in_progress
reduce(0) { |memo, item| memo += item.count(:current) }
end
def tasks_completed
reduce(0) { |memo, item| memo += item.count(:done) }
end
def completed?
completed = true
each { |item| completed = item.task[0] == :done }
completed
end
end
class TodoList
include Enumerable
include Filter
include Statistics
attr_accessor :list
def initialize(value = [])
@list = value
end
def each
@list.each { |item| yield item }
end
def self.parse(text)
list = []
text.each_line { |line| list << line.gsub(/[ ]*[|][ ]*/, '|').split("|") }
TodoList.new(Parser.create_tasks(list))
end
def adjoin(other)
TodoList.new((@list + other.list).uniq { |item| item.task[1] })
end
end
class Criteria
include Enumerable
attr_accessor :criteria
def initialize(value)
@criteria = value
end
def self.status(value)
Criteria.new([value]) if [:todo, :current, :done].include? value
end
def self.priority(value)
Criteria.new([value]) if [:low, :normal, :high].include? value
end
def self.tags(value)
- Criteria.new(value)
+ Criteria.new([value])
end
def |(other)
Criteria.new([@criteria] + [other.criteria])
end
def &(other)
Criteria.new([@criteria + other.criteria])
end
def !
- puts "Work in progress..."
+ if Filter.contains? [:todo, :current, :done], @criteria
+ return Criteria.new(([:todo, :current, :done] - @criteria).product)
+ end
+ if Filter.contains? [:low, :normal, :high], @criteria
+ return Criteria.new(([:low, :normal, :high] - @criteria).product)
+ end
end
def each
@criteria.each { |item| yield item }
end
end
class Task
include Enumerable
attr_accessor :task
def each
@task.each { |item| yield item }
end
def status
task[0]
end
def description
task[1]
end
def priority
task[2]
end
def tags
task[3]
end
end