Решение на Седма задача от Георги Стефанов

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

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

Резултати

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

Код

class LazyMode
def self.create_file(file_name, &block)
file = File.new(file_name)
file.instance_eval(&block)
file
end
class Date
@@period_table = {"d" => 1, "w" => 7, "m" => 30}
attr_accessor :year, :month, :day
def self.to_date(days)
the_day = days % 30 == 0 ? 30 : days % 30
the_month = sprintf('%02d', (((days - the_day) / 30) + 1) % 12)
the_year = sprintf('%04d', ((days - the_day) / 360 + 1))
the_month = "12" if the_month == "00"
Date.new("#{the_year}-#{the_month}-#{sprintf('%02d', the_day)}")
end
def initialize(date_string)
@year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
@year, @month, @day = [@year, @month, @day].map { |time| time.to_i }
@date_string = date_string[0..9]
end
def to_s
@date_string
end
def ===(date)
difference = date.to_days - self.to_days
return false if difference < 0
period ? difference % period == 0 : difference == 0
end
def period
return nil if @repetition == nil
jump = @repetition.scan(/\d*/).join("").to_i
type = @repetition.match(/[dwm]/).to_s
jump * @@period_table[type]
end
def to_days
(year - 1) * 360 + (month - 1) * 30 + day
end
def within_week(date)
occurs_on = [0,1,2,3,4,5,6].map { |day| date.to_days + day }
.map{ |days| Date.to_date(days) }
.delete_if { |date| !(self === date) }
occurs_on
end
end
class File
attr_accessor :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(self, header)
new_note.tags = tags
new_note.instance_eval(&block)
@notes.push(new_note)
new_note
end
def daily_agenda(date)
agenda = File.new("daily_agenda_'#{date.to_s}'")
agenda.notes = notes.map { |note| note.dup }
agenda.notes.delete_if { |note| !(note.date === date) }
agenda.notes.each { |note| note.date = date }
agenda
end
def weekly_agenda(date)
agenda = File.new("weekly_agenda_'#{date.to_s}'")
agenda.notes = notes.map { |note| note.weekly_occurrence(date) }
agenda.notes.flatten!
agenda
end
def filter_notes(tag: nil, text: nil, status: nil)
filter = notes.reject { |note| !note.tags.include?(tag) and tag }
filter.delete_if { |note| note.status != status and status }
filter.delete_if do |note|
(note.body =~ text) == nil and (note.header =~ text) == nil and text
end
filter
end
def where(tag: nil, text: nil, status: nil)
filter = File.new("filter")
filter.notes = filter_notes(tag: tag, text: text, status: status)
filter
end
end
class Note
attr_accessor :tags, :file, :header, :date
def initialize(file, header)
@file = file
@header = header
@status = :topostpone
@body = ""
end
def body(new_body = nil)
@body = new_body if new_body
@body
end
def file_name
@file.name
end
def status(new_status = nil)
@status = new_status if new_status
@status
end
def note(header, *tags, &block)
new_note = Note.new(@file, header)
new_note.tags = tags
new_note.instance_eval(&block)
@file.notes.push(new_note)
new_note
end
def weekly_occurrence(date)
occurrence = self.date.within_week(date)
note_array = occurrence.map { |date| self.schedule_on(date) }
note_array
end
def schedule_on(date)
new_note = self.dup
new_note.date = date
new_note
end
def scheduled(date)
@date = Date.new(date)
end
end
end

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

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

Finished in 0.02033 seconds
31 examples, 0 failures

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

Георги обнови решението на 20.12.2015 04:27 (преди над 8 години)

