Решение на Втора задача от Иван Латунов

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

Към профила на Иван Латунов

Резултати

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

Код

module Constants
STATUS = {
"TODO"f => :todo,
"DONE"f => :done,
"CURRENT"f => :current
}.freeze
PRIORITY = {
"High"f => :high,
"Normal"f => :normal,
"Low"f => :low
}.freeze
end
class TodoList
def initialize(item_array)
@array = item_array.select {|elem| elem.kind_of? TodoItem}
end
def each(&block)
@array.each {|x| yield x}
end
def TodoList.parse(todo_rows)
return_list = []
todo_rows.split("\n").each {|row| return_list << TodoItem.new(row.to_s)}
TodoList.new(return_list)
end
def filter(criteria)
end
def tasks_todo
@array.count {|x| x.status == :todo}
end
def tasks_in_progress
@array.count {|x| x.status == :current}
end
def tasks_done
@array.count{|x| x.status == :done}
end
def completed?
@array.length == tasks_done
end
def filter(criteria)
@array.select {|x| criteria.call(x)}
end
end
class TodoItem
attr_reader :tags
attr_reader :description
def initialize(row)
stripped_list = []
row.split('|').each {|x| stripped_list << x.strip}
@status, @description, @priority, @tags = stripped_list
@tags = stripped_list.pop.split(',') if @tags
end
def to_s
"#{@status} | #{@description} | #{@priority} | #{@tags}"
end
def status
Constants::STATUS[@status]
end
def priority
Constants::PRIORITY[@priority]
end
end
class Criteria
@operator = :==
class << self
def status(status)
Proc.new {|obj| obj.status.send(@operator, status)}
end
def priority(priority)
Proc.new {|obj| obj.priority.send(@operator, priority)}
end
def tags(tags)
Proc.new {|obj| obj.tags.send(@operator, tags)}
end
end
end

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

..FFFFFFFFF...FFFFFFF.

Failures:

  1) TodoList filters tasks by tag
     Failure/Error: todo_list.filter(Criteria.tags %w[food]).map(&:description).should =~ ['Eat spaghetti.']
       expected collection contained:  ["Eat spaghetti."]
       actual collection contained:    []
       the missing elements were:      ["Eat spaghetti."]
     # /tmp/d20131107-4393-sjc7xf/spec.rb:36: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 filters tasks by multiple tags
     Failure/Error: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Grok Ruby."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Grok Ruby."]
     # /tmp/d20131107-4393-sjc7xf/spec.rb:43: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 filtering by multiple tags matches only tasks with all the tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development FMI]).map(&:description).should =~ ['Do the 5th Ruby challenge.']
       expected collection contained:  ["Do the 5th Ruby challenge."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge."]
     # /tmp/d20131107-4393-sjc7xf/spec.rb:47: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 conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `&' for #<Proc:0xba3ebe0c@/tmp/d20131107-4393-sjc7xf/solution.rb:90>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:51: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 a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NoMethodError:
       undefined method `|' for #<Proc:0xba3e8cac@/tmp/d20131107-4393-sjc7xf/solution.rb:90>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:56: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 a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NoMethodError:
       undefined method `call' for false:FalseClass
     # /tmp/d20131107-4393-sjc7xf/solution.rb:52:in `block in filter'
     # /tmp/d20131107-4393-sjc7xf/solution.rb:52:in `select'
     # /tmp/d20131107-4393-sjc7xf/solution.rb:52:in `filter'
     # /tmp/d20131107-4393-sjc7xf/spec.rb:66: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 supports simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NoMethodError:
       undefined method `&' for #<Proc:0xba45c904@/tmp/d20131107-4393-sjc7xf/solution.rb:94>
     # /tmp/d20131107-4393-sjc7xf/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)>'

  8) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<Proc:0xba412a84@/tmp/d20131107-4393-sjc7xf/solution.rb:90>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:82: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 can be adjoined with another to-do list
     Failure/Error: adjoined    = development.adjoin food
     NoMethodError:
       undefined method `adjoin' for []:Array
     # /tmp/d20131107-4393-sjc7xf/spec.rb:99: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
     NoMethodError:
       undefined method `tasks_completed' for #<TodoList:0xba2d24a8>
     # /tmp/d20131107-4393-sjc7xf/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)>'

  11) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
     NoMethodError:
       undefined method `completed?' for #<Array:0xba2d0428>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:132: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)>'

  12) TodoList doesn't modify the to-do list when filtering
     Failure/Error: todo_list.should have(text_input.lines.count).items
     NoMethodError:
       undefined method `items' for #<TodoList:0xba2ca514>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:137: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)>'

  13) TodoList implements Enumerable
     Failure/Error: todo_list.should be_an Enumerable
       expected #<TodoList:0xba2be034 @array=[#<TodoItem:0xba2bef98 @status="TODO", @description="Eat spaghetti.", @priority="High", @tags=["food", " happiness"]>, #<TodoItem:0xba2bed90 @status="TODO", @description="Get 8 hours of sleep.", @priority="Low", @tags=["health"]>, #<TodoItem:0xba2beb9c @status="CURRENT", @description="Party animal.", @priority="Normal", @tags=["socialization"]>, #<TodoItem:0xba2bea0c @status="CURRENT", @description="Grok Ruby.", @priority="High", @tags=["development", " ruby"]>, #<TodoItem:0xba2be804 @status="DONE", @description="Have some tea.", @priority="Normal", @tags=nil>, #<TodoItem:0xba2be6ec @status="TODO", @description="Destroy Facebook and Google.", @priority="High", @tags=["save humanity", " conspiracy"]>, #<TodoItem:0xba2be548 @status="DONE", @description="Do the 5th Ruby challenge.", @priority="High", @tags=["ruby course", " FMI", " development", " ruby"]>, #<TodoItem:0xba2be368 @status="TODO", @description="Find missing socks.", @priority="Low", @tags=nil>, #<TodoItem:0xba2be250 @status="TODO", @description="Occupy Sofia University.", @priority="High", @tags=["#ДАНСwithMe", " #occupysu", " #оставка"]>]> to be a kind of Enumerable
     # /tmp/d20131107-4393-sjc7xf/spec.rb:142: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)>'

  14) TodoList contains tasks with the neccessary interface
     Failure/Error: task = todo_list.first
     NoMethodError:
       undefined method `first' for #<TodoList:0xba2b36e8>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:146: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)>'

  15) TodoList tasks have an array of tags
     Failure/Error: todo_list.first.tags.should be_an Array
     NoMethodError:
       undefined method `first' for #<TodoList:0xba2b1140>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:155: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)>'

  16) TodoList preserves the order of tasks
     Failure/Error: todo_list.map(&:description).should eq [
     NoMethodError:
       undefined method `map' for #<TodoList:0xba2aa4d0>
     # /tmp/d20131107-4393-sjc7xf/spec.rb:159: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.03814 seconds
