On Wed, Feb 11, 2009 at 11:22 AM, Christoph B. [email protected] wrote:
This was a nice day
I tried using zsh functionality to output this, but it doesn’t seem that
easy.
Does anyone know an easy way how to do this in ruby?
There was a similar question on this list recently, I’ll try to find
it later for reference. For a ruby quiz
(Ruby Quiz - Reverse the Polarity (#143)) I built a regexp “generator”. It
adds a method to the Regexp to generate all possible strings that
match the regexp. If you use my code:
irb(main):001:0> require ‘quiz143’
=> true
irb(main):002:0> a = “This {is|was} a {good|nice} day”
=> “This {is|was} a {good|nice} day”
irb(main):006:0> s = a.gsub(/{(.*?)}/, “(\1)”)
=> “This (is|was) a (good|nice) day”
irb(main):008:0> Regexp.new(s).generate
=> [“This is a good day”, “This is a nice day”, “This was a good day”,
“This was a nice day”]
I’m changing your string to a valid regexp, changing all occurrences
of {} with () (depending on your case you might want to change that).
Then I create a regexp with that and call my method.
This supports many constructs from regexps, which might be overkill
for your problem. For sure there will be easier solutions for your
specific case. Anyway, I hope you find it interesting.
Wow, this is really useful! Especially in a general way like that.
Thanks a lot for the quick and helpful answer!
greetings,
Christoph
Jesús Gabriel y Galán wrote:
On Wed, Feb 11, 2009 at 11:22 AM, Christoph B. [email protected] wrote:
This was a nice day
I tried using zsh functionality to output this, but it doesn’t seem that
easy.
Does anyone know an easy way how to do this in ruby?
There was a similar question on this list recently, I’ll try to find
it later for reference. For a ruby quiz
(Ruby Quiz - Reverse the Polarity (#143)) I built a regexp “generator”. It
adds a method to the Regexp to generate all possible strings that
match the regexp. If you use my code:
irb(main):001:0> require ‘quiz143’
=> true
irb(main):002:0> a = “This {is|was} a {good|nice} day”
=> “This {is|was} a {good|nice} day”
irb(main):006:0> s = a.gsub(/{(.*?)}/, “(\1)”)
=> “This (is|was) a (good|nice) day”
irb(main):008:0> Regexp.new(s).generate
=> [“This is a good day”, “This is a nice day”, “This was a good day”,
“This was a nice day”]
I’m changing your string to a valid regexp, changing all occurrences
of {} with () (depending on your case you might want to change that).
Then I create a regexp with that and call my method.
This supports many constructs from regexps, which might be overkill
for your problem. For sure there will be easier solutions for your
specific case. Anyway, I hope you find it interesting.
On Wed, Feb 11, 2009 at 11:47 AM, Christoph B. [email protected] wrote:
Wow, this is really useful! Especially in a general way like that.
Thanks a lot for the quick and helpful answer!
BTW, I have found the other post that was similar. I don’t know how to
find the link to the archive but
if you search for this subject: “Describing degerate dna strings” you
might be able to find it.
People answered with custom solutions for that exact problem (which
was pretty similar), so you might get other ideas there.
my_text = “This {is|was} a {good|nice} day”
r = /{(.*?)}/
elem = my_text.split®.map! {|x| x.split("|")}
comb = []
elem.each_index do |i|
if comb.empty?
elem[i].each {|x| comb << x}
else
comb_size = comb.size
0…comb_size.times do |j|
elem[i].each { |x| s = comb.first + x ; comb.push(s) }
comb.shift
end
end
end
p comb
I tried using zsh functionality to output this, but it doesn’t seem
that easy.
Does anyone know an easy way how to do this in ruby?
Thank you.
def comb ary
ary.inject([[]]){|old,lst|
lst.inject([]){|new,e| new + old.map{|c| c + [ e ] }}}
end
s = “This {is|was|will be} a {good|nice} day to {surf|swim}.”
f = nil
a,b = s.split( /{(.*?)}/ ).partition{f=!f}
a = a.join( “%s” ) + “\n”
b.map!{|s| s.split “|”}
comb(b).each{|x| printf a % x }
— output —
This is a good day to surf.
This was a good day to surf.
This will be a good day to surf.
This is a nice day to surf.
This was a nice day to surf.
This will be a nice day to surf.
This is a good day to swim.
This was a good day to swim.
This will be a good day to swim.
This is a nice day to swim.
This was a nice day to swim.
This will be a nice day to swim.
William’s code is amazing!
A one line solution, inspired by his ideas:
“This {is|was} a {good|nice} day”.split(/{(.*?)}/).map! {|x|
x.split("|")}.inject([[]]){|old,lst| lst.inject([]){|new,e| new +
old.map{|c| c + [ e ] }}}.each {|x| p x.join}
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.