Решение на Седма задача от Мариян Борисов

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

Към профила на Мариян Борисов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 19 успешни тест(а)
  • 12 неуспешни тест(а)

Код

module LazyMode
class Date
attr_accessor :year, :month, :day
def initialize(date)
split_date = date.split "-"
@year = split_date[0].to_i
@month = split_date[1].to_i
@day = split_date[2].to_i
end
def -(date)
self_in_days = year * 12 * 30 + month * 30 + day
date_in_days = date.year * 12 * 30 + date.month * 30 + date.day
self_in_days - date_in_days
end
def <(date)
self - date < 0
end
def to_s
"%04d-%02d-%02d" % [@year, @month, @day]
end
end
class Note
attr_accessor :header, :file_name, :body, :status, :tags, :scheduled, :notes
attr_accessor :date
def initialize(header, file_name, status, tags)
@header = header
@file_name = file_name
@status = status
@tags = tags
end
end
class File
attr_accessor :name, :notes
def initialize(name, notes)
@name = name.dup
@notes = notes
end
def daily_agenda(date)
Agenda.new(self, date, 1)
end
def weekly_agenda(date)
Agenda.new(self, date, 7)
end
end
class NoteBuilder
def initialize(file_name, header = nil, tags = nil)
@notes = []
@file_name = file_name
@header = header
@tags = tags
@status = :topostpone
@body = ""
end
def status(symbol)
@status = symbol
end
def body(text)
@body = text
end
def scheduled(date)
@scheduled = date
end
def note(header, *tags, &block)
@notes << NoteBuilder.new(@file_name, header, tags).build(&block)
end
def build(&block)
instance_eval &block
note = Note.new(@header, @file_name, @status, @tags)
note.body = @body
note.scheduled = @scheduled
note.notes = @notes
note
end
end
class Agenda
attr_accessor :notes
DAYS_IN_LETTERS = {"d" => 1, "w" => 7, "m" => 30}
# TODO add filters to "where"
def initialize(notes_holder, date = nil, day_difference = nil, **filters)
@notes = flatten(notes_holder.notes).each { |note| note.date = date }
p @notes.size
if date.nil?
filter(**filters)
else
check_date(date, day_difference)
end
end
def where(**filters)
Agenda.new(self, **filters)
end
#private
def flatten(note_array)
return [] if note_array == []
note_array + note_array.reduce([]) do |array, note|
array + flatten(note.notes)
end
end
def parse_scheduled(scheduled)
split_scheduled = scheduled.split " "
days = 0
unless split_scheduled[1].nil?
matches = split_scheduled[1].match /\+(\d+)([dwm])/
days = matches[1].to_i * DAYS_IN_LETTERS[matches[2]]
end
[Date.new(split_scheduled[0]), days]
end
def filter(**filters)
# TODO
end
def check_date(date, day_difference)
@notes.keep_if do |note|
note_date, note_delta = *parse_scheduled(note.scheduled)
note_date < date and (date - note_date) % note_delta < day_difference
end
end
end
def self.create_file(file_name, &block)
File.new(file_name, NoteBuilder.new(file_name).build(&block).notes)
end
end

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

............2
F2
.2
.2
.1
.2
F2
F2
F2
F2
F1
.2
F3
0
F3
0
F3
0
F3
0
F3
0
F..