22 examples, 16 failures

Failed examples:

rspec /tmp/d20131107-4393-sjc7xf/spec.rb:35 # TodoList filters tasks by tag
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:126 # TodoList returns the number of the completed tasks
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:130 # TodoList checks if all tasks are completed
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:135 # TodoList doesn't modify the to-do list when filtering
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:140 # TodoList implements Enumerable
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:145 # TodoList contains tasks with the neccessary interface
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:154 # TodoList tasks have an array of tags
rspec /tmp/d20131107-4393-sjc7xf/spec.rb:158 # TodoList preserves the order of tasks

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

Иван обнови решението на 06.11.2013 15:20 (преди над 10 години)

+module Constants
+ STATUS = {
+ "TODO"f => :todo,
+ "DONE"f => :done,
+ "CURRENT"f => :current
+ }.freeze
+
+ PRIORITY = {
+ "High"f => :high,
+ "Normal"f => :normal,
+ "Low"f => :low
+ }.freeze
+
+end
+
+class TodoList
+
+ def initialize(item_array)
+ @array = item_array.select {|elem| elem.kind_of? TodoItem}
+ end
+
+ def each(&block)
+ @array.each {|x| yield x}
+ end
+
+ def TodoList.parse(todo_rows)
+ return_list = []
+ todo_rows.split("\n").each {|row| return_list << TodoItem.new(row.to_s)}
+ TodoList.new(return_list)
+ end
+
+ def filter(criteria)
+ end
+
+ def tasks_todo
+ @array.count {|x| x.status == :todo}
+ end
+
+ def tasks_in_progress
+ @array.count {|x| x.status == :current}
+ end
+
+ def tasks_done
+ @array.count{|x| x.status == :done}
+ end
+
+ def completed?
+ @array.length == tasks_done
+ end
+
+end
+
+
+class TodoItem
+
+ attr_reader :tags
+ attr_reader :description
+
+ def initialize(row)
+ stripped_list = []
+ row.split('|').each {|x| stripped_list << x.strip}
+
+ @status, @description, @priority, @tags = stripped_list
+ @tags = stripped_list.pop.split(',') if @tags
+ end
+
+ def to_s
+ "#{@status} | #{@description} | #{@priority} | #{@tags}"
+ end
+
+ def status
+ Constants::STATUS[@status]
+ end
+
+ def priority
+ Constants::PRIORITY[@priority]
+ end
+
+end
+
+
+class Criteria
+ class << self
+ def status
+ end
+ def priority
+ end
+ def tags
+ end
+ end
+end

Иван обнови решението на 06.11.2013 16:57 (преди над 10 години)

