Решение на Втора задача от Георги Гърдев

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

Към профила на Георги Гърдев

Резултати

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

Код

class Task
def self.parse(line)
status, description, priority, tags = line.split('|').map(&:strip)
tags = tags.nil? ? [] : tags.split(', ')
Task.new status, description, priority, tags
end
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
def self.parse(input)
lines = input.split("\n")
TodoList.new lines.map { |line| Task.parse(line) }
end
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def completed?
@tasks.all? { |task| task.status == :done }
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.matches.(task) }
end
def adjoin(other)
TodoList.new (@tasks + other.tasks).uniq
end
def tasks_completed
@tasks.select { |task| task.status == :done }.size
end
def tasks_in_progress
@tasks.select { |task| task.status == :current }.size
end
def tasks_todo
@tasks.select { |task| task.status == :todo }.size
end
end
class Criteria
class << self
def status(status)
Criteria.new -> (task) { task.status == status }
end
def priority(priority)
Criteria.new -> (task) { task.priority == priority }
end
def tags(tags)
Criteria.new -> (task) { (task.tags & tags).length == tags.length }
end
end
attr_reader :matches
def initialize(matches)
@matches = matches
end
def &(other)
Criteria.new -> (task) do
matches.(task) && other.matches.(task)
end
end
def |(other)
Criteria.new -> (task) do
matches.(task) || other.matches.(task)
end
end
def !
Criteria.new -> (task) { not matches.(task) }
end
end

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

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

Finished in 0.03929 seconds
22 examples, 0 failures

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

Георги обнови решението на 01.11.2013 23:51 (преди около 11 години)

+class Task
+ class << self
+ def parse(line)
+ status, description, priority, tags = line.split('|').map(&:strip)
+ tags = tags.nil? ? [] : tags.split(', ')
+
+ Task.new status, description, priority, tags
+ end
+ end
+
+ attr_accessor :status, :description, :priority, :tags
+
+ def initialize(status, description, priority, tags)
+ @status = status.downcase.to_sym
+ @description = description
+ @priority = priority.downcase.to_sym
+ @tags = tags
+ end
+end
+
+class TodoList
+ TASK_STATUSES = {done: 'completed', current: 'in_progress', todo: 'todo'}
+
+ class << self
+ def parse(input)
+ lines = input.split("\n")
+ TodoList.new lines.map { |line| Task.parse(line) }
+ end
+ end
+
+ include Enumerable
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def completed?
+ @tasks.all? { |task| task.status == :done }
+ end
+
+ def filter(criteria)
+ TodoList.new @tasks.select { |task| criteria.criterion.call task }
+ end
+
+ def adjoin(other)
+ TodoList.new @tasks + other.tasks
+ end
+
+ def method_missing(method, *args, &block)
+ method.to_s =~ /^tasks_(.+)$/
+ status = TASK_STATUSES.key($1)
+
+ @tasks.select { |task| task.status == status }.size
+ end
+end
+
+class Criteria
+ class << self
+ def status(status)
+ Criteria.new -> (task) { task.status == status }
+ end
+
+ def priority(priority)
+ Criteria.new -> (task) { task.priority == priority }
+ end
+
+ def tags(tags)
+ Criteria.new -> (task) do
+ (task.tags & tags).length == tags.length
+ end
+ end
+ end
+
+ attr_reader :criterion
+
+ def initialize(criterion)
+ @criterion = criterion
+ end
+
+ def &(other)
+ Criteria.new -> (task) do
+ criterion.call(task) & other.criterion.call(task)
+ end
+ end
+
+ def |(other)
+ Criteria.new -> (task) do
+ criterion.call(task) || other.criterion.call(task)
+ end
+ end
+
+ def !
+ Criteria.new -> (task) { not criterion.call(task) }
+ end
+end

Георги обнови решението на 05.11.2013 21:24 (преди около 11 години)

class Task
class << self
def parse(line)
status, description, priority, tags = line.split('|').map(&:strip)
tags = tags.nil? ? [] : tags.split(', ')
Task.new status, description, priority, tags
end
end
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
- TASK_STATUSES = {done: 'completed', current: 'in_progress', todo: 'todo'}
-
- class << self
- def parse(input)
- lines = input.split("\n")
- TodoList.new lines.map { |line| Task.parse(line) }
- end
+ def self.parse(input)
+ lines = input.split("\n")
+ TodoList.new lines.map { |line| Task.parse(line) }
end
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def completed?
@tasks.all? { |task| task.status == :done }
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.criterion.call task }
end
def adjoin(other)
- TodoList.new @tasks + other.tasks
+ TodoList.new (@tasks + other.tasks).uniq
end
- def method_missing(method, *args, &block)
- method.to_s =~ /^tasks_(.+)$/
- status = TASK_STATUSES.key($1)
+ def tasks_completed
+ @tasks.select { |task| task.status == :done }.size
+ end
- @tasks.select { |task| task.status == status }.size
+ def tasks_in_progress
+ @tasks.select { |task| task.status == :current }.size
end
+
+ def tasks_todo
+ @tasks.select { |task| task.status == :todo }.size
+ end
end
class Criteria
class << self
def status(status)
Criteria.new -> (task) { task.status == status }
end
def priority(priority)
Criteria.new -> (task) { task.priority == priority }
end
def tags(tags)
Criteria.new -> (task) do
(task.tags & tags).length == tags.length
end
end
end
attr_reader :criterion
def initialize(criterion)
@criterion = criterion
end
def &(other)
Criteria.new -> (task) do
- criterion.call(task) & other.criterion.call(task)
+ criterion.call(task) && other.criterion.call(task)
end
end
def |(other)
Criteria.new -> (task) do
criterion.call(task) || other.criterion.call(task)
end
end
def !
Criteria.new -> (task) { not criterion.call(task) }
end
end

