Решение на Втора задача от Методи Димитров

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

Към профила на Методи Димитров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 22 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Criteria
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
Criteria.new(->(task) { task.status == status })
end
def Criteria.priority(priority)
Criteria.new(->(task) { task.priority == priority })
end
def Criteria.tags(tags)
Criteria.new(->(task) { task.has_all_tags? tags })
end
def &(criteria)
Criteria.new(->(task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
Criteria.new(->(task) { criteria.block.call(task) or @block.call(task) })
end
def !()
Criteria.new(->(task) { not @block.call(task) })
end
end
class Task
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags = [])
@status = status.strip.downcase.to_sym
@description = description.strip
@priority = priority.strip.downcase.to_sym
@tags = tags.split(",").map(&:strip)
end
def has_all_tags?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
[@status, @description, @priority, @tags] <=> task_as_list
end
end
class TodoList
include Enumerable
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
TodoList.new(@todo_list.select { |task| criterias.block.call task })
end
def adjoin(todo_list_object)
TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
def tasks_todo
filter(Criteria.status(:todo)).todo_list.length
end
def tasks_in_progress
filter(Criteria.status(:current)).todo_list.length
end
def tasks_completed
filter(Criteria.status(:done)).todo_list.length
end
def completed?
@todo_list.all? { |task| task.status == :done }
end
end

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

......................

Finished in 0.03983 seconds
22 examples, 0 failures

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

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

+module Criteria
+ def status(status)
+ -> (task) { task.status == status }
+ end
+
+ def priority(priority)
+ -> (task) { task.priority == priority }
+ end
+
+ def tags(tags)
+ -> (task) { task.has_them_all? tags }
+ end
+
+ def &(criteria)
+ -> (task) { criteria.call(task) and call(task) }
+ end
+
+ def |(criteria)
+ -> (task) { criteria.call(task) or call(task) }
+ end
+
+ def !()
+ -> (task) { not call(task) }
+ end
+
+end
+
+class Hash
+ def status
+ self[:status].strip.downcase.to_sym
+ end
+
+ def description
+ self[:description].strip
+ end
+
+ def priority
+ self[:priority].strip.downcase.to_sym
+ end
+
+ def tags
+ if self[:tags] then
+ self[:tags].scan(/[^,]+/).map(&:strip)
+ else
+ []
+ end
+ end
+
+ def has_them_all?(tags)
+ tags.all? { |tag| self.tags.include? tag }
+ end
+
+end
+
+module TodoList
+ include Criteria
+
+ def parse(raw_list)
+ list_fields = [:status, :description, :priority, :tags]
+ raw_list.scan(/[^\n]+/).map do |task|
+ Hash[list_fields.zip task.scan(/[^\|]+/)]
+ end
+ end
+
+ def filter(criterias)
+ select { |task| criterias.call task}
+ end
+
+ def adjoin(list)
+ self + list
+ end
+
+ def tasks_todo
+ filter Criteria.status(:todo)
+ end
+
+ def tasks_in_progress
+ filter Criteria.status(:current)
+ end
+
+ def tasks_completed
+ filter(Criteria.status(:done)).length
+ end
+
+ def completed?
+ all? { |task| task.status == :done }
+ end
+
+end
+include TodoList

Методи обнови решението на 05.11.2013 10:51 (преди около 11 години)

module Criteria
def status(status)
-> (task) { task.status == status }
end
def priority(priority)
-> (task) { task.priority == priority }
end
def tags(tags)
-> (task) { task.has_them_all? tags }
end
def &(criteria)
-> (task) { criteria.call(task) and call(task) }
end
def |(criteria)
-> (task) { criteria.call(task) or call(task) }
end
def !()
-> (task) { not call(task) }
end
end
class Hash
def status
self[:status].strip.downcase.to_sym
end
def description
self[:description].strip
end
def priority
self[:priority].strip.downcase.to_sym
end
def tags
if self[:tags] then
self[:tags].scan(/[^,]+/).map(&:strip)
else
[]
end
end
def has_them_all?(tags)
tags.all? { |tag| self.tags.include? tag }
end
end
module TodoList
include Criteria
def parse(raw_list)
list_fields = [:status, :description, :priority, :tags]
raw_list.scan(/[^\n]+/).map do |task|
Hash[list_fields.zip task.scan(/[^\|]+/)]
end
end
def filter(criterias)
select { |task| criterias.call task}
end
def adjoin(list)
self + list
end
def tasks_todo
- filter Criteria.status(:todo)
+ filter(Criteria.status(:todo)).length
end
def tasks_in_progress
- filter Criteria.status(:current)
+ filter(Criteria.status(:current)).length
end
def tasks_completed
filter(Criteria.status(:done)).length
end
def completed?
all? { |task| task.status == :done }
end
end
include TodoList

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

-module Criteria
- def status(status)
- -> (task) { task.status == status }
+class Criteria
+ attr_reader :block
+
+ def initialize(block)
+ @block = block
end
- def priority(priority)
- -> (task) { task.priority == priority }
+ def Criteria.status(status)
+ Criteria.new(-> (task) { task.status == status })
+
end
- def tags(tags)
- -> (task) { task.has_them_all? tags }
+ def Criteria.priority(priority)
+ Criteria.new(-> (task) { task.priority == priority })
end
+ def Criteria.tags(tags)
+ Criteria.new(-> (task) { task.has_them_all? tags })
+ end
+
def &(criteria)
- -> (task) { criteria.call(task) and call(task) }
+ Criteria.new(-> (task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
- -> (task) { criteria.call(task) or call(task) }
+ Criteria.new(-> (task) { criteria.block.call(task) or @block.call(task) })
end
def !()
- -> (task) { not call(task) }
+ Criteria.new(-> (task) { not @block.call(task) })
end
end
-class Hash
+class Task
+ def initialize(status, description, priority, tags = [])
+ @status = status
+ @description = description
+ @priority = priority
+ @tags = tags
+ end
+
def status
- self[:status].strip.downcase.to_sym
+ @status.strip.downcase.to_sym
end
def description
- self[:description].strip
+ @description.strip
end
def priority
- self[:priority].strip.downcase.to_sym
+ @priority.strip.downcase.to_sym
end
def tags
- if self[:tags] then
- self[:tags].scan(/[^,]+/).map(&:strip)
- else
- []
- end
+ @tags.scan(/[^,]+/).map(&:strip)
end
def has_them_all?(tags)
tags.all? { |tag| self.tags.include? tag }
end
+ def <=>(task)
+ task_as_list = [task.status, task.description, task.priority, task.tags]
+ [status, description, priority, tags] <=> task_as_list
+ end
+
end
-module TodoList
- include Criteria
+module TodoListTools
+ def tasks_todo
+ filter(Criteria.status(:todo)).todo_list.length
+ end
- def parse(raw_list)
- list_fields = [:status, :description, :priority, :tags]
- raw_list.scan(/[^\n]+/).map do |task|
- Hash[list_fields.zip task.scan(/[^\|]+/)]
- end
+ def tasks_in_progress
+ filter(Criteria.status(:current)).todo_list.length
end
- def filter(criterias)
- select { |task| criterias.call task}
+ def tasks_completed
+ filter(Criteria.status(:done)).todo_list.length
end
- def adjoin(list)
- self + list
+ def completed?
+ @todo_list.all? { |task| task.status == :done }
end
+end
- def tasks_todo
- filter(Criteria.status(:todo)).length
+class TodoList
+ include Enumerable
+ include TodoListTools
+ attr_reader :todo_list
+
+ def initialize(todo_list)
+ @todo_list = todo_list
end
- def tasks_in_progress
- filter(Criteria.status(:current)).length
+ def TodoList.parse(raw_todo_list)
+ todo_list = raw_todo_list.lines.map do |task|
+ Task.new *task.split('|')
+ end
+ TodoList.new(todo_list)
end
- def tasks_completed
- filter(Criteria.status(:done)).length
+ def filter(criterias)
+ TodoList.new(@todo_list.select { |task| criterias.block.call task})
end
- def completed?
- all? { |task| task.status == :done }
+ def adjoin(todo_object)
+ TodoList.new(@todo_list + todo_object.todo_list)
end
-end
-include TodoList
+ def each
+ @todo_list.each do |task|
+ yield task
+ end
+ end
+
+ def <=>(todo_list)
+ @todo_list <=> todo_list
+ end
+end

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

class Criteria
+
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
Criteria.new(-> (task) { task.status == status })
-
end
def Criteria.priority(priority)
Criteria.new(-> (task) { task.priority == priority })
end
def Criteria.tags(tags)
Criteria.new(-> (task) { task.has_them_all? tags })
end
def &(criteria)
Criteria.new(-> (task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
Criteria.new(-> (task) { criteria.block.call(task) or @block.call(task) })
end
def !()
Criteria.new(-> (task) { not @block.call(task) })
end
-
end
class Task
- def initialize(status, description, priority, tags = [])
- @status = status
- @description = description
- @priority = priority
- @tags = tags
- end
- def status
- @status.strip.downcase.to_sym
- end
+ attr_reader :status
+ attr_reader :description
+ attr_reader :priority
+ attr_reader :tags
- def description
- @description.strip
+ def initialize(status, description, priority, tags = [])
+ @status = status.strip.downcase.to_sym
+ @description = description.strip
+ @priority = priority.strip.downcase.to_sym
+ @tags = tags.split(",").map(&:strip)
end
- def priority
- @priority.strip.downcase.to_sym
- end
-
- def tags
- @tags.scan(/[^,]+/).map(&:strip)
- end
-
def has_them_all?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
- [status, description, priority, tags] <=> task_as_list
+ [@status, @description, @priority, @tags] <=> task_as_list
end
-
end
module TodoListTools
+
def tasks_todo
filter(Criteria.status(:todo)).todo_list.length
end
def tasks_in_progress
filter(Criteria.status(:current)).todo_list.length
end
def tasks_completed
filter(Criteria.status(:done)).todo_list.length
end
def completed?
@todo_list.all? { |task| task.status == :done }
end
end
class TodoList
+
include Enumerable
include TodoListTools
+
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
TodoList.new(@todo_list.select { |task| criterias.block.call task})
end
- def adjoin(todo_object)
- TodoList.new(@todo_list + todo_object.todo_list)
+ def adjoin(todo_list_object)
+ TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
def <=>(todo_list)
@todo_list <=> todo_list
end
end

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

class Criteria
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
Criteria.new(-> (task) { task.status == status })
end
def Criteria.priority(priority)
Criteria.new(-> (task) { task.priority == priority })
end
def Criteria.tags(tags)
- Criteria.new(-> (task) { task.has_them_all? tags })
+ Criteria.new(-> (task) { task.has_all_tags? tags })
end
def &(criteria)
Criteria.new(-> (task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
Criteria.new(-> (task) { criteria.block.call(task) or @block.call(task) })
end
def !()
Criteria.new(-> (task) { not @block.call(task) })
end
end
class Task
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags = [])
@status = status.strip.downcase.to_sym
@description = description.strip
@priority = priority.strip.downcase.to_sym
@tags = tags.split(",").map(&:strip)
end
- def has_them_all?(tags)
+ def has_all_tags?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
[@status, @description, @priority, @tags] <=> task_as_list
end
end
module TodoListTools
def tasks_todo
filter(Criteria.status(:todo)).todo_list.length
end
def tasks_in_progress
filter(Criteria.status(:current)).todo_list.length
end
def tasks_completed
filter(Criteria.status(:done)).todo_list.length
end
def completed?
@todo_list.all? { |task| task.status == :done }
end
end
class TodoList
include Enumerable
include TodoListTools
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
TodoList.new(@todo_list.select { |task| criterias.block.call task})
end
def adjoin(todo_list_object)
TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
def <=>(todo_list)
@todo_list <=> todo_list
end
end

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

class Criteria
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
- Criteria.new(-> (task) { task.status == status })
+ Criteria.new(->(task) { task.status == status })
end
def Criteria.priority(priority)
- Criteria.new(-> (task) { task.priority == priority })
+ Criteria.new(->(task) { task.priority == priority })
end
def Criteria.tags(tags)
- Criteria.new(-> (task) { task.has_all_tags? tags })
+ Criteria.new(->(task) { task.has_all_tags? tags })
end
def &(criteria)
- Criteria.new(-> (task) { criteria.block.call(task) and @block.call(task) })
+ Criteria.new(->(task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
- Criteria.new(-> (task) { criteria.block.call(task) or @block.call(task) })
+ Criteria.new(->(task) { criteria.block.call(task) or @block.call(task) })
end
def !()
- Criteria.new(-> (task) { not @block.call(task) })
+ Criteria.new(->(task) { not @block.call(task) })
end
end
class Task
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags = [])
@status = status.strip.downcase.to_sym
@description = description.strip
@priority = priority.strip.downcase.to_sym
@tags = tags.split(",").map(&:strip)
end
def has_all_tags?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
[@status, @description, @priority, @tags] <=> task_as_list
end
end
module TodoListTools
def tasks_todo
filter(Criteria.status(:todo)).todo_list.length
end
def tasks_in_progress
filter(Criteria.status(:current)).todo_list.length
end
def tasks_completed
filter(Criteria.status(:done)).todo_list.length
end
def completed?
@todo_list.all? { |task| task.status == :done }
end
end
class TodoList
include Enumerable
include TodoListTools
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
TodoList.new(@todo_list.select { |task| criterias.block.call task})
end
def adjoin(todo_list_object)
TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
def <=>(todo_list)
@todo_list <=> todo_list
end
end

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

class Criteria
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
Criteria.new(->(task) { task.status == status })
end
def Criteria.priority(priority)
Criteria.new(->(task) { task.priority == priority })
end
def Criteria.tags(tags)
Criteria.new(->(task) { task.has_all_tags? tags })
end
def &(criteria)
Criteria.new(->(task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
Criteria.new(->(task) { criteria.block.call(task) or @block.call(task) })
end
def !()
Criteria.new(->(task) { not @block.call(task) })
end
end
class Task
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags = [])
@status = status.strip.downcase.to_sym
@description = description.strip
@priority = priority.strip.downcase.to_sym
@tags = tags.split(",").map(&:strip)
end
def has_all_tags?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
[@status, @description, @priority, @tags] <=> task_as_list
end
end
module TodoListTools
def tasks_todo
filter(Criteria.status(:todo)).todo_list.length
end
def tasks_in_progress
filter(Criteria.status(:current)).todo_list.length
end
def tasks_completed
filter(Criteria.status(:done)).todo_list.length
end
def completed?
@todo_list.all? { |task| task.status == :done }
end
end
class TodoList
include Enumerable
include TodoListTools
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
- TodoList.new(@todo_list.select { |task| criterias.block.call task})
+ TodoList.new(@todo_list.select { |task| criterias.block.call task })
end
def adjoin(todo_list_object)
TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
def <=>(todo_list)
@todo_list <=> todo_list
end
end

Методи обнови решението на 06.11.2013 12:18 (преди около 11 години)

class Criteria
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
Criteria.new(->(task) { task.status == status })
end
def Criteria.priority(priority)
Criteria.new(->(task) { task.priority == priority })
end
def Criteria.tags(tags)
Criteria.new(->(task) { task.has_all_tags? tags })
end
def &(criteria)
Criteria.new(->(task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
Criteria.new(->(task) { criteria.block.call(task) or @block.call(task) })
end
def !()
Criteria.new(->(task) { not @block.call(task) })
end
end
class Task
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags = [])
@status = status.strip.downcase.to_sym
@description = description.strip
@priority = priority.strip.downcase.to_sym
@tags = tags.split(",").map(&:strip)
end
def has_all_tags?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
[@status, @description, @priority, @tags] <=> task_as_list
end
end
-module TodoListTools
-
- def tasks_todo
- filter(Criteria.status(:todo)).todo_list.length
- end
-
- def tasks_in_progress
- filter(Criteria.status(:current)).todo_list.length
- end
-
- def tasks_completed
- filter(Criteria.status(:done)).todo_list.length
- end
-
- def completed?
- @todo_list.all? { |task| task.status == :done }
- end
-end
-
class TodoList
include Enumerable
- include TodoListTools
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
TodoList.new(@todo_list.select { |task| criterias.block.call task })
end
def adjoin(todo_list_object)
TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
- def <=>(todo_list)
- @todo_list <=> todo_list
+ def tasks_todo
+ filter(Criteria.status(:todo)).todo_list.length
+ end
+
+ def tasks_in_progress
+ filter(Criteria.status(:current)).todo_list.length
+ end
+
+ def tasks_completed
+ filter(Criteria.status(:done)).todo_list.length
+ end
+
+ def completed?
+ @todo_list.all? { |task| task.status == :done }
end
end

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

class Criteria
attr_reader :block
def initialize(block)
@block = block
end
def Criteria.status(status)
Criteria.new(->(task) { task.status == status })
end
def Criteria.priority(priority)
Criteria.new(->(task) { task.priority == priority })
end
def Criteria.tags(tags)
Criteria.new(->(task) { task.has_all_tags? tags })
end
def &(criteria)
Criteria.new(->(task) { criteria.block.call(task) and @block.call(task) })
end
def |(criteria)
Criteria.new(->(task) { criteria.block.call(task) or @block.call(task) })
end
def !()
Criteria.new(->(task) { not @block.call(task) })
end
end
class Task
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags = [])
- @status = status.strip.downcase.to_sym
+ @status = status.strip.downcase.to_sym
@description = description.strip
- @priority = priority.strip.downcase.to_sym
- @tags = tags.split(",").map(&:strip)
+ @priority = priority.strip.downcase.to_sym
+ @tags = tags.split(",").map(&:strip)
end
def has_all_tags?(tags)
tags.all? { |tag| self.tags.include? tag }
end
def <=>(task)
task_as_list = [task.status, task.description, task.priority, task.tags]
[@status, @description, @priority, @tags] <=> task_as_list
end
end
class TodoList
include Enumerable
attr_reader :todo_list
def initialize(todo_list)
@todo_list = todo_list
end
def TodoList.parse(raw_todo_list)
todo_list = raw_todo_list.lines.map do |task|
Task.new *task.split('|')
end
TodoList.new(todo_list)
end
def filter(criterias)
TodoList.new(@todo_list.select { |task| criterias.block.call task })
end
def adjoin(todo_list_object)
TodoList.new(@todo_list + todo_list_object.todo_list)
end
def each
@todo_list.each do |task|
yield task
end
end
def tasks_todo
filter(Criteria.status(:todo)).todo_list.length
end
def tasks_in_progress
filter(Criteria.status(:current)).todo_list.length
end
def tasks_completed
filter(Criteria.status(:done)).todo_list.length
end
def completed?
@todo_list.all? { |task| task.status == :done }
end
end