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

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

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

Резултати

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

Код

class TrueClass
def !
false
end
end
class FalseClass
def !
true
end
end
class Task
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 != [] ? tags.split(', ') : tags
end
end
class Criteria
class CriteriaExpression
attr_accessor :operator, :operands
def initialize(operator, operands)
@operator = operator
@operands = operands
end
def to_a
[operator, operands]
end
def &(other)
CriteriaExpression.new :&, [to_a, other.to_a]
end
def |(other)
CriteriaExpression.new :|, [to_a, other.to_a]
end
def !
CriteriaExpression.new :!, [to_a]
end
def match?(task)
operator.nil? ? eval_without_operator(task) : eval_with_operator(task)
end
private
def eval_with_operator(task)
if operands.length == 2
expr = CriteriaExpression.new(*operands[0]).match?(task)
expr.send(operator, CriteriaExpression.new(*operands[1]).match?(task))
else
CriteriaExpression.new(*operands[0]).match?(task).send(operator)
end
end
def eval_without_operator(task)
if operands[0][0] != :tags
return task.send(operands[0][0]) == operands[0][1]
end
operands[0][1].all? { |item| task.send(operands[0][0]).include? item }
end
end
class << self
def status(status)
CriteriaExpression.new nil, [[:status, status]]
end
def priority(priority)
CriteriaExpression.new nil, [[:priority, priority]]
end
def tags(tags)
CriteriaExpression.new nil, [[:tags, tags]]
end
end
end
class TodoList
class TaskList
include Enumerable
def each
data.each { |item| yield item }
end
attr_accessor :data
def initialize(data)
@data = data
end
def filter(criteria)
TaskList.new(data.select { |task| criteria.match?(task) })
end
def adjoin(other)
TaskList.new(data | (other.data))
end
def tasks_todo
filter(Criteria.status(:todo)).data.length
end
def tasks_in_progress
filter(Criteria.status(:current)).data.length
end
def tasks_completed
filter(Criteria.status(:done)).data.length
end
def completed?
data.all? { |task| task.status == :done }
end
end
def self.parse(text)
tasks = text.split("\n").each { |task| task.strip! }
TaskList.new(tasks.map { |item| Task.new(*(item.split(/\s*\|\s*/))) })
end
end

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

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

Finished in 0.0421 seconds
22 examples, 0 failures

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

Георги обнови решението на 05.11.2013 19:30 (преди над 10 години)

+class TrueClass
+ def !
+ false
+ end
+end
+
+class FalseClass
+ def !
+ true
+ end
+end
+
+class Task
+ 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 != [] ? tags.split(', ') : tags
+ end
+end
+
+class Criteria
+ class << self
+ def status(st)
+ CriteriaExpression.new nil, [[:status, st]]
+ end
+
+ def priority(pr)
+ CriteriaExpression.new nil, [[:priority, pr]]
+ end
+
+ def tags(tgs)
+ CriteriaExpression.new nil, [[:tags, tgs]]
+ end
+ end
+end
+
+class CriteriaExpression
+ attr_accessor :operator, :operands
+
+ def initialize(operator, operands)
+ @operator = operator
+ @operands = operands
+ end
+
+ def to_a
+ [operator, operands]
+ end
+
+ def &(other)
+ CriteriaExpression.new :&, [to_a, other.to_a]
+ end
+
+ def |(other)
+ CriteriaExpression.new :|, [to_a, other.to_a]
+ end
+
+ def !
+ CriteriaExpression.new :!, [to_a]
+ end
+
+ def eval(task)
+ operator.nil? ? eval_without_operator(task) : eval_with_operator(task)
+ end
+
+ private
+
+ def eval_with_operator(task)
+ if operands.length == 2
+ expr = CriteriaExpression.new(*operands[0]).eval(task)
+ expr.send(operator, CriteriaExpression.new(*operands[1]).eval(task))
+ else
+ CriteriaExpression.new(*operands[0]).eval(task).send(operator)
+ end
+ end
+
+ def eval_without_operator(task)
+ if operands[0][0] != :tags
+ return task.send(operands[0][0]) == operands[0][1]
+ end
+ operands[0][1].all? { |item| task.send(operands[0][0]).include? item }
+
+ end
+end
+
+class TodoList
+ class << self
+ def parse(text)
+ tasks = text.split("\n").each { |task| task.strip! }
+ TaskList.new(tasks.map { |item| Task.new(*(item.split(/\s*\|\s*/))) })
+ end
+ end
+end
+
+class TaskList
+ include Enumerable
+
+ def each
+ data.each { |item| yield item }
+ end
+
+ attr_accessor :data
+
+ def initialize(data)
+ @data = data
+ end
+
+ def filter(criteria)
+ TaskList.new(data.select { |task| criteria.eval(task) })
+ end
+
+ def adjoin(other)
+ TaskList.new(data | (other.data))
+ end
+
+ def tasks_todo
+ filter(Criteria.status(:todo)).data.length
+ end
+
+ def tasks_in_progress
+ filter(Criteria.status(:current)).data.length
+ end
+
+ def tasks_completed
+ filter(Criteria.status(:done)).data.length
+ end
+
+ def completed?
+ data.all? { |task| task.status == :done }
+ end
+end

