Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > An elegant way...

Reply
Thread Tools

An elegant way...

 
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-20-2010
On 09/20/2010 11:40 AM, F. Senault wrote:
> Le 20 septembre à 20:17, Joel VanderWerf a écrit :
>
>> a.each_run(proc {|prev, s| prev.succ == s}) do |run|

>
> There's a catch with succ :
>
>>> "S9".succ

> => "T0"
>
> Definitely not what I want !
>
> Fred


Ow! Yup. Maybe you could use this:

>> "S9".sub(/\d+$/) {|digits| digits.succ}

=> "S10"


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      09-20-2010
On Sep 20, 2010, at 1:45 PM, F. Senault wrote:

> Le 20 septembre =E0 19:44, James Edward Gray II a =E9crit :
>=20
>> Dave Thomas and I were playing around with an idea like this =

recently:
>>=20
>> http://gist.github.com/570434
>>=20
>> http://gist.github.com/570556
>>=20
>> I hope those give you some fresh ideas.

>=20
> Where does the slice_before method comes from ?


>> RUBY_VERSION

=3D> "1.9.2"
>> Enumerable.public_instance_methods.include? :slice_before

=3D> true

It was added in 1.9.2, I believe.

James Edward Gray II=

 
Reply With Quote
 
 
 
 
F. Senault
Guest
Posts: n/a
 
      09-20-2010
Le 20 septembre à 20:59, James Edward Gray II a écrit :

>>> RUBY_VERSION

> => "1.9.2"
>>> Enumerable.public_instance_methods.include? :slice_before

> => true
>
> It was added in 1.9.2, I believe.


>> RUBY_VERSION

=> "1.9.1"
>> Enumerable.public_instance_methods.include? :slice_before

=> false

Indeed. Explains why I can't find it in my third edition Pick Axe...

Fred
--
The problem with defending the purity of the English language is that
English is about as pure as a cribhouse whore. We don't just borrow
words; on occasion, English has pursued other languages down alleyways
to beat them unconscious and rifle their pockets for new vocabulary.
(James D. Nicoll, rasfw)
 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      09-21-2010
On Tue, Sep 21, 2010 at 2:10 AM, F. Senault <(E-Mail Removed)> wrote:
> Hello everybody.
>
> =A0My
> goal is to find sequences in the numbers and join them with dashes :
>
>
>


This is not a complete solution but *maybe* it is something worth looking a=
t.
It depends on your specs.
I'll leave it to you to work out the details.

arr =3D [ '1', '2', '3', '4', '6', '7', '9', 'S1', 'S2' ]
s =3D ('1'..'20').to_a + ('S1'..'S5').to_a

t =3D []
s.each do |x|
t << x if arr.include?(x)
t << "*" if arr.include?(x) =3D=3D false
end

p t.join(" ").split("*").map{|y| y.strip.split(" ")}.select{|z| z.size
> 0}.map{|w| "#{w[0]}-#{w[-1]}"}


#> ["1-4", "6-7", "9-9", "S1-S2"]


Harry

 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      09-21-2010
On Tue, Sep 21, 2010 at 12:51 PM, Harry Kakueki <(E-Mail Removed)> wrote=
:
>
> arr =3D [ '1', '2', '3', '4', '6', '7', '9', 'S1', 'S2' ]
> s =3D ('1'..'20').to_a + ('S1'..'S5').to_a
>
> t =3D []
> s.each do |x|
> =A0t << x if arr.include?(x)
> =A0t << "*" if arr.include?(x) =3D=3D false
> end
>
> p t.join(" ").split("*").map{|y| y.strip.split(" ")}.select{|z| z.size
>> 0}.map{|w| "#{w[0]}-#{w[-1]}"}

>
> #> ["1-4", "6-7", "9-9", "S1-S2"]
>
>



A slightly shorter but still unreadable version

r =3D [ '1', '2', '3', '4', '6', '7', '9', 'S1', 'S2' ]
s =3D ('1'..'20').to_a + ('S1'..'S5').to_a

p [].tap{|m| s.each {|x| m << (r.include?(x) ? x : "*")}}.join("
").split("*").map{|y| y.strip.split(" ")}.select{|z| z.size >
0}.map{|w| "#{w[0]}-#{w[-1]}"}

#> ["1-4", "6-7", "9-9", "S1-S2"]


Harry

 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      09-21-2010
On Tue, Sep 21, 2010 at 2:17 PM, Harry Kakueki <(E-Mail Removed)> wrote:
> On Tue, Sep 21, 2010 at 12:51 PM, Harry Kakueki <(E-Mail Removed)> wrote:
>>

>
> A slightly shorter but still unreadable version
>
> r = [ '1', '2', '3', '4', '6', '7', '9', 'S1', 'S2' ]
> s = ('1'..'20').to_a + ('S1'..'S5').to_a
>
> p [].tap{|m| s.each {|x| m << (r.include?(x) ? x : "*")}}.join("
> ").split("*").map{|y| y.strip.split(" ")}.select{|z| z.size >
> 0}.map{|w| "#{w[0]}-#{w[-1]}"}
>
> #> ["1-4", "6-7", "9-9", "S1-S2"]
>