+class LazyMode
+ def self.create_file(file_name, &block)
+ file = File.new(file_name)
+ file.instance_eval(&block)
+ file
+ end
+
+
+ class Date
+ @@to_days_table = {"d" => 1, "w" => 7, "m" => 30}
+
+ def initialize(date_string)
+ @year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
+ @date_string = date_string[0..9]
+ end
+
+ def year
+ @year.to_i
+ end
+
+ def month
+ @month.to_i
+ end
+
+ def day
+ @day.to_i
+ end
+
+ def to_s
+ @date_string
+ end
+
+ def period
+ return nil if @repetition == nil
+ jump = @repetition.scan(/\d*/).join("").to_i
+ type = @repetition.match(/[dwm]/).to_s
+
+ jump * @@to_days_table[type]
+ end
+ def -(date)
+ days_now = (year - 1) * 360 + (month - 1) * 30 + day
+ days_date = (date.year - 1) * 360 + (date.month - 1) * 30 + date.day
+
+ days_date - days_now
+ end
+ def ===(date)
+ difference = self - date
+ return false if difference < 0
+
+ period ? difference % period == 0 : difference == 0
+ end
+ end
+
+
+
+
+ class File
+ attr_accessor :name, :notes
+
+ def initialize(name)
+ @name = name
+ @notes = []
+ end
+
+ def note(header, *tags, &block)
+ new_note = Note.new(self, header)
+ new_note.tags = tags
+ new_note.instance_eval(&block)
+ @notes.push(new_note)
+ new_note
+ end
+
+ def daily_agenda(date)
+ dummy_file = File.new("daily_agenda_'#{date.to_s}'")
+ dummy_file.notes = notes.map { |note| note.dup }
+ dummy_file.notes.delete_if { |note| !(note.date === date) }
+ dummy_file.notes.each { |note| note.date = date }
+
+ dummy_file
+ end
+
+
+ def weekly_agenda(date)
+ dummy_file = File.new("daily_agenda_'#{date.to_s}'")
+ dummy_file.notes = notes.select do |note|
+ (date - note.date).between?(0,6)
+ end
+
+ dummy_file
+ end
+
+ def filter_notes(tag: nil, text: nil, status: nil)
+
+ filtered = notes.reject { |note| !note.tags.include?(tag) and tag }
+ filtered = filtered.reject { |note| note.status != status and status }
+ filtered = filtered.reject do |note|
+ (note.body =~ text) == nil and (note.header =~ text) == nil and text
+ end
+ filtered
+ end
+
+ def where(tag: nil, text: nil, status: nil)
+ dummy_file = File.new("filter")
+ dummy_file.notes = filter_notes(tag: tag, text: text, status: status)
+ dummy_file
+ end
+ end
+
+
+
+ class Note
+ attr_accessor :tags, :file, :header, :date
+ def initialize(file, header)
+ @file = file
+ @header = header
+ @status = :topostpone
+ @body = ""
+ end
+ def body(new_body = nil)
+ @body = new_body if new_body
+ @body
+ end
+ def file_name
+ @file.name
+ end
+ def status(new_status = nil)
+ @status = new_status if new_status
+ @status
+ end
+ def note(header, *tags, &block)
+ new_note = Note.new(@file, header)
+ new_note.tags = tags
+ new_note.instance_eval(&block)
+ @file.notes.push(new_note)
+ new_note
+ end
+
+ def scheduled(date)
+ @date = Date.new(date)
+ end
+ end
+end

Георги обнови решението на 20.12.2015 22:28 (преди над 8 години)

