Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > cyclic array

Reply
Thread Tools

cyclic array

 
 
Josselin
Guest
Posts: n/a
 
      04-28-2007
I would like to print n elements from an Array in a cyclic way.

I mean :

using anArray = [ "a", "b", "c", "d", "e", "f", "g"], I should do :

anArray.print_cyclic(4, 0) => "a", "b", "c", "d" (first print 4
elements starting with the first one)

anArray.print_cyclic_next => "b", "c", "d", "e"
anArray.print_cyclic_next => "c", "d", "e", "f"
anArray.print_cyclic_next => "d", "e", "f", "g"
anArray.print_cyclic_next => "e", "f", "g", "a"
anArray.print_cyclic_next => "f", "g", "a", "b"
anArray.print_cyclic_next => "g", "a", "b", "c"
......
also in reverse cycle
anArray.print_cyclic(4, 0) => "a", "b", "c", "d"
anArray.print_cyclic_previous => "g", "a", "b", "c"
anArray.print_cyclic_previous => "f", "g", "a", "b",

what could be the simplest way to do it ? no simple way using Array
class methods only ,
should I define a class and methods to loop into the array ? any
existing lib ? if it has been already done why should I rewrite it ....


tfyh

joss

 
Reply With Quote
 
 
 
 
John Joyce
Guest
Posts: n/a
 
      04-28-2007

On Apr 28, 2007, at 10:10 PM, Josselin wrote:

> I would like to print n elements from an Array in a cyclic way.
>
> I mean :
>
> using anArray = [ "a", "b", "c", "d", "e", "f", "g"], I should do :
>
> anArray.print_cyclic(4, 0) => "a", "b", "c", "d" (first print 4
> elements starting with the first one)
>
> anArray.print_cyclic_next => "b", "c", "d", "e"
> anArray.print_cyclic_next => "c", "d", "e", "f"
> anArray.print_cyclic_next => "d", "e", "f", "g"
> anArray.print_cyclic_next => "e", "f", "g", "a"
> anArray.print_cyclic_next => "f", "g", "a", "b"
> anArray.print_cyclic_next => "g", "a", "b", "c"
> .....
> also in reverse cycle
> anArray.print_cyclic(4, 0) => "a", "b", "c", "d"
> anArray.print_cyclic_previous => "g", "a", "b", "c"
> anArray.print_cyclic_previous => "f", "g", "a", "b",
>
> what could be the simplest way to do it ? no simple way using
> Array class methods only ,
> should I define a class and methods to loop into the array ? any
> existing lib ? if it has been already done why should I rewrite
> it ....
>
>
> tfyh
>
> joss
>


sounds like classic (?) Ruby. you just need a block. in the block,
get the array size, move element 0 to end. do block until satisfied.

 
Reply With Quote
 
 
 
 
Xavier Noria
Guest
Posts: n/a
 
      04-28-2007
On Apr 28, 2007, at 3:10 PM, Josselin wrote:

> I would like to print n elements from an Array in a cyclic way.
>
> I mean :
>
> using anArray = [ "a", "b", "c", "d", "e", "f", "g"], I should do :
>
> anArray.print_cyclic(4, 0) => "a", "b", "c", "d" (first print 4
> elements starting with the first one)
>
> anArray.print_cyclic_next => "b", "c", "d", "e"
> anArray.print_cyclic_next => "c", "d", "e", "f"
> anArray.print_cyclic_next => "d", "e", "f", "g"
> anArray.print_cyclic_next => "e", "f", "g", "a"
> anArray.print_cyclic_next => "f", "g", "a", "b"
> anArray.print_cyclic_next => "g", "a", "b", "c"
> .....
> also in reverse cycle
> anArray.print_cyclic(4, 0) => "a", "b", "c", "d"
> anArray.print_cyclic_previous => "g", "a", "b", "c"
> anArray.print_cyclic_previous => "f", "g", "a", "b",
>
> what could be the simplest way to do it ? no simple way using
> Array class methods only ,
> should I define a class and methods to loop into the array ? any
> existing lib ? if it has been already done why should I rewrite
> it ....