Георги обнови решението на 05.11.2013 19:32 (преди над 10 години)

class TrueClass
def !
false
end
end
class FalseClass
def !
true
end
end
class Task
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 != [] ? tags.split(', ') : tags
end
end
class Criteria
class << self
- def status(st)
- CriteriaExpression.new nil, [[:status, st]]
+ def status(status)
+ CriteriaExpression.new nil, [[:status, status]]
end
- def priority(pr)
- CriteriaExpression.new nil, [[:priority, pr]]
+ def priority(priority)
+ CriteriaExpression.new nil, [[:priority, priority]]
end
- def tags(tgs)
- CriteriaExpression.new nil, [[:tags, tgs]]
+ def tags(tags)
+ CriteriaExpression.new nil, [[:tags, tags]]
end
end
end
class CriteriaExpression
attr_accessor :operator, :operands
def initialize(operator, operands)
@operator = operator
@operands = operands
end
def to_a
[operator, operands]
end
def &(other)
CriteriaExpression.new :&, [to_a, other.to_a]
end
def |(other)
CriteriaExpression.new :|, [to_a, other.to_a]
end
def !
CriteriaExpression.new :!, [to_a]
end
def eval(task)
operator.nil? ? eval_without_operator(task) : eval_with_operator(task)
end
private
def eval_with_operator(task)
if operands.length == 2
expr = CriteriaExpression.new(*operands[0]).eval(task)
expr.send(operator, CriteriaExpression.new(*operands[1]).eval(task))
else
CriteriaExpression.new(*operands[0]).eval(task).send(operator)
end
end
def eval_without_operator(task)
if operands[0][0] != :tags
return task.send(operands[0][0]) == operands[0][1]
end
operands[0][1].all? { |item| task.send(operands[0][0]).include? item }
end
end
class TodoList
class << self
def parse(text)
tasks = text.split("\n").each { |task| task.strip! }
TaskList.new(tasks.map { |item| Task.new(*(item.split(/\s*\|\s*/))) })
end
end
end
class TaskList
include Enumerable
def each
data.each { |item| yield item }
end
attr_accessor :data
def initialize(data)
@data = data
end
def filter(criteria)
TaskList.new(data.select { |task| criteria.eval(task) })
end
def adjoin(other)
TaskList.new(data | (other.data))
end
def tasks_todo
filter(Criteria.status(:todo)).data.length
end
def tasks_in_progress
filter(Criteria.status(:current)).data.length
end
def tasks_completed
filter(Criteria.status(:done)).data.length
end
def completed?
data.all? { |task| task.status == :done }
end
end

Георги обнови решението на 06.11.2013 17:08 (преди над 10 години)