class LazyMode
def self.create_file(file_name, &block)
file = File.new(file_name)
file.instance_eval(&block)
file
end
- class Date
+ class Date
@@to_days_table = {"d" => 1, "w" => 7, "m" => 30}
- def initialize(date_string)
- @year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
- @date_string = date_string[0..9]
+ attr_accessor :year, :month, :day
+
+ def self.to_date(days)
+ the_day = days % 30 == 0 ? 30 : days % 30
+ the_month = sprintf('%02d', (((days - the_day) / 30) + 1) % 12)
+ the_year = sprintf('%04d', ((days - the_day) / 360 + 1))
+ the_month = "01" if the_month == "00"
+
+ Date.new("#{the_year}-#{the_month}-#{sprintf('%02d', the_day)}")
end
- def year
- @year.to_i
+ def to_days
+ (year - 1) * 360 + (month - 1) * 30 + day
end
- def month
- @month.to_i
+ def initialize(date_string)
+ @year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
+ @year, @month, @day = [@year, @month, @day].map { |time| time.to_i }
+ @date_string = date_string[0..9]
end
- def day
- @day.to_i
+ def within_week?(date)
+ [0,1,2,3,4,5,6].map {|day| date.to_days + day}
+ .map{|days| Date.to_date(days) }
+ .any? {|date| self === date}
end
def to_s
@date_string
end
def period
return nil if @repetition == nil
jump = @repetition.scan(/\d*/).join("").to_i
type = @repetition.match(/[dwm]/).to_s
jump * @@to_days_table[type]
end
- def -(date)
- days_now = (year - 1) * 360 + (month - 1) * 30 + day
- days_date = (date.year - 1) * 360 + (date.month - 1) * 30 + date.day
- days_date - days_now
- end
def ===(date)
- difference = self - date
+ difference = date.to_days - self.to_days
return false if difference < 0
period ? difference % period == 0 : difference == 0
end
end
- class File
+ class File
attr_accessor :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(self, header)
new_note.tags = tags
new_note.instance_eval(&block)
@notes.push(new_note)
new_note
end
def daily_agenda(date)
dummy_file = File.new("daily_agenda_'#{date.to_s}'")
dummy_file.notes = notes.map { |note| note.dup }
dummy_file.notes.delete_if { |note| !(note.date === date) }
dummy_file.notes.each { |note| note.date = date }
dummy_file
end
def weekly_agenda(date)
dummy_file = File.new("daily_agenda_'#{date.to_s}'")
- dummy_file.notes = notes.select do |note|
- (date - note.date).between?(0,6)
- end
+ dummy_file.notes = notes.map { |note| note.dup }
+ dummy_file.notes.delete_if { |note| !(note.date.within_week?(date))}
dummy_file
end
def filter_notes(tag: nil, text: nil, status: nil)
filtered = notes.reject { |note| !note.tags.include?(tag) and tag }
filtered = filtered.reject { |note| note.status != status and status }
filtered = filtered.reject do |note|
(note.body =~ text) == nil and (note.header =~ text) == nil and text
end
filtered
end
def where(tag: nil, text: nil, status: nil)
dummy_file = File.new("filter")
dummy_file.notes = filter_notes(tag: tag, text: text, status: status)
dummy_file
end
end
class Note
attr_accessor :tags, :file, :header, :date
+
def initialize(file, header)
@file = file
@header = header
@status = :topostpone
@body = ""
end
+
def body(new_body = nil)
@body = new_body if new_body
@body
end
+
def file_name
@file.name
end
+
def status(new_status = nil)
@status = new_status if new_status
@status
end
+
def note(header, *tags, &block)
new_note = Note.new(@file, header)
new_note.tags = tags
new_note.instance_eval(&block)
@file.notes.push(new_note)
new_note
end
def scheduled(date)
@date = Date.new(date)
end
end
end

Георги обнови решението на 21.12.2015 12:06 (преди над 8 години)