This is a possible approach:

module Enumerable
def each_cycle(window, start=0)
(start...length+start).each do |i|
yield((i..i+window).map {|n| self[n % length]})
end
end
end

Usage is:

a = [ "a", "b", "c", "d", "e", "f", "g"]
a.each_cycle(4) do |cycle|
puts "#{cycle}"
end

You can delegate reverse cycle to each_cycle on the reverse (and
perhaps taking the opposite of start mod length, depending on the
meaning of start).

-- fxn




 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      04-28-2007
On Apr 28, 7:05 am, Josselin <josse...@wanadoo.fr> wrote:
> I would like to print n elements from an Array in a cyclic way.


Not a direct answer to your question, but regarding circular lists
(which can be created from an array):
http://phrogz.net/RubyLibs/rdoc/files/Ouroboros_rb.html

 
Reply With Quote
 
Josselin
Guest
Posts: n/a
 
      04-28-2007
On 2007-04-28 15:05:48 +0200, Josselin <> said:

> I would like to print n elements from an Array in a cyclic way.
>
> I mean :
>
> using anArray = [ "a", "b", "c", "d", "e", "f", "g"], I should do :
>
> anArray.print_cyclic(4, 0) => "a", "b", "c", "d" (first print 4
> elements starting with the first one)
>
> anArray.print_cyclic_next => "b", "c", "d", "e"
> anArray.print_cyclic_next => "c", "d", "e", "f"
> anArray.print_cyclic_next => "d", "e", "f", "g"
> anArray.print_cyclic_next => "e", "f", "g", "a"
> anArray.print_cyclic_next => "f", "g", "a", "b"
> anArray.print_cyclic_next => "g", "a", "b", "c"
> .....
> also in reverse cycle
> anArray.print_cyclic(4, 0) => "a", "b", "c", "d"
> anArray.print_cyclic_previous => "g", "a", "b", "c"
> anArray.print_cyclic_previous => "f", "g", "a", "b",
>
> what could be the simplest way to do it ? no simple way using Array
> class methods only ,
> should I define a class and methods to loop into the array ? any
> existing lib ? if it has been already done why should I rewrite it ....
>
>
> tfyh
>
> joss


Thanks to all of u . As always different ways .. the most difficult
step for a newbie is : where should I start ?

joss

 
Reply With Quote
 
John Joyce
Guest
Posts: n/a
 
      04-28-2007

On Apr 29, 2007, at 12:10 AM, Josselin wrote:

> On 2007-04-28 15:05:48 +0200, Josselin <> said:
>
>> I would like to print n elements from an Array in a cyclic way.
>> I mean :
>> using anArray = [ "a", "b", "c", "d", "e", "f", "g"], I should do :
>> anArray.print_cyclic(4, 0) => "a", "b", "c", "d" (first print
>> 4 elements starting with the first one)
>> anArray.print_cyclic_next => "b", "c", "d", "e"
>> anArray.print_cyclic_next => "c", "d", "e", "f"
>> anArray.print_cyclic_next => "d", "e", "f", "g"
>> anArray.print_cyclic_next => "e", "f", "g", "a"
>> anArray.print_cyclic_next => "f", "g", "a", "b"
>> anArray.print_cyclic_next => "g", "a", "b", "c"
>> .....
>> also in reverse cycle
>> anArray.print_cyclic(4, 0) => "a", "b", "c", "d"
>> anArray.print_cyclic_previous => "g", "a", "b", "c"
>> anArray.print_cyclic_previous => "f", "g", "a", "b",
>> what could be the simplest way to do it ? no simple way using
>> Array class methods only ,
>> should I define a class and methods to loop into the array ? any
>> existing lib ? if it has been already done why should I rewrite
>> it ....
>> tfyh
>> joss

>
> Thanks to all of u . As always different ways .. the most difficult
> step for a newbie is : where should I start ?
>
> joss