Георги обнови решението на 05.11.2013 21:25 (преди около 11 години)

class Task
- class << self
- def parse(line)
- status, description, priority, tags = line.split('|').map(&:strip)
- tags = tags.nil? ? [] : tags.split(', ')
+ def self.parse(line)
+ status, description, priority, tags = line.split('|').map(&:strip)
+ tags = tags.nil? ? [] : tags.split(', ')
- Task.new status, description, priority, tags
- end
+ Task.new status, description, priority, tags
end
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
def self.parse(input)
lines = input.split("\n")
TodoList.new lines.map { |line| Task.parse(line) }
end
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def completed?
@tasks.all? { |task| task.status == :done }
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.criterion.call task }
end
def adjoin(other)
TodoList.new (@tasks + other.tasks).uniq
end
def tasks_completed
@tasks.select { |task| task.status == :done }.size
end
def tasks_in_progress
@tasks.select { |task| task.status == :current }.size
end
def tasks_todo
@tasks.select { |task| task.status == :todo }.size
end
end
class Criteria
class << self
def status(status)
Criteria.new -> (task) { task.status == status }
end
def priority(priority)
Criteria.new -> (task) { task.priority == priority }
end
def tags(tags)
Criteria.new -> (task) do
(task.tags & tags).length == tags.length
end
end
end
attr_reader :criterion
def initialize(criterion)
@criterion = criterion
end
def &(other)
Criteria.new -> (task) do
criterion.call(task) && other.criterion.call(task)
end
end
def |(other)
Criteria.new -> (task) do
criterion.call(task) || other.criterion.call(task)
end
end
def !
Criteria.new -> (task) { not criterion.call(task) }
end
end

Георги обнови решението на 05.11.2013 21:32 (преди около 11 години)

class Task
def self.parse(line)
status, description, priority, tags = line.split('|').map(&:strip)
tags = tags.nil? ? [] : tags.split(', ')
Task.new status, description, priority, tags
end
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
def self.parse(input)
lines = input.split("\n")
TodoList.new lines.map { |line| Task.parse(line) }
end
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def completed?
@tasks.all? { |task| task.status == :done }
end
def filter(criteria)
- TodoList.new @tasks.select { |task| criteria.criterion.call task }
+ TodoList.new @tasks.select { |task| criteria.matches.(task) }
end
def adjoin(other)
TodoList.new (@tasks + other.tasks).uniq
end
def tasks_completed
@tasks.select { |task| task.status == :done }.size
end
def tasks_in_progress
@tasks.select { |task| task.status == :current }.size
end
def tasks_todo
@tasks.select { |task| task.status == :todo }.size
end
end
class Criteria
class << self
def status(status)
Criteria.new -> (task) { task.status == status }
end
def priority(priority)
Criteria.new -> (task) { task.priority == priority }
end
def tags(tags)
Criteria.new -> (task) do
(task.tags & tags).length == tags.length
end
end
end
- attr_reader :criterion
+ attr_reader :matches
- def initialize(criterion)
- @criterion = criterion
+ def initialize(matches)
+ @matches = matches
end
def &(other)
Criteria.new -> (task) do
- criterion.call(task) && other.criterion.call(task)
+ matches.(task) && other.matches.(task)
end
end
def |(other)
Criteria.new -> (task) do
- criterion.call(task) || other.criterion.call(task)
+ matches.(task) || other.matches.(task)
end
end
def !
- Criteria.new -> (task) { not criterion.call(task) }
+ Criteria.new -> (task) { not matches.(task) }
end
end

Георги обнови решението на 05.11.2013 21:34 (преди около 11 години)

class Task
def self.parse(line)
status, description, priority, tags = line.split('|').map(&:strip)
tags = tags.nil? ? [] : tags.split(', ')
Task.new status, description, priority, tags
end
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
def self.parse(input)
lines = input.split("\n")
TodoList.new lines.map { |line| Task.parse(line) }
end
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def completed?
@tasks.all? { |task| task.status == :done }
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.matches.(task) }
end
def adjoin(other)
TodoList.new (@tasks + other.tasks).uniq
end
def tasks_completed
@tasks.select { |task| task.status == :done }.size
end
def tasks_in_progress
@tasks.select { |task| task.status == :current }.size
end
def tasks_todo
@tasks.select { |task| task.status == :todo }.size
end
end
class Criteria
class << self
def status(status)
Criteria.new -> (task) { task.status == status }
end
def priority(priority)
Criteria.new -> (task) { task.priority == priority }
end
def tags(tags)
- Criteria.new -> (task) do
- (task.tags & tags).length == tags.length
- end
+ Criteria.new -> (task) { (task.tags & tags).length == tags.length }
end
end
attr_reader :matches
def initialize(matches)
@matches = matches
end
def &(other)
Criteria.new -> (task) do
matches.(task) && other.matches.(task)
end
end
def |(other)
Criteria.new -> (task) do
matches.(task) || other.matches.(task)
end
end
def !
Criteria.new -> (task) { not matches.(task) }
end
end