Решение на Четвърта задача от Мартин Попов

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

Към профила на Мартин Попов

Резултати

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

Код

class PrivacyFilter
attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
phone_filtered(email_filtered(text))
end
def phone_filtered(string)
if preserve_phone_country_code
local_format_phone_filtered(partially_inter_format_phone(string))
else local_format_phone_filtered(inter_format_phone_filtered(string))
end
end
def email_filtered(string)
if partially_preserve_email_username
hostname_filtered(partially_hostname_filtered(string))
elsif preserve_email_hostname then hostname_filtered(string)
else email_ordinary_filtered(string)
end
end
def local_format_phone_filtered(string)
string.gsub(/\b0([\s()-]{,2}\d){6,11}\b/, '[PHONE]')
end
def inter_format_phone_filtered(string)
string.gsub(/(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\b/, '[PHONE]')
end
def partially_inter_format_phone(string)
string.gsub(/((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\b/, "\\1 [FILTERED]")
end
def email_ordinary_filtered(string)
pattern = /\b[[:alnum:]][\w\+\.-]{,200}@ #hostname
(([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+ #domain
[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\b #TLD/x
string.gsub(pattern,'[EMAIL]')
end
def hostname_filtered(string)
pattern = /\b[[:alnum:]][\w\+\.-]{,200}#hostname
@((([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+ #domain
[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\b) #TLD/x
string.gsub(pattern, "[FILTERED]@\\1")
end
def partially_hostname_filtered(string)
pattern = /\b([[:alnum:]][\w\+\.-]{2})[\w\+\.-]{3,198} #hostname
(@((([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+ #domain
[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\b)) #TLD/x
string.gsub(pattern, "\\1[FILTERED]\\2")
end
end
module Validations
def self.email?(value)
return false unless email = /\A[[:alnum:]][\w\+\.-]{,200}@(?<hostname>.+)\z/.match(value)
self.hostname?(email[:hostname])
end
def self.phone?(value)
local_format = /\A0([\s()-]{,2}\d){6,11}\z/
inter_format = /\A(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\z/
return false unless value =~ local_format or value =~ inter_format
true
end
def self.hostname?(value)
hostname = /\A(?<domains>.+\.)[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\z/.match(value)
return false unless hostname
return false unless hostname[:domains] =~ /\A(([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+\z/
true
end
def self.ip_address?(value)
return false unless value =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/
if $1.to_i > 255 or $2.to_i > 255 or $3.to_i > 255 or $4.to_i > 255 then false
else true
end
end
def self.integer?(value)
value =~ /\A0\z|\A-?[1-9]\d*\z/
end
def self.number?(value)
value =~ /\A0\z|\A-?[1-9]\d*\z|\A-?[1-9]\d*\.\d+\z|\A-?0.\d+\z/
end
def self.date?(value)
return false unless value =~ /\A(\d{4})-(\d{2})-(\d{2})\z/
if $3.to_i > 31 or $2.to_i > 12 or $2.to_i < 1 or $3.to_i < 1 then false
else true
end
end
def self.time?(value)
return false unless value =~ /\A(\d{2}):(\d{2}):(\d{2})\z/
if $1.to_i > 23 or $2.to_i > 59 or $3.to_i > 59 then false
else true
end
end
def self.date_time?(value)
return false unless value =~ /\A(\S*)[T ](\S*)\z/
self.date? $1 and self.time? $2
end
end

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

.........F.........F.........F...........

Failures:

  1) PrivacyFilter does not filter invalid phone numbers
     Failure/Error: filter(text).should eq filtered
       
       expected: "Reach me at: 0885123"
            got: "Reach me at: [PHONE]"
       
       (compared using ==)
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:95:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Validations can validate more complex phone numbers
     Failure/Error: Validations.phone?(phone).should be(valid)
       
       expected #<FalseClass:0> => false
            got #<TrueClass:2> => true
       
       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       'actual.should eq(expected)' if you don't care about
       object identity in this example.
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:209:in `each'
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:209:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Validations validates more complex integers
     Failure/Error: Validations.integer?('-0').should be_true
       expected: true value
            got: nil
     # /tmp/d20130203-23049-1v1ihtf/spec.rb:289:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.04737 seconds
41 examples, 3 failures

Failed examples:

rspec /tmp/d20130203-23049-1v1ihtf/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1v1ihtf/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1v1ihtf/spec.rb:283 # Validations validates more complex integers

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

Мартин обнови решението на 25.11.2012 11:42 (преди почти 12 години)

+class PrivacyFilter
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text.split(/\s+/)
+ @preserve_phone_country_code = false
+ @preserve_email_hostname = false
+ @partially_preserve_email_username = false
+ end
+
+ def filtered
+ concat = lambda {|word,other| word + ' ' + other}
+ filter.inject &concat
+ end
+
+ def filter
+ @text.map do |word|
+ if Validations.email?(word) then email_filter(word)
+ elsif Validations.phone?(word) then phone_filter(word)
+ else word
+ end
+ end
+ end
+
+ def phone_filter(phone)
+ return "[PHONE]" unless preserve_phone_country_code
+ if /\A((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\z/ =~ phone then "#{$1} [FILTERED]"
+ else "[PHONE]"
+ end
+ end
+
+ def email_filter(email)
+ return "[EMAIL]" unless preserve_email_hostname or partially_preserve_email_username
+ if partially_preserve_email_username then partial_email_filter(email)
+ else email.sub(/\A.+(?=@)/,"[FILTERED]")
+ end
+ end
+
+ def partial_email_filter(word)
+ if word.match(/\A.{,5}(?=@)/) then word.sub(/\A.+(?=@)/,"[FILTERED]")
+ else word.sub(/(?<=...).+(?=@)/,"[FILTERED]")
+ end
+ end
+end
+
+module Validations
+ def self.email?(value)
+ return false unless email = /\A[[:alnum:]][\w\+\.-]{,200}@(?<hostname>.+)\z/.match(value)
+ self.hostname?(email[:hostname])
+ end
+
+ def self.phone?(value)
+ local_format = /\A0([\s()-]{,2}\d){6,11}\z/
+ inter_format = /\A(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\z/
+ return false unless value =~ local_format or value =~ inter_format
+ true
+ end
+
+ def self.hostname?(value)
+ hostname = /\A(?<domains>.+\.)[[:alpha:]]{2,3}(.[[:alpha:]]{2})?\z/.match(value)
+ return false unless hostname
+ return false unless hostname[:domains] =~ /\A(([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+\z/
+ true
+ end
+
+ def self.ip_address?(value)
+ return false unless value =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/
+ if $1.to_i > 255 or $2.to_i > 255 or $3.to_i > 255 or $4.to_i > 255 then false
+ else true
+ end
+ end
+
+ def self.integer?(value)
+ value =~ /\A0\z|\A-?[1-9]\d*\z/
+ end
+
+ def self.number?(value)
+ value =~ /\A0\z|\A-?[1-9]\d*\z|\A-?[1-9]\d*\.\d+\z|\A-?0.\d+\z/
+ end
+
+ def self.date?(value)
+ return false unless value =~ /\A(\d{4})-(\d{2})-(\d{2})\z/
+ if $3.to_i > 31 or $2.to_i > 12 or $2.to_i < 1 or $3.to_i < 1 then false
+ else true
+ end
+ end
+
+ def self.time?(value)
+ return false unless value =~ /\A(\d{1,2}):(\d{2}):(\d{2})\z/
+ if $1.to_i > 23 or $2.to_i > 59 or $3.to_i > 59 then false
+ else true
+ end
+ end
+
+ def self.date_time?(value)
+ return false unless value =~ /\A(\S*)[T ](\S*)\z/
+ self.date? $1 and self.time? $2
+ end
+end

Мартин обнови решението на 25.11.2012 11:45 (преди почти 12 години)

class PrivacyFilter
- attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+ attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
- def initialize(text)
- @text = text.split(/\s+/)
- @preserve_phone_country_code = false
- @preserve_email_hostname = false
- @partially_preserve_email_username = false
- end
+ def initialize(text)
+ @text = text.split(/\s+/)
+ @preserve_phone_country_code = false
+ @preserve_email_hostname = false
+ @partially_preserve_email_username = false
+ end
- def filtered
- concat = lambda {|word,other| word + ' ' + other}
- filter.inject &concat
- end
+ def filtered
+ concat = lambda {|word,other| word + ' ' + other}
+ filter.inject &concat
+ end
- def filter
- @text.map do |word|
- if Validations.email?(word) then email_filter(word)
- elsif Validations.phone?(word) then phone_filter(word)
- else word
- end
- end
- end
+ def filter
+ @text.map do |word|
+ if Validations.email?(word) then email_filter(word)
+ elsif Validations.phone?(word) then phone_filter(word)
+ else word
+ end
+ end
+ end
- def phone_filter(phone)
- return "[PHONE]" unless preserve_phone_country_code
- if /\A((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\z/ =~ phone then "#{$1} [FILTERED]"
- else "[PHONE]"
- end
- end
+ def phone_filter(phone)
+ return "[PHONE]" unless preserve_phone_country_code
+ if /\A((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\z/ =~ phone then "#{$1} [FILTERED]"
+ else "[PHONE]"
+ end
+ end
- def email_filter(email)
- return "[EMAIL]" unless preserve_email_hostname or partially_preserve_email_username
- if partially_preserve_email_username then partial_email_filter(email)
- else email.sub(/\A.+(?=@)/,"[FILTERED]")
- end
- end
+ def email_filter(email)
+ return "[EMAIL]" unless preserve_email_hostname or partially_preserve_email_username
+ if partially_preserve_email_username then partial_email_filter(email)
+ else email.sub(/\A.+(?=@)/,"[FILTERED]")
+ end
+ end
- def partial_email_filter(word)
- if word.match(/\A.{,5}(?=@)/) then word.sub(/\A.+(?=@)/,"[FILTERED]")
- else word.sub(/(?<=...).+(?=@)/,"[FILTERED]")
- end
- end
+ def partial_email_filter(word)
+ if word.match(/\A.{,5}(?=@)/) then word.sub(/\A.+(?=@)/,"[FILTERED]")
+ else word.sub(/(?<=...).+(?=@)/,"[FILTERED]")
+ end
+ end
end
module Validations
- def self.email?(value)
- return false unless email = /\A[[:alnum:]][\w\+\.-]{,200}@(?<hostname>.+)\z/.match(value)
- self.hostname?(email[:hostname])
- end
+ def self.email?(value)
+ return false unless email = /\A[[:alnum:]][\w\+\.-]{,200}@(?<hostname>.+)\z/.match(value)
+ self.hostname?(email[:hostname])
+ end
- def self.phone?(value)
- local_format = /\A0([\s()-]{,2}\d){6,11}\z/
- inter_format = /\A(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\z/
- return false unless value =~ local_format or value =~ inter_format
- true
- end
+ def self.phone?(value)
+ local_format = /\A0([\s()-]{,2}\d){6,11}\z/
+ inter_format = /\A(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\z/
+ return false unless value =~ local_format or value =~ inter_format
+ true
+ end
- def self.hostname?(value)
- hostname = /\A(?<domains>.+\.)[[:alpha:]]{2,3}(.[[:alpha:]]{2})?\z/.match(value)
- return false unless hostname
- return false unless hostname[:domains] =~ /\A(([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+\z/
- true
- end
+ def self.hostname?(value)
+ hostname = /\A(?<domains>.+\.)[[:alpha:]]{2,3}(.[[:alpha:]]{2})?\z/.match(value)
+ return false unless hostname
+ return false unless hostname[:domains] =~ /\A(([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+\z/
+ true
+ end
- def self.ip_address?(value)
- return false unless value =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/
- if $1.to_i > 255 or $2.to_i > 255 or $3.to_i > 255 or $4.to_i > 255 then false
- else true
- end
- end
+ def self.ip_address?(value)
+ return false unless value =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/
+ if $1.to_i > 255 or $2.to_i > 255 or $3.to_i > 255 or $4.to_i > 255 then false
+ else true
+ end
+ end
- def self.integer?(value)
- value =~ /\A0\z|\A-?[1-9]\d*\z/
- end
+ def self.integer?(value)
+ value =~ /\A0\z|\A-?[1-9]\d*\z/
+ end
- def self.number?(value)
- value =~ /\A0\z|\A-?[1-9]\d*\z|\A-?[1-9]\d*\.\d+\z|\A-?0.\d+\z/
- end
+ def self.number?(value)
+ value =~ /\A0\z|\A-?[1-9]\d*\z|\A-?[1-9]\d*\.\d+\z|\A-?0.\d+\z/
+ end
- def self.date?(value)
- return false unless value =~ /\A(\d{4})-(\d{2})-(\d{2})\z/
- if $3.to_i > 31 or $2.to_i > 12 or $2.to_i < 1 or $3.to_i < 1 then false
- else true
- end
- end
+ def self.date?(value)
+ return false unless value =~ /\A(\d{4})-(\d{2})-(\d{2})\z/
+ if $3.to_i > 31 or $2.to_i > 12 or $2.to_i < 1 or $3.to_i < 1 then false
+ else true
+ end
+ end
- def self.time?(value)
- return false unless value =~ /\A(\d{1,2}):(\d{2}):(\d{2})\z/
- if $1.to_i > 23 or $2.to_i > 59 or $3.to_i > 59 then false
- else true
- end
- end
+ def self.time?(value)
+ return false unless value =~ /\A(\d{1,2}):(\d{2}):(\d{2})\z/
+ if $1.to_i > 23 or $2.to_i > 59 or $3.to_i > 59 then false
+ else true
+ end
+ end
- def self.date_time?(value)
- return false unless value =~ /\A(\S*)[T ](\S*)\z/
- self.date? $1 and self.time? $2
- end
+ def self.date_time?(value)
+ return false unless value =~ /\A(\S*)[T ](\S*)\z/
+ self.date? $1 and self.time? $2
+ end
end

Мартин обнови решението на 27.11.2012 23:19 (преди почти 12 години)

class PrivacyFilter
- attr_accessor :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+ attr_accessor :text, :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
- @text = text.split(/\s+/)
+ @text = text
@preserve_phone_country_code = false
@preserve_email_hostname = false
@partially_preserve_email_username = false
end
def filtered
- concat = lambda {|word,other| word + ' ' + other}
- filter.inject &concat
+ phone_filtered(email_filtered(text))
end
- def filter
- @text.map do |word|
- if Validations.email?(word) then email_filter(word)
- elsif Validations.phone?(word) then phone_filter(word)
- else word
- end
+ def phone_filtered(string)
+ if preserve_phone_country_code
+ local_format_phone_filtered(partially_inter_format_phone(string))
+ else local_format_phone_filtered(inter_format_phone_filtered(string))
end
end
- def phone_filter(phone)
- return "[PHONE]" unless preserve_phone_country_code
- if /\A((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\z/ =~ phone then "#{$1} [FILTERED]"
- else "[PHONE]"
+ def email_filtered(string)
+ if partially_preserve_email_username
+ hostname_filtered(partially_hostname_filtered(string))
+ elsif preserve_email_hostname then hostname_filtered(string)
+ else email_ordinary_filtered(string)
end
end
- def email_filter(email)
- return "[EMAIL]" unless preserve_email_hostname or partially_preserve_email_username
- if partially_preserve_email_username then partial_email_filter(email)
- else email.sub(/\A.+(?=@)/,"[FILTERED]")
- end
+ def local_format_phone_filtered(string)
+ string.gsub(/\b0([\s()-]{,2}\d){6,11}\b/, '[PHONE]')
end
- def partial_email_filter(word)
- if word.match(/\A.{,5}(?=@)/) then word.sub(/\A.+(?=@)/,"[FILTERED]")
- else word.sub(/(?<=...).+(?=@)/,"[FILTERED]")
- end
+ def inter_format_phone_filtered(string)
+ string.gsub(/(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\b/, '[PHONE]')
end
+
+ def partially_inter_format_phone(string)
+ string.gsub(/((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\b/, "\\1 [FILTERED]")
+ end
+
+ def email_ordinary_filtered(string)
+ pattern = /\b[[:alnum:]][\w\+\.-]{,200}@ #hostname
+ (([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+ #domain
+ [[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\b #TLD/x
+ string.gsub(pattern,'[EMAIL]')
+ end
+
+ def hostname_filtered(string)
+ pattern = /\b[[:alnum:]][\w\+\.-]{,200}#hostname
+ @((([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+ #domain
+ [[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\b) #TLD/x
+ string.gsub(pattern, "[FILTERED]@\\1")
+ end
+
+ def partially_hostname_filtered(string)
+ pattern = /\b([[:alnum:]][\w\+\.-]{2})[\w\+\.-]{3,198} #hostname
+ (@((([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+ #domain
+ [[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\b)) #TLD/x
+ string.gsub(pattern, "\\1[FILTERED]\\2")
+ end
end
module Validations
def self.email?(value)
return false unless email = /\A[[:alnum:]][\w\+\.-]{,200}@(?<hostname>.+)\z/.match(value)
self.hostname?(email[:hostname])
end
def self.phone?(value)
local_format = /\A0([\s()-]{,2}\d){6,11}\z/
inter_format = /\A(00|\+)[1-9]\d{0,2}([\s()-]{,2}\d){6,11}\z/
return false unless value =~ local_format or value =~ inter_format
true
end
def self.hostname?(value)
- hostname = /\A(?<domains>.+\.)[[:alpha:]]{2,3}(.[[:alpha:]]{2})?\z/.match(value)
+ hostname = /\A(?<domains>.+\.)[[:alpha:]]{2,3}(\.[[:alpha:]]{2})?\z/.match(value)
return false unless hostname
return false unless hostname[:domains] =~ /\A(([A-Za-z\d]\.)|([A-Za-z\d][-\dA-Za-z]{,60}[A-Za-z\d])\.)+\z/
true
end
def self.ip_address?(value)
return false unless value =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/
if $1.to_i > 255 or $2.to_i > 255 or $3.to_i > 255 or $4.to_i > 255 then false
else true
end
end
def self.integer?(value)
value =~ /\A0\z|\A-?[1-9]\d*\z/
end
def self.number?(value)
value =~ /\A0\z|\A-?[1-9]\d*\z|\A-?[1-9]\d*\.\d+\z|\A-?0.\d+\z/
end
def self.date?(value)
return false unless value =~ /\A(\d{4})-(\d{2})-(\d{2})\z/
if $3.to_i > 31 or $2.to_i > 12 or $2.to_i < 1 or $3.to_i < 1 then false
else true
end
end
def self.time?(value)
- return false unless value =~ /\A(\d{1,2}):(\d{2}):(\d{2})\z/
+ return false unless value =~ /\A(\d{2}):(\d{2}):(\d{2})\z/
if $1.to_i > 23 or $2.to_i > 59 or $3.to_i > 59 then false
else true
end
end
def self.date_time?(value)
return false unless value =~ /\A(\S*)[T ](\S*)\z/
self.date? $1 and self.time? $2
end
end