Indeed, as a newbie myself, (forever I think) it is often a bit
overwhelming where to start.
A common method is to sit down with pen and paper and write out the
ideas, brainstorming. Figure out what you want to do. then break each
part down into it's components. it doesn't have to be in code yet.
That's almost the easy fun part, translating your ideas into code.
It really is a design process. Some snippets that you create or do
often enough will stick in your mind and you'll be able to apply them
quicker with practice. But pseudo code is always a good thing, if you
comment it out, it is instant documentation! (if necessary.

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      04-28-2007
Xavier Noria wrote:
...
> module Enumerable
> def each_cycle(window, start=0)
> (start...length+start).each do |i|
> yield((i..i+window).map {|n| self[n % length]})
> end
> end
> end


Here is a slight variation, for amusement, but it doesn't work for
window > length:

module Enumerable
def each_cycle(window, start = 0)
(-length+start...start).each do |i|
yield values_at(*(i..i+window))
end
end
end

a = [ "a", "b", "c", "d", "e", "f", "g"]
a.each_cycle(4) do |cycle|
puts "#{cycle}"
end

a.each_cycle(14) do |cycle|
puts "#{cycle}"
end

__END__

abcde
bcdef
cdefg
defga
efgab
fgabc
gabcd

abcdefgabcdefg
bcdefgabcdefg
cdefgabcdefg
defgabcdefg
efgabcdefg
fgabcdefg
gabcdefg

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Harry
Guest
Posts: n/a
 
      04-29-2007
>
> what could be the simplest way to do it ? no simple way using Array
> class methods only ,



I did a poor job of cleaning that code.
It had unnecessary stuff in it.
Sorry, my brain is not working today.

arr = ("a".."g").to_a
(1..25).each do
arr << arr.shift
p arr.slice(0..5)
end

Harry

--
http://www.kakueki.com/ruby/list.html
A Look into Japanese Ruby List in English

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      04-29-2007
On Apr 28, 7:58 am, Phrogz <g...@refinery.com> wrote:
> On Apr 28, 7:05 am, Josselin <josse...@wanadoo.fr> wrote:
>
> > I would like to print n elements from an Array in a cyclic way.

>
> Not a direct answer to your question, but regarding circular lists
> (which can be created from an array):
> http://phrogz.net/RubyLibs/rdoc/files/Ouroboros_rb.html


Expanding on its usage for the stated problem:

irb(main):001:0> require 'Ouroboros.rb'
=> true
irb(main):002:0> anArray = [ "a", "b", "c", "d", "e", "f", "g"]
=> ["a", "b", "c", "d", "e", "f", "g"]
irb(main):003:0> aSnake = Ouroboros.from_a anArray
=> #<Ouroboros:0x367810 @current_index=0, @current="a", @all=["a",
"b", "c", "d", "e", "f", "g"], @size=7>
irb(main):004:0> aSnake.to_a[ 0...4 ]
=> ["a", "b", "c", "d"]
irb(main):005:0> aSnake.increment
=> ["b"]
irb(main):006:0> aSnake.to_a[ 0...4 ]
=> ["b", "c", "d", "e"]
irb(main):008:0> aSnake.increment
=> "c"
irb(main):009:0> aSnake.to_a[ 0...4 ]
=> ["c", "d", "e", "f"]

....and so on.

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      04-30-2007
On 4/28/07, Xavier Noria <> wrote:

>
> This is a possible approach:
>
> module Enumerable
> def each_cycle(window, start=0)
> (start...length+start).each do |i|
> yield((i..i+window).map {|n| self[n % length]})
> end
> end
> end


This method won't work for Enumerables in general, although I guess it
does work for Arrays.

Not all enumerables have one or both length or [] methods.



--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.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
Cyclic array? jimi_usenet@hotmail.com Java 2 03-03-2006 06:20 PM
How the compiler deals with cyclic static initialization Hongzheng Wang Java 1 12-02-2003 01:28 PM
DTD problem: transforming cyclic graph into element definitions Yves Forkl XML 0 10-28-2003 12:31 PM
Cyclic object graph question Laird Nelson Java 2 10-10-2003 07:53 PM
Cyclic array problem nkw C Programming 7 09-25-2003 09:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57