class LazyMode
def self.create_file(file_name, &block)
file = File.new(file_name)
file.instance_eval(&block)
+
file
end
class Date
- @@to_days_table = {"d" => 1, "w" => 7, "m" => 30}
+ @@period_table = {"d" => 1, "w" => 7, "m" => 30}
attr_accessor :year, :month, :day
def self.to_date(days)
the_day = days % 30 == 0 ? 30 : days % 30
the_month = sprintf('%02d', (((days - the_day) / 30) + 1) % 12)
the_year = sprintf('%04d', ((days - the_day) / 360 + 1))
- the_month = "01" if the_month == "00"
+ the_month = "12" if the_month == "00"
Date.new("#{the_year}-#{the_month}-#{sprintf('%02d', the_day)}")
end
- def to_days
- (year - 1) * 360 + (month - 1) * 30 + day
- end
-
def initialize(date_string)
@year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
@year, @month, @day = [@year, @month, @day].map { |time| time.to_i }
@date_string = date_string[0..9]
end
- def within_week?(date)
- [0,1,2,3,4,5,6].map {|day| date.to_days + day}
- .map{|days| Date.to_date(days) }
- .any? {|date| self === date}
- end
-
def to_s
@date_string
end
+ def ===(date)
+ difference = date.to_days - self.to_days
+ return false if difference < 0
+
+ period ? difference % period == 0 : difference == 0
+ end
+
def period
return nil if @repetition == nil
jump = @repetition.scan(/\d*/).join("").to_i
type = @repetition.match(/[dwm]/).to_s
- jump * @@to_days_table[type]
+ jump * @@period_table[type]
end
- def ===(date)
- difference = date.to_days - self.to_days
- return false if difference < 0
+ def to_days
+ (year - 1) * 360 + (month - 1) * 30 + day
+ end
- period ? difference % period == 0 : difference == 0
+ def within_week?(date)
+ [0,1,2,3,4,5,6].map {|day| date.to_days + day}
+ .map{|days| Date.to_date(days) }
+ .any? {|date| self === date}
end
end
-
-
class File
attr_accessor :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(self, header)
new_note.tags = tags
new_note.instance_eval(&block)
@notes.push(new_note)
+
new_note
end
def daily_agenda(date)
- dummy_file = File.new("daily_agenda_'#{date.to_s}'")
- dummy_file.notes = notes.map { |note| note.dup }
- dummy_file.notes.delete_if { |note| !(note.date === date) }
- dummy_file.notes.each { |note| note.date = date }
+ agenda = File.new("daily_agenda_'#{date.to_s}'")
+ agenda.notes = notes.map { |note| note.dup }
+ agenda.notes.delete_if { |note| !(note.date === date) }
+ agenda.notes.each { |note| note.date = date }
- dummy_file
+ agenda
end
def weekly_agenda(date)
- dummy_file = File.new("daily_agenda_'#{date.to_s}'")
- dummy_file.notes = notes.map { |note| note.dup }
- dummy_file.notes.delete_if { |note| !(note.date.within_week?(date))}
+ agenda = File.new("weekly_agenda_'#{date.to_s}'")
+ agenda.notes = notes.map { |note| note.dup }
+ agenda.notes.delete_if { |note| !(note.date.within_week?(date)) }
- dummy_file
+ agenda
end
def filter_notes(tag: nil, text: nil, status: nil)
-
- filtered = notes.reject { |note| !note.tags.include?(tag) and tag }
- filtered = filtered.reject { |note| note.status != status and status }
- filtered = filtered.reject do |note|
+ filter = notes.reject { |note| !note.tags.include?(tag) and tag }
+ filter.delete_if { |note| note.status != status and status }
+ filter.delete_if do |note|
(note.body =~ text) == nil and (note.header =~ text) == nil and text
end
- filtered
+
+ filter
end
def where(tag: nil, text: nil, status: nil)
- dummy_file = File.new("filter")
- dummy_file.notes = filter_notes(tag: tag, text: text, status: status)
- dummy_file
+ filter = File.new("filter")
+ filter.notes = filter_notes(tag: tag, text: text, status: status)
+
+ filter
end
end
-
class Note
attr_accessor :tags, :file, :header, :date
def initialize(file, header)
@file = file
@header = header
@status = :topostpone
@body = ""
end
def body(new_body = nil)
@body = new_body if new_body
+
@body
end
def file_name
@file.name
end
def status(new_status = nil)
@status = new_status if new_status
+
@status
end
def note(header, *tags, &block)
new_note = Note.new(@file, header)
new_note.tags = tags
new_note.instance_eval(&block)
@file.notes.push(new_note)
+
new_note
end
def scheduled(date)
@date = Date.new(date)
end
end
end