Failures:

  1) LazyMode#daily_agenda returns note scheduled without repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/spec.rb:131: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#daily_agenda returns nested notes
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/spec.rb:232: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#weekly_agenda returns note scheduled without repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/spec.rb:249: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#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: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/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)>'

  5) 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-jw91nt/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)>'

  6) 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-jw91nt/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)>'

  7) LazyMode#weekly_agenda returns nested notes
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-09-05'))
     ZeroDivisionError:
       divided by 0
     # /tmp/d20160107-5693-jw91nt/solution.rb:136:in `block in check_date'
     # /tmp/d20160107-5693-jw91nt/solution.rb:134:in `keep_if'
     # /tmp/d20160107-5693-jw91nt/solution.rb:134:in `check_date'
     # /tmp/d20160107-5693-jw91nt/solution.rb:103:in `initialize'
     # /tmp/d20160107-5693-jw91nt/solution.rb:51:in `new'
     # /tmp/d20160107-5693-jw91nt/solution.rb:51:in `weekly_agenda'
     # /tmp/d20160107-5693-jw91nt/spec.rb:359: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#where filters by tag
     Failure/Error: expect(notes.size).to eq(2)
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/spec.rb:392: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#where filters by body text
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/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)>'

  10) LazyMode#where filters by header text
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/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)>'

  11) LazyMode#where filters by status
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/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)>'

  12) LazyMode#where filters by multiple filters
     Failure/Error: expect(notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-jw91nt/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.01867 seconds
31 examples, 12 failures

Failed examples:

rspec /tmp/d20160107-5693-jw91nt/spec.rb:119 # LazyMode#daily_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-jw91nt/spec.rb:219 # LazyMode#daily_agenda returns nested notes
rspec /tmp/d20160107-5693-jw91nt/spec.rb:237 # LazyMode#weekly_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-jw91nt/spec.rb:259 # LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
rspec /tmp/d20160107-5693-jw91nt/spec.rb:283 # LazyMode#weekly_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-jw91nt/spec.rb:315 # LazyMode#weekly_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-jw91nt/spec.rb:348 # LazyMode#weekly_agenda returns nested notes
rspec /tmp/d20160107-5693-jw91nt/spec.rb:390 # LazyMode#where filters by tag
rspec /tmp/d20160107-5693-jw91nt/spec.rb:408 # LazyMode#where filters by body text
rspec /tmp/d20160107-5693-jw91nt/spec.rb:420 # LazyMode#where filters by header text
rspec /tmp/d20160107-5693-jw91nt/spec.rb:432 # LazyMode#where filters by status
rspec /tmp/d20160107-5693-jw91nt/spec.rb:443 # LazyMode#where filters by multiple filters

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

Мариян обнови решението на 21.12.2015 17:02 (преди около 9 години)

+module LazyMode
+ class Date
+ attr_accessor :year, :month, :day
+ def initialize(date)
+ split_date = date.split "-"
+ @year = split_date[0].to_i
+ @month = split_date[1].to_i
+ @day = split_date[2].to_i
+ end
+
+ def -(date)
+ self_in_days = year * 12 * 30 + month * 30 + day
+ date_in_days = date.year * 12 * 30 + date.month * 30 + date.day
+ self_in_days - date_in_days
+ end
+
+ def <(date)
+ self - date < 0
+ end
+
+ def to_s
+ "%04d-%02d-%02d" % [@year, @month, @day]
+ end
+ end
+
+ class Note
+ attr_accessor :header, :file_name, :body, :status, :tags, :scheduled, :notes
+
+ def initialize(header, file_name, status, tags)
+ @header = header
+ @file_name = file_name
+ @status = status
+ @tags = tags
+ end
+ end
+
+ class File
+ attr_accessor :name, :notes
+
+ def initialize(name, notes)
+ @name = name.dup
+ @notes = notes
+ end
+
+ def daily_agenda(date)
+ Agenda.new(self, date, 1)
+ end
+
+ def weekly_agenda(date)
+ Agenda.new(self, date, 7)
+ end
+ end
+
+ class NoteBuilder
+ def initialize(file_name, header = nil, tags = nil)
+ @notes = []
+ @file_name = file_name
+ @header = header
+ @tags = tags
+ @status = :topostpone
+ @body = ""
+ end
+
+ def status(symbol)
+ @status = symbol
+ end
+
+ def body(text)
+ @body = text
+ end
+
+ def scheduled(date)
+ @scheduled = date
+ end
+
+ def note(header, *tags, &block)
+ @notes << NoteBuilder.new(@file_name, header, tags).build(&block)
+ end
+
+ def build(&block)
+ instance_eval &block
+ note = Note.new(@header, @file_name, @status, @tags)
+ note.body = @body
+ note.scheduled = @scheduled
+ note.notes = @notes
+ note
+ end
+ end
+
+ class Agenda
+ DAYS_IN_LETTERS = {"d" => 1, "w" => 7, "m" => 30}
+
+ # TODO add filters to "where"
+ def initialize(notes_holder, date = nil, day_difference = nil, **filters)
+ @notes = flatten(notes_holder)#.each { |note| note.date = date }
+ filter(filters) if date.nil?
+ if date.nil?
+ #filter(**filters)
+ else
+ #check_date(date, day_difference)
+ end
+ end
+
+ def where(**filters)
+ Agenda.new(self, **filters)
+ end
+
+ #private
+ def flatten(note_array)
+ if note_array.notes == []
+ note_array
+ else
+ #note_array.reduce([]) { |array, note| array + flatten(note) }
+ end
+ end
+
+ def parse_scheduled(schedule)
+ split_schedule = schedule.split " "
+ days = 0
+ unless split_schedule[1].nil?
+ matches = split_schedule[1].match /\+(\d+)([dwm])/
+ days = matches[1].to_i * DAYS_IN_LETTERS[matches[2]]
+ end
+ [Date.new(split_schedule[0]), days]
+ end
+
+ def filter(**filters)
+ # TODO
+ end
+
+ def check_date(date, day_difference)
+ @notes.keep_if do |note|
+ note_date, note_delta = parse_scheduled(note.schedule)
+ (@date - note_date) % note_delta < @day_difference
+ end
+ end
+ end
+
+ def self.create_file(file_name, &block)
+ File.new(file_name, NoteBuilder.new(file_name).build(&block).notes)
+ end
+end

Мариян обнови решението на 21.12.2015 17:29 (преди около 9 години)

module LazyMode
class Date
attr_accessor :year, :month, :day
def initialize(date)
split_date = date.split "-"
@year = split_date[0].to_i
@month = split_date[1].to_i
@day = split_date[2].to_i
end
def -(date)
self_in_days = year * 12 * 30 + month * 30 + day
date_in_days = date.year * 12 * 30 + date.month * 30 + date.day
self_in_days - date_in_days
end
def <(date)
self - date < 0
end
def to_s
"%04d-%02d-%02d" % [@year, @month, @day]
end
end
class Note
attr_accessor :header, :file_name, :body, :status, :tags, :scheduled, :notes
+ attr_accessor :date
def initialize(header, file_name, status, tags)
@header = header
@file_name = file_name
@status = status
@tags = tags
end
end
class File
attr_accessor :name, :notes
def initialize(name, notes)
@name = name.dup
@notes = notes
end
def daily_agenda(date)
Agenda.new(self, date, 1)
end
def weekly_agenda(date)
Agenda.new(self, date, 7)
end
end
class NoteBuilder
def initialize(file_name, header = nil, tags = nil)
@notes = []
@file_name = file_name
@header = header
@tags = tags
@status = :topostpone
@body = ""
end
def status(symbol)
@status = symbol
end
def body(text)
@body = text
end
def scheduled(date)
@scheduled = date
end
def note(header, *tags, &block)
@notes << NoteBuilder.new(@file_name, header, tags).build(&block)
end
def build(&block)
instance_eval &block
note = Note.new(@header, @file_name, @status, @tags)
note.body = @body
note.scheduled = @scheduled
note.notes = @notes
note
end
end
class Agenda
+ attr_accessor :notes
+
DAYS_IN_LETTERS = {"d" => 1, "w" => 7, "m" => 30}
# TODO add filters to "where"
def initialize(notes_holder, date = nil, day_difference = nil, **filters)
- @notes = flatten(notes_holder)#.each { |note| note.date = date }
- filter(filters) if date.nil?
+ @notes = flatten(notes_holder.notes).each { |note| note.date = date }
+ p @notes.size
if date.nil?
- #filter(**filters)
+ filter(**filters)
else
- #check_date(date, day_difference)
+ check_date(date, day_difference)
end
end
def where(**filters)
Agenda.new(self, **filters)
end
#private
def flatten(note_array)
- if note_array.notes == []
- note_array
- else
- #note_array.reduce([]) { |array, note| array + flatten(note) }
+ return [] if note_array == []
+ note_array + note_array.reduce([]) do |array, note|
+ array + flatten(note.notes)
end
end
- def parse_scheduled(schedule)
- split_schedule = schedule.split " "
+ def parse_scheduled(scheduled)
+ split_scheduled = scheduled.split " "
days = 0
- unless split_schedule[1].nil?
- matches = split_schedule[1].match /\+(\d+)([dwm])/
+ unless split_scheduled[1].nil?
+ matches = split_scheduled[1].match /\+(\d+)([dwm])/
days = matches[1].to_i * DAYS_IN_LETTERS[matches[2]]
end
- [Date.new(split_schedule[0]), days]
+ [Date.new(split_scheduled[0]), days]
end
def filter(**filters)
# TODO
end
def check_date(date, day_difference)
@notes.keep_if do |note|
- note_date, note_delta = parse_scheduled(note.schedule)
- (@date - note_date) % note_delta < @day_difference
+ note_date, note_delta = *parse_scheduled(note.scheduled)
+ note_date < date and (date - note_date) % note_delta < day_difference
end
end
end
def self.create_file(file_name, &block)
File.new(file_name, NoteBuilder.new(file_name).build(&block).notes)
end
end