Решение на Седма задача от Кузман Белев

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

Към профила на Кузман Белев

Резултати

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

Код

module LazyMode
include Enumerable
def self.create_file(name, &block)
new_file = File.new(name)
new_file.instance_eval(&block)
new_file
end
class Date
attr_reader :year, :month, :day
DAY_TYPE = { :d => 1, :w => 7, :m => 30 }
def initialize(date)
@year = date.slice(0, 4)
@month = date.slice(5, 2)
@day = date.slice(8, 2)
end
def to_s
"#{year}-#{month}-#{day}"
end
def add(time_frame)
time = (/\+\d+[dwm]/.match time_frame).to_s
count = (/\d+/.match time).to_s.to_i
type = (/[dwm]/.match time).to_s.to_sym
days = DAY_TYPE[type] * count
time_travel(self, days)
end
def ==(other)
self.to_s == other.to_s ? true : false
end
def >=(other)
return false if year.to_i < other.year.to_i
return false if month.to_i < other.month.to_i
return false if day.to_i < other.day.to_i
true
end
def <=(other)
return false if year.to_i > other.year.to_i
return false if month.to_i > other.month.to_i
return false if day.to_i > other.day.to_i
true
end
private
def time_travel(date, days)
nights, months, years = date.day.to_i + days, date.month.to_i, date.year
while nights > 30 do
months, nights = months.to_i + 1, nights.to_i - 30
end
while months > 12 do
years, months = years.to_i + 1, months.to_i - 12
end
LazyMode::Date.new(format_date(years.to_s, months.to_s, nights.to_s))
end
def format_date(years, months, nights)
formatted_year = ("0" * (4 - years.length)) + years
formatted_month = months.length == 1 ? "0" + months : months
formatted_day = nights.length == 1 ? "0" + nights : nights
"#{formatted_year}-#{formatted_month}-#{formatted_day}"
end
end
class File
attr_reader :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(header, @name, *tags)
new_note.instance_eval(&block)
@notes << new_note
end
def daily_agenda(day)
agenda = []
tasks = collect_tasks(@notes)
tasks.each { |note| agenda << return_note(note, day, day) }
agenda.delete(nil)
new_agenda = Agenda.new(agenda)
end
def weekly_agenda(day)
agenda = []
tasks = collect_tasks(@notes)
tasks.each { |note| agenda << return_note(note, day, day.add("+6d")) }
agenda.delete(nil)
new_agenda = Agenda.new(agenda)
end
private
def collect_tasks(tasks)
collection = []
tasks.each { |task| collection << task }
tasks.each { |task| collection << task.sub_notes}
collection.flatten
end
def return_note(note, start_date, end_date)
(note.date >= start_date and note.date <= end_date) ? note : nil
end
end
class Note
attr_reader :tags, :header, :file_name, :sub_notes, :date, :recurrence
def initialize(header, file_name, *tags)
@header = header
@tags = tags
@status = :topostpone
@file_name = file_name
@body = ''
@sub_notes = []
end
def note(header, *tags, &block)
new_note = Note.new(header, @file_name, *tags)
new_note.instance_eval(&block)
@sub_notes << new_note
end
def body(*text)
text.size == 1 ? @body = text.first : @body
end
def status(*arguments)
arguments.size == 1 ? @status = arguments.first : @status
end
def scheduled(date)
@recurrence = (/\+\d+[dwm]/.match date).to_s
@date = LazyMode::Date.new(date)
end
end
class Agenda
attr_reader :notes
def initialize(agenda)
@notes = agenda
end
def where(tag: /./, text: /./, status: /./)
filtered_notes = []
@notes.each { |note| filtered_notes << tag_finder(note, tag) }
@notes.each { |note| remove_text(filtered_notes, note, text) }
@notes.each { |note| remove_status(filtered_notes, note, status) }
filtered_notes.delete(nil)
filtered_agenda = Agenda.new(filtered_notes)
end
private
def remove_text(tasks, note, text)
if (text.match note.header).nil? and (text.match note.body).nil?
tasks.delete(note)
end
end
def remove_status(tasks, note, status)
tasks.delete(note) if (note.status.to_s.match(status.to_s)).nil?
end
def tag_finder(note, tag)
note.tags.include?(tag) ? note : nil
end
end
end

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

FFF.........FFFF..FFFF.F.FFFF..