r = [ '1', '2', '3','4', '6', '7', '9', 'S1', 'S2' ]
s = ('1'..'20').to_a + ('S1'..'S5').to_a

p [].tap{|m| s.each{|x| m << (r.include?(x) ? x : "*")}}.join("
").split("*").map{|y| y.strip.split(" ")}.select{|z| z.size >
0}.map{|w| (w.size>1 ? "#{w[0]}-#{w[-1]}" : "#{w[0]}")}

#> ["1-4", "6-7", "9", "S1-S2"]


I'll shut up, now.

Harry

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-21-2010
Here is a very traditional imperative solution.

def format_episodes_list(src)
res = []
j = 0
while j < src.size
i = j
j += 1 while j+1 < src.size &&
src[j+1] == src[j].sub(/\d+/) { $&.succ }
res << ((i == j) ? src[i] : "#{src[i]}-#{src[j]}")
j += 1
end
res.join(", ")
end

puts format_episodes_list(['1', '2', '3', '4', '6', '7', '9', 'S1', 'S2'
])
puts format_episodes_list([ '1', '2', 'S3', 'S4', 'S5', 'O6' ])
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-21-2010
In fact, I forgot it's OK to run off the end of an Array in ruby So
the inner loop can simplify to this:

j += 1 while src[j+1] == src[j].sub(/\d+/) { $&.succ }
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-21-2010
On Tue, Sep 21, 2010 at 10:38 AM, Brian Candler <(E-Mail Removed)> wrote:

Fred, for the contrast here's my engineering approach. It may not be
elegant (you decide) but you get modularity and reusability.

First I'd start out with a proper representation. If your numbers are
a central part of your application you may want to spend a class for
them:

Num = Struct.new re, :val do
def succ
self.class.new pre, val.succ
end

def to_s; "#{pre}#{val}" end
def inspect; "#{self.class.name}(#{self})" end

include Comparable
def <=>(o) to_a <=> o.to_a end
end

def Num(s)
%r{\A(\D*)(\d+)\z} =~ s or raise "bad arg %p" % s
Num.new $1, $2.to_i
end

Then we can set up a generic mechanism for this

module Enumerable
def rangify(&bl)
bl ||= lambda {|x, y| y ? (x..y) : x}

res = []
a = b = nil

each do |x|
case
when a.nil?
a = b = x
when x == b.succ
b = x
else
res << bl[a, b]
a = b = x
end
end

res
end
end


Full story is here http://gist.github.com/589427

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-21-2010
On Tue, Sep 21, 2010 at 11:00 AM, Robert Klemme
<(E-Mail Removed)> wrote:
> On Tue, Sep 21, 2010 at 10:38 AM, Brian Candler <(E-Mail Removed)> wro=

te:
>
> Fred, for the contrast here's my engineering approach. =A0It may not be
> elegant (you decide) but you get modularity and reusability.
>
> First I'd start out with a proper representation. =A0If your numbers are
> a central part of your application you may want to spend a class for
> them:
>
> Num =3D Struct.new re, :val do
> =A0def succ
> =A0 =A0self.class.new pre, val.succ
> =A0end
>
> =A0def to_s; "#{pre}#{val}" end
> =A0def inspect; "#{self.class.name}(#{self})" end
>
> =A0include Comparable
> =A0def <=3D>(o) to_a <=3D> o.to_a end
> end
>
> def Num(s)
> =A0%r{\A(\D*)(\d+)\z} =3D~ s or raise "bad arg %p" % s
> =A0Num.new $1, $2.to_i
> end
>
> Then we can set up a generic mechanism for this


Forgot the end handling and mixed up two versions. Sorry for that.
Here's the proper one

module Enumerable
def rangify(&bl)
bl ||=3D lambda {|x, y| x =3D=3D y ? x : (x..y)}

res =3D []
a =3D b =3D nil

each do |x|
case
when a.nil?
a =3D b =3D x
when x =3D=3D b.succ
b =3D x
else
res << bl[a, b]
a =3D b =3D x
end
end

res << bl[a, b]
end
end

> Full story is here http://gist.github.com/589427


Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is there an elegant way to set an unsigned vector to 1 Jan De Ceuster VHDL 5 01-13-2005 07:26 AM
Elegant algorithm. Vladimir ASP .Net 0 07-31-2004 05:51 PM
Any elegant solution for managing upload file size? Braky Wacky ASP .Net 8 07-15-2004 08:19 PM
More Elegant Column Widths in ASP:Table Objects? =?Utf-8?B?QWxleCBNYWdoZW4=?= ASP .Net 1 05-14-2004 07:38 PM
Elegant way of returning FieldNames? Jay Balapa ASP .Net 1 08-07-2003 10:25 PM



Advertisments