Георги обнови решението на 21.12.2015 13:50 (преди над 8 години)

class LazyMode
def self.create_file(file_name, &block)
file = File.new(file_name)
file.instance_eval(&block)
file
end
class Date
@@period_table = {"d" => 1, "w" => 7, "m" => 30}
attr_accessor :year, :month, :day
def self.to_date(days)
the_day = days % 30 == 0 ? 30 : days % 30
the_month = sprintf('%02d', (((days - the_day) / 30) + 1) % 12)
the_year = sprintf('%04d', ((days - the_day) / 360 + 1))
the_month = "12" if the_month == "00"
Date.new("#{the_year}-#{the_month}-#{sprintf('%02d', the_day)}")
end
def initialize(date_string)
@year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
@year, @month, @day = [@year, @month, @day].map { |time| time.to_i }
@date_string = date_string[0..9]
end
def to_s
@date_string
end
def ===(date)
difference = date.to_days - self.to_days
return false if difference < 0
period ? difference % period == 0 : difference == 0
end
def period
return nil if @repetition == nil
jump = @repetition.scan(/\d*/).join("").to_i
type = @repetition.match(/[dwm]/).to_s
jump * @@period_table[type]
end
def to_days
(year - 1) * 360 + (month - 1) * 30 + day
end
- def within_week?(date)
- [0,1,2,3,4,5,6].map {|day| date.to_days + day}
- .map{|days| Date.to_date(days) }
- .any? {|date| self === date}
+ def within_week(date)
+ occurs_on = [0,1,2,3,4,5,6].map {|day| date.to_days + day }
+ .map{|days| Date.to_date(days) }
+ .delete_if {|date| !(self === date) }
+ occurs_on
end
end
class File
attr_accessor :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(self, header)
new_note.tags = tags
new_note.instance_eval(&block)
@notes.push(new_note)
new_note
end
def daily_agenda(date)
agenda = File.new("daily_agenda_'#{date.to_s}'")
agenda.notes = notes.map { |note| note.dup }
agenda.notes.delete_if { |note| !(note.date === date) }
agenda.notes.each { |note| note.date = date }
agenda
end
def weekly_agenda(date)
agenda = File.new("weekly_agenda_'#{date.to_s}'")
- agenda.notes = notes.map { |note| note.dup }
- agenda.notes.delete_if { |note| !(note.date.within_week?(date)) }
+ agenda.notes = notes.map { |note| note.weekly_occurrence(date) }
+ agenda.notes.flatten!
agenda
end
def filter_notes(tag: nil, text: nil, status: nil)
filter = notes.reject { |note| !note.tags.include?(tag) and tag }
filter.delete_if { |note| note.status != status and status }
filter.delete_if do |note|
(note.body =~ text) == nil and (note.header =~ text) == nil and text
end
filter
end
def where(tag: nil, text: nil, status: nil)
filter = File.new("filter")
filter.notes = filter_notes(tag: tag, text: text, status: status)
filter
end
end
-
class Note
attr_accessor :tags, :file, :header, :date
def initialize(file, header)
@file = file
@header = header
@status = :topostpone
@body = ""
end
def body(new_body = nil)
@body = new_body if new_body
@body
end
def file_name
@file.name
end
def status(new_status = nil)
@status = new_status if new_status
@status
end
def note(header, *tags, &block)
new_note = Note.new(@file, header)
new_note.tags = tags
new_note.instance_eval(&block)
@file.notes.push(new_note)
new_note
+ end
+
+ def weekly_occurrence(date)
+ occurrence = self.date.within_week(date)
+ note_array = occurrence.map { |date| self.schedule_on(date) }
+
+ note_array
+ end
+
+ def schedule_on(date)
+ new_note = self.dup
+ new_note.date = date
+
+ new_note
end
def scheduled(date)
@date = Date.new(date)
end
end
end