Failures:

  1) LazyMode LazyMode::Date can parse year
     Failure/Error: expect(date.year).to eq(2012)
       
       expected: 2012
            got: "2012"
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:5:in `block (3 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) LazyMode LazyMode::Date can parse month
     Failure/Error: expect(date.month).to eq(10)
       
       expected: 10
            got: "10"
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:10:in `block (3 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) LazyMode LazyMode::Date can parse day
     Failure/Error: expect(date.day).to eq(3)
       
       expected: 3
            got: "03"
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:15:in `block (3 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) LazyMode#daily_agenda returns note scheduled without repeater
     Failure/Error: expect(note.date.year).to eq(2012)
       
       expected: 2012
            got: "2012"
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:136:in `block (3 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) LazyMode#daily_agenda returns note scheduled with daily repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:153:in `block (3 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) LazyMode#daily_agenda returns note scheduled with weekly repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:175:in `block (3 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) LazyMode#daily_agenda returns note scheduled with monthly repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:198:in `block (3 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) LazyMode#weekly_agenda returns note scheduled without repeater
     Failure/Error: expect(note.date.year).to eq(2012)
       
       expected: 2012
            got: "2012"
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:254:in `block (3 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) LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
     Failure/Error: expect(agenda.notes.size).to eq(2)
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:271:in `block (3 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) LazyMode#weekly_agenda returns note scheduled with weekly repeater
     Failure/Error: expect(agenda.notes.size).to eq(2)
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:295:in `block (3 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) LazyMode#weekly_agenda returns note scheduled with monthly repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:327:in `block (3 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) LazyMode#weekly_agenda returns nested notes
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:361:in `block (3 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) LazyMode#where filters by body text
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:411:in `block (3 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) LazyMode#where filters by header text
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:423:in `block (3 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) LazyMode#where filters by status
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:434:in `block (3 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) LazyMode#where filters by multiple filters
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-110evcz/spec.rb:445:in `block (3 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.02079 seconds
31 examples, 16 failures

Failed examples:

rspec /tmp/d20160107-5693-110evcz/spec.rb:3 # LazyMode LazyMode::Date can parse year
rspec /tmp/d20160107-5693-110evcz/spec.rb:8 # LazyMode LazyMode::Date can parse month
rspec /tmp/d20160107-5693-110evcz/spec.rb:13 # LazyMode LazyMode::Date can parse day
rspec /tmp/d20160107-5693-110evcz/spec.rb:119 # LazyMode#daily_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:141 # LazyMode#daily_agenda returns note scheduled with daily repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:163 # LazyMode#daily_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:186 # LazyMode#daily_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:237 # LazyMode#weekly_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:259 # LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:283 # LazyMode#weekly_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:315 # LazyMode#weekly_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-110evcz/spec.rb:348 # LazyMode#weekly_agenda returns nested notes
rspec /tmp/d20160107-5693-110evcz/spec.rb:408 # LazyMode#where filters by body text
rspec /tmp/d20160107-5693-110evcz/spec.rb:420 # LazyMode#where filters by header text
rspec /tmp/d20160107-5693-110evcz/spec.rb:432 # LazyMode#where filters by status
rspec /tmp/d20160107-5693-110evcz/spec.rb:443 # LazyMode#where filters by multiple filters

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

Кузман обнови решението на 21.12.2015 16:24 (преди около 9 години)

+module LazyMode
+ include Enumerable
+
+ def self.create_file(name, &block)
+ new_file = File.new(name)
+ new_file.instance_eval(&block)
+ new_file
+ end
+
+ class Date
+ attr_reader :year, :month, :day
+
+ DAY_TYPE = { :d => 1, :w => 7, :m => 30 }
+
+ def initialize(date)
+ @year = date.slice(0, 4)
+ @month = date.slice(5, 2)
+ @day = date.slice(8, 2)
+ end
+
+ def to_s
+ "#{year}-#{month}-#{day}"
+ end
+
+ def add(time_frame)
+ time = (/\+\d+[dwm]/.match time_frame).to_s
+ count = (/\d+/.match time).to_s.to_i
+ type = (/[dwm]/.match time).to_s.to_sym
+ days = DAY_TYPE[type] * count
+ time_travel(self, days)
+ end
+
+ def time_travel(date, days)
+ nights, months, years = date.day.to_i + days, date.month.to_i, date.year
+ while nights > 30 do
+ months, nights = months.to_i + 1, nights.to_i - 30
+ end
+ while months > 12 do
+ years, months = years.to_i + 1, months.to_i - 12
+ end
+ LazyMode::Date.new(format_date(years.to_s, months.to_s, nights.to_s))
+ end
+
+ def ==(other)
+ self.to_s == other.to_s ? true : false
+ end
+
+ def >=(other)
+ return false if year.to_i < other.year.to_i
+ return false if month.to_i < other.month.to_i
+ return false if day.to_i < other.day.to_i
+ true
+ end
+
+ def <=(other)
+ return false if year.to_i > other.year.to_i
+ return false if month.to_i > other.month.to_i
+ return false if day.to_i > other.day.to_i
+ true
+ end
+
+ def format_date(years, months, nights)
+ formatted_year = ("0" * (4 - years.length)) + years
+ formatted_month = months.length == 1 ? "0" + months : months
+ formatted_day = nights.length == 1 ? "0" + nights : nights
+ "#{formatted_year}-#{formatted_month}-#{formatted_day}"
+ end
+ end
+
+ class File
+ attr_reader :name, :notes
+
+ def initialize(name)
+ @name = name
+ @notes = []
+ end
+
+ def note(header, *tags, &block)
+ new_note = Note.new(header, @name, *tags)
+ new_note.instance_eval(&block)
+ @notes << new_note
+ end
+
+ def daily_agenda(day)
+ agenda = []
+ tasks = collect_tasks(@notes)
+ tasks.each { |note| agenda << return_note(note, day, day) }
+ agenda.delete(nil)
+ new_agenda = Agenda.new(agenda)
+ end
+
+ def weekly_agenda(day)
+ agenda = []
+ tasks = collect_tasks(@notes)
+ tasks.each { |note| agenda << return_note(note, day, day.add("+6d")) }
+ agenda.delete(nil)
+ new_agenda = Agenda.new(agenda)
+ end
+
+ def collect_tasks(tasks)
+ collection = []
+ tasks.each { |task| collection << task }
+ tasks.each { |task| collection << task.sub_notes}
+ collection.flatten
+ end
+
+ def return_note(note, start_date, end_date)
+ (note.date >= start_date and note.date <= end_date) ? note : nil
+ end
+ end
+
+ class Note
+ attr_reader :tags, :header, :file_name, :sub_notes, :date, :recurrence
+
+ def initialize(header, file_name, *tags)
+ @header = header
+ @tags = tags
+ @status = :topostpone
+ @file_name = file_name
+ @body = ''
+ @sub_notes = []
+ end
+
+ def note(header, *tags, &block)
+ new_note = Note.new(header, @file_name, *tags)
+ new_note.instance_eval(&block)
+ @sub_notes << new_note
+ end
+
+ def body(*text)
+ text.size == 1 ? @body = text.first : @body
+ end
+
+ def status(*arguments)
+ arguments.size == 1 ? @status = arguments.first : @status
+ end
+
+ def scheduled(date)
+ @recurrence = (/\+\d+[dwm]/.match date).to_s
+ @date = LazyMode::Date.new(date)
+ end
+ end
+
+ class Agenda
+ attr_reader :notes
+
+ def initialize(agenda)
+ @notes = agenda
+ end
+
+ def where(tag: /./, text: /./, status: /./)
+ filtered_notes = []
+ @notes.each { |note| filtered_notes << tag_finder(note, tag) }
+ @notes.each { |note| remove_text(filtered_notes, note, text) }
+ @notes.each { |note| remove_status(filtered_notes, note, status) }
+ filtered_notes.delete(nil)
+ filtered_agenda = Agenda.new(filtered_notes)
+ end
+
+ def remove_text(tasks, note, text)
+ if (text.match note.header).nil? and (text.match note.body).nil?
+ tasks.delete(note)
+ end
+ end
+
+ def remove_status(tasks, note, status)
+ tasks.delete(note) if (note.status.to_s.match(status.to_s)).nil?
+ end
+
+ def tag_finder(note, tag)
+ note.tags.include?(tag) ? note : nil
+ end
+ end
+end

Кузман обнови решението на 21.12.2015 16:33 (преди около 9 години)

module LazyMode
include Enumerable
def self.create_file(name, &block)
new_file = File.new(name)
new_file.instance_eval(&block)
new_file
end
class Date
attr_reader :year, :month, :day
DAY_TYPE = { :d => 1, :w => 7, :m => 30 }
def initialize(date)
@year = date.slice(0, 4)
@month = date.slice(5, 2)
@day = date.slice(8, 2)
end
def to_s
"#{year}-#{month}-#{day}"
end
def add(time_frame)
time = (/\+\d+[dwm]/.match time_frame).to_s
count = (/\d+/.match time).to_s.to_i
type = (/[dwm]/.match time).to_s.to_sym
days = DAY_TYPE[type] * count
time_travel(self, days)
end
- def time_travel(date, days)
- nights, months, years = date.day.to_i + days, date.month.to_i, date.year
- while nights > 30 do
- months, nights = months.to_i + 1, nights.to_i - 30
- end
- while months > 12 do
- years, months = years.to_i + 1, months.to_i - 12
- end
- LazyMode::Date.new(format_date(years.to_s, months.to_s, nights.to_s))
- end
-
def ==(other)
self.to_s == other.to_s ? true : false
end
def >=(other)
return false if year.to_i < other.year.to_i
return false if month.to_i < other.month.to_i
return false if day.to_i < other.day.to_i
true
end
def <=(other)
return false if year.to_i > other.year.to_i
return false if month.to_i > other.month.to_i
return false if day.to_i > other.day.to_i
true
end
+ private
+
+ def time_travel(date, days)
+ nights, months, years = date.day.to_i + days, date.month.to_i, date.year
+ while nights > 30 do
+ months, nights = months.to_i + 1, nights.to_i - 30
+ end
+ while months > 12 do
+ years, months = years.to_i + 1, months.to_i - 12
+ end
+ LazyMode::Date.new(format_date(years.to_s, months.to_s, nights.to_s))
+ end
+
def format_date(years, months, nights)
formatted_year = ("0" * (4 - years.length)) + years
formatted_month = months.length == 1 ? "0" + months : months
formatted_day = nights.length == 1 ? "0" + nights : nights
"#{formatted_year}-#{formatted_month}-#{formatted_day}"
end
end
class File
attr_reader :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(header, @name, *tags)
new_note.instance_eval(&block)
@notes << new_note
end
def daily_agenda(day)
agenda = []
tasks = collect_tasks(@notes)
tasks.each { |note| agenda << return_note(note, day, day) }
agenda.delete(nil)
new_agenda = Agenda.new(agenda)
end
def weekly_agenda(day)
agenda = []
tasks = collect_tasks(@notes)
tasks.each { |note| agenda << return_note(note, day, day.add("+6d")) }
agenda.delete(nil)
new_agenda = Agenda.new(agenda)
end
+ private
+
def collect_tasks(tasks)
collection = []
tasks.each { |task| collection << task }
tasks.each { |task| collection << task.sub_notes}
collection.flatten
end
def return_note(note, start_date, end_date)
(note.date >= start_date and note.date <= end_date) ? note : nil
end
end
class Note
attr_reader :tags, :header, :file_name, :sub_notes, :date, :recurrence
def initialize(header, file_name, *tags)
@header = header
@tags = tags
@status = :topostpone
@file_name = file_name
@body = ''
@sub_notes = []
end
def note(header, *tags, &block)
new_note = Note.new(header, @file_name, *tags)
new_note.instance_eval(&block)
@sub_notes << new_note
end
def body(*text)
text.size == 1 ? @body = text.first : @body
end
def status(*arguments)
arguments.size == 1 ? @status = arguments.first : @status
end
def scheduled(date)
@recurrence = (/\+\d+[dwm]/.match date).to_s
@date = LazyMode::Date.new(date)
end
end
class Agenda
attr_reader :notes
def initialize(agenda)
@notes = agenda
end
def where(tag: /./, text: /./, status: /./)
filtered_notes = []
@notes.each { |note| filtered_notes << tag_finder(note, tag) }
@notes.each { |note| remove_text(filtered_notes, note, text) }
@notes.each { |note| remove_status(filtered_notes, note, status) }
filtered_notes.delete(nil)
filtered_agenda = Agenda.new(filtered_notes)
end
+
+ private
def remove_text(tasks, note, text)
if (text.match note.header).nil? and (text.match note.body).nil?
tasks.delete(note)
end
end
def remove_status(tasks, note, status)
tasks.delete(note) if (note.status.to_s.match(status.to_s)).nil?
end
def tag_finder(note, tag)
note.tags.include?(tag) ? note : nil
end
end
end