class TrueClass
def !
false
end
end
class FalseClass
def !
true
end
end
class Task
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 != [] ? tags.split(', ') : tags
end
end
class Criteria
- class << self
- def status(status)
- CriteriaExpression.new nil, [[:status, status]]
+ class CriteriaExpression
+ attr_accessor :operator, :operands
+
+ def initialize(operator, operands)
+ @operator = operator
+ @operands = operands
end
- def priority(priority)
- CriteriaExpression.new nil, [[:priority, priority]]
+ def to_a
+ [operator, operands]
end
- def tags(tags)
- CriteriaExpression.new nil, [[:tags, tags]]
+ def &(other)
+ CriteriaExpression.new :&, [to_a, other.to_a]
end
- end
-end
-class CriteriaExpression
- attr_accessor :operator, :operands
+ def |(other)
+ CriteriaExpression.new :|, [to_a, other.to_a]
+ end
- def initialize(operator, operands)
- @operator = operator
- @operands = operands
- end
+ def !
+ CriteriaExpression.new :!, [to_a]
+ end
- def to_a
- [operator, operands]
- end
+ def match?(task)
+ operator.nil? ? eval_without_operator(task) : eval_with_operator(task)
+ end
- def &(other)
- CriteriaExpression.new :&, [to_a, other.to_a]
- end
+ private
- def |(other)
- CriteriaExpression.new :|, [to_a, other.to_a]
- end
+ def eval_with_operator(task)
+ if operands.length == 2
+ expr = CriteriaExpression.new(*operands[0]).match?(task)
+ expr.send(operator, CriteriaExpression.new(*operands[1]).match?(task))
+ else
+ CriteriaExpression.new(*operands[0]).match?(task).send(operator)
+ end
+ end
- def !
- CriteriaExpression.new :!, [to_a]
+ def eval_without_operator(task)
+ if operands[0][0] != :tags
+ return task.send(operands[0][0]) == operands[0][1]
+ end
+ operands[0][1].all? { |item| task.send(operands[0][0]).include? item }
+ end
end
- def eval(task)
- operator.nil? ? eval_without_operator(task) : eval_with_operator(task)
- end
-
- private
-
- def eval_with_operator(task)
- if operands.length == 2
- expr = CriteriaExpression.new(*operands[0]).eval(task)
- expr.send(operator, CriteriaExpression.new(*operands[1]).eval(task))
- else
- CriteriaExpression.new(*operands[0]).eval(task).send(operator)
+ class << self
+ def status(status)
+ CriteriaExpression.new nil, [[:status, status]]
end
- end
- def eval_without_operator(task)
- if operands[0][0] != :tags
- return task.send(operands[0][0]) == operands[0][1]
+ def priority(priority)
+ CriteriaExpression.new nil, [[:priority, priority]]
end
- operands[0][1].all? { |item| task.send(operands[0][0]).include? item }
+ def tags(tags)
+ CriteriaExpression.new nil, [[:tags, tags]]
+ end
end
end
class TodoList
- class << self
- def parse(text)
- tasks = text.split("\n").each { |task| task.strip! }
- TaskList.new(tasks.map { |item| Task.new(*(item.split(/\s*\|\s*/))) })
+ class TaskList
+ include Enumerable
+
+ def each
+ data.each { |item| yield item }
end
- end
-end
-class TaskList
- include Enumerable
+ attr_accessor :data
- def each
- data.each { |item| yield item }
- end
+ def initialize(data)
+ @data = data
+ end
- attr_accessor :data
+ def filter(criteria)
+ TaskList.new(data.select { |task| criteria.match?(task) })
+ end
- def initialize(data)
- @data = data
- end
+ def adjoin(other)
+ TaskList.new(data | (other.data))
+ end
- def filter(criteria)
- TaskList.new(data.select { |task| criteria.eval(task) })
- end
+ def tasks_todo
+ filter(Criteria.status(:todo)).data.length
+ end
- def adjoin(other)
- TaskList.new(data | (other.data))
- end
+ def tasks_in_progress
+ filter(Criteria.status(:current)).data.length
+ end
- def tasks_todo
- filter(Criteria.status(:todo)).data.length
- end
+ def tasks_completed
+ filter(Criteria.status(:done)).data.length
+ end
- def tasks_in_progress
- filter(Criteria.status(:current)).data.length
+ def completed?
+ data.all? { |task| task.status == :done }
+ end
end
- def tasks_completed
- filter(Criteria.status(:done)).data.length
- end
-
- def completed?
- data.all? { |task| task.status == :done }
+ def self.parse(text)
+ tasks = text.split("\n").each { |task| task.strip! }
+ TaskList.new(tasks.map { |item| Task.new(*(item.split(/\s*\|\s*/))) })
end
end