Георги обнови решението на 21.12.2015 15:35 (преди над 8 години)

class LazyMode
def self.create_file(file_name, &block)
file = File.new(file_name)
file.instance_eval(&block)
file
end
class Date
@@period_table = {"d" => 1, "w" => 7, "m" => 30}
attr_accessor :year, :month, :day
def self.to_date(days)
the_day = days % 30 == 0 ? 30 : days % 30
the_month = sprintf('%02d', (((days - the_day) / 30) + 1) % 12)
the_year = sprintf('%04d', ((days - the_day) / 360 + 1))
the_month = "12" if the_month == "00"
Date.new("#{the_year}-#{the_month}-#{sprintf('%02d', the_day)}")
end
def initialize(date_string)
@year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
@year, @month, @day = [@year, @month, @day].map { |time| time.to_i }
@date_string = date_string[0..9]
end
def to_s
@date_string
end
def ===(date)
difference = date.to_days - self.to_days
return false if difference < 0
period ? difference % period == 0 : difference == 0
end
def period
return nil if @repetition == nil
jump = @repetition.scan(/\d*/).join("").to_i
type = @repetition.match(/[dwm]/).to_s
jump * @@period_table[type]
end
def to_days
(year - 1) * 360 + (month - 1) * 30 + day
end
def within_week(date)
- occurs_on = [0,1,2,3,4,5,6].map {|day| date.to_days + day }
- .map{|days| Date.to_date(days) }
- .delete_if {|date| !(self === date) }
+ occurs_on = [0,1,2,3,4,5,6].map { |day| date.to_days + day }
+ .map{ |days| Date.to_date(days) }
+ .delete_if { |date| !(self === date) }
occurs_on
end
end
class File
attr_accessor :name, :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
new_note = Note.new(self, header)
new_note.tags = tags
new_note.instance_eval(&block)
@notes.push(new_note)
new_note
end
def daily_agenda(date)
agenda = File.new("daily_agenda_'#{date.to_s}'")
agenda.notes = notes.map { |note| note.dup }
agenda.notes.delete_if { |note| !(note.date === date) }
agenda.notes.each { |note| note.date = date }
agenda
end
def weekly_agenda(date)
agenda = File.new("weekly_agenda_'#{date.to_s}'")
agenda.notes = notes.map { |note| note.weekly_occurrence(date) }
agenda.notes.flatten!
agenda
end
def filter_notes(tag: nil, text: nil, status: nil)
filter = notes.reject { |note| !note.tags.include?(tag) and tag }
filter.delete_if { |note| note.status != status and status }
filter.delete_if do |note|
(note.body =~ text) == nil and (note.header =~ text) == nil and text
end
filter
end
def where(tag: nil, text: nil, status: nil)
filter = File.new("filter")
filter.notes = filter_notes(tag: tag, text: text, status: status)
filter
end
end
class Note
attr_accessor :tags, :file, :header, :date
def initialize(file, header)
@file = file
@header = header
@status = :topostpone
@body = ""
end
def body(new_body = nil)
@body = new_body if new_body
@body
end
def file_name
@file.name
end
def status(new_status = nil)
@status = new_status if new_status
@status
end
def note(header, *tags, &block)
new_note = Note.new(@file, header)
new_note.tags = tags
new_note.instance_eval(&block)
@file.notes.push(new_note)
new_note
end
def weekly_occurrence(date)
occurrence = self.date.within_week(date)
note_array = occurrence.map { |date| self.schedule_on(date) }
note_array
end
def schedule_on(date)
new_note = self.dup
new_note.date = date
new_note
end
def scheduled(date)
@date = Date.new(date)
end
end
end