module Constants
STATUS = {
"TODO"f => :todo,
"DONE"f => :done,
"CURRENT"f => :current
}.freeze
PRIORITY = {
"High"f => :high,
"Normal"f => :normal,
"Low"f => :low
}.freeze
end
class TodoList
def initialize(item_array)
@array = item_array.select {|elem| elem.kind_of? TodoItem}
end
def each(&block)
@array.each {|x| yield x}
end
def TodoList.parse(todo_rows)
return_list = []
todo_rows.split("\n").each {|row| return_list << TodoItem.new(row.to_s)}
TodoList.new(return_list)
end
def filter(criteria)
end
def tasks_todo
@array.count {|x| x.status == :todo}
end
def tasks_in_progress
@array.count {|x| x.status == :current}
end
def tasks_done
@array.count{|x| x.status == :done}
end
def completed?
@array.length == tasks_done
end
+ def filter(criteria)
+ @array.select {|x| criteria.call(x)}
+ end
+
end
class TodoItem
attr_reader :tags
attr_reader :description
def initialize(row)
stripped_list = []
row.split('|').each {|x| stripped_list << x.strip}
@status, @description, @priority, @tags = stripped_list
@tags = stripped_list.pop.split(',') if @tags
end
def to_s
"#{@status} | #{@description} | #{@priority} | #{@tags}"
end
def status
Constants::STATUS[@status]
end
def priority
Constants::PRIORITY[@priority]
end
end
class Criteria
+ @operator = :==
class << self
- def status
+ def status(status)
+ Proc.new {|obj| obj.status.send(@operator, status)}
end
- def priority
+
+ def priority(priority)
+ Proc.new {|obj| obj.priority.send(@operator, priority)}
end
- def tags
+
+ def tags(tags)
+ Proc.new {|obj| obj.tags.send(@operator, tags)}
+
end
+
end
-end
+end

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

module Constants
STATUS = {
"TODO"f => :todo,
"DONE"f => :done,
"CURRENT"f => :current
}.freeze
PRIORITY = {
"High"f => :high,
"Normal"f => :normal,
"Low"f => :low
}.freeze
end
class TodoList
def initialize(item_array)
@array = item_array.select {|elem| elem.kind_of? TodoItem}
end
def each(&block)
@array.each {|x| yield x}
end
def TodoList.parse(todo_rows)
return_list = []
todo_rows.split("\n").each {|row| return_list << TodoItem.new(row.to_s)}
TodoList.new(return_list)
end
def filter(criteria)
end
def tasks_todo
@array.count {|x| x.status == :todo}
end
def tasks_in_progress
@array.count {|x| x.status == :current}
end
def tasks_done
@array.count{|x| x.status == :done}
end
def completed?
@array.length == tasks_done
end
def filter(criteria)
@array.select {|x| criteria.call(x)}
end
end
class TodoItem
attr_reader :tags
attr_reader :description
def initialize(row)
stripped_list = []
row.split('|').each {|x| stripped_list << x.strip}
@status, @description, @priority, @tags = stripped_list
@tags = stripped_list.pop.split(',') if @tags
end
def to_s
"#{@status} | #{@description} | #{@priority} | #{@tags}"
end
def status
Constants::STATUS[@status]
end
def priority
Constants::PRIORITY[@priority]
end
end
class Criteria
@operator = :==
class << self
def status(status)
Proc.new {|obj| obj.status.send(@operator, status)}
end
def priority(priority)
Proc.new {|obj| obj.priority.send(@operator, priority)}
end
def tags(tags)
Proc.new {|obj| obj.tags.send(@operator, tags)}
+ end
+ def !
+ ret_crit = Criteria.new
+ ret_crit.operator = :!=
+ ret_crit
end
end
-end
+end

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

module Constants
STATUS = {
"TODO"f => :todo,
"DONE"f => :done,
"CURRENT"f => :current
}.freeze
PRIORITY = {
"High"f => :high,
"Normal"f => :normal,
"Low"f => :low
}.freeze
end
class TodoList
def initialize(item_array)
@array = item_array.select {|elem| elem.kind_of? TodoItem}
end
def each(&block)
@array.each {|x| yield x}
end
def TodoList.parse(todo_rows)
return_list = []
todo_rows.split("\n").each {|row| return_list << TodoItem.new(row.to_s)}
TodoList.new(return_list)
end
def filter(criteria)
end
def tasks_todo
@array.count {|x| x.status == :todo}
end
def tasks_in_progress
@array.count {|x| x.status == :current}
end
def tasks_done
@array.count{|x| x.status == :done}
end
def completed?
@array.length == tasks_done
end
def filter(criteria)
@array.select {|x| criteria.call(x)}
end
end
class TodoItem
attr_reader :tags
attr_reader :description
def initialize(row)
stripped_list = []
row.split('|').each {|x| stripped_list << x.strip}
@status, @description, @priority, @tags = stripped_list
@tags = stripped_list.pop.split(',') if @tags
end
def to_s
"#{@status} | #{@description} | #{@priority} | #{@tags}"
end
def status
Constants::STATUS[@status]
end
def priority
Constants::PRIORITY[@priority]
end
end
class Criteria
@operator = :==
class << self
def status(status)
Proc.new {|obj| obj.status.send(@operator, status)}
end
def priority(priority)
Proc.new {|obj| obj.priority.send(@operator, priority)}
end
def tags(tags)
Proc.new {|obj| obj.tags.send(@operator, tags)}
end
- def !
- ret_crit = Criteria.new
- ret_crit.operator = :!=
- ret_crit
- end
end
end