If n belongs to set a and m belongs to set b repeat some steps, How?

We can do so in java and similar languages like:

for (int i=0, int j=3; i<=2 && j<=5;i++,j++){
  do some repetive task until the condition becomes false...
}

how to keep these two conditions based on two different sets(suppose two
arrays whose indexes can be used as i and j) in condition block??

some working code would be greatly appreciated..thanks in advance..

···

--
Posted via http://www.ruby-forum.com/.

Hi,

so you want to iterate parallely over two arrays?

There are many ways to do that in Ruby. The most comfortable solution is
probably using Array#zip:

as, bs =
  [2, 4, 6], [10, 20, 30, 40]
as.zip bs do |a, b|
  puts "a: #{a}, b: #{b}"
end

You can also use Enumerators:

each_a, each_b =
  as.each, bs.each
loop do
  a, b =
    each_a.next, each_b.next
  puts "a: #{a}, b: #{b}"
end

And a low level Java-like solution would be

0.upto [as.length, bs.length].min - 1 do |i|
  puts "a: #{as[i]}, b: #{bs[i]}"
end

···

--
Posted via http://www.ruby-forum.com/.

Quoting zubair a. (lists@ruby-forum.com):

We can do so in java and similar languages like:

for (int i=0, int j=3; i<=2 && j<=5;i++,j++){
  do some repetive task until the condition becomes false...
}

how to keep these two conditions based on two different sets(suppose two
arrays whose indexes can be used as i and j) in condition block??

The simplest way is just to unroll the C construct:

i=0
j=3
while(i<=2 && j<=5)
..
..
..
i+=1
j+=1
end

It is less neat, I am afraid, but it works the same way.

I often use for loops like that in C, but when I write in Ruby I know
I must express the algorithm in a different way. Ruby loops manage
only one variable. How I do it depends on the case. In your example,
for example, the loop runs 3 times (unless you modify the loop
variables within the loop itself), and j is always equal to i+3. Thus
I could do

3.times do |i|
..
..
end

and inside the loop, when in need of the 'j' value, I'd instead use
'i+3'.

Carlo

···

Subject: if n belongs to set a and m belongs to set b repeat some steps, How?
  Date: Sat 08 Dec 12 02:05:33PM +0900

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

In this particular case it's totally useless to use two variables in
the loop condition - regardless of programming language.

0.upto 2 do |i|
  j = i+3
  ...
end

for i in 0..2
  j = i+3
  ...
end

Best approach depends on your actual use case. Can you give a more
concrete example of what you are trying to achieve?

Cheers

robert

···

On Sat, Dec 8, 2012 at 6:05 AM, zubair a. <lists@ruby-forum.com> wrote:

We can do so in java and similar languages like:

for (int i=0, int j=3; i<=2 && j<=5;i++,j++){
  do some repetive task until the condition becomes false...
}

how to keep these two conditions based on two different sets(suppose two
arrays whose indexes can be used as i and j) in condition block??

some working code would be greatly appreciated..thanks in advance..

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

pls find the attachment, this is a merge sort algo implementation where
you can see i have to check values of i and j in while loop assuming
they are dividing the array into two halves.

actually i was trying to code something like:

  list=[1,2,3,4,5,6]
  (0..2,3..5).each do |i,j|
    do some task using i and j
  end
but ruby is not accepting this code as legal,

thanks to @Jan E. , i appreciate your code as it is much closer to what
i was trying to do, as ruby is very expressive so code written in ruby
should
be read as we write pseudo-code :slight_smile:

and yes code that i have attached is still have some error which i yet
to remove them all...

Attachments:
http://www.ruby-forum.com/attachment/7941/MergeSort.rb

···

--
Posted via http://www.ruby-forum.com/.

Actually, you don't have to do anything with the indices in Ruby:

···

#-----------------------------------------
def merge_sort list
  return list if list.length <= 1
  half = list.length / 2
  left, right =
    list.take(half), list.drop(half)
  merge merge_sort(left), merge_sort(right)
end

def merge left, right
  rem_left, rem_right, merged =
    left.clone, right.clone, []
  until rem_left.empty? or rem_right.empty?
    if rem_left.first <= rem_right.first
      merged << rem_left.shift
    else
      merged << rem_right.shift
    end
  end
  merged + rem_left + rem_right
end

p merge_sort [5, 2, 3, 4, 1]
#-----------------------------------------

I'll look into your code later, but in any case the expression (a..b,
c..d) isn't valid. There are no tuples or something in Ruby. You'd have
to use an array: [a..b, c..d].

--
Posted via http://www.ruby-forum.com/.

Thanks a lot.

And I find it great that you really want to learn Ruby in depth. Many
people just keep their habits from their previous language and merely
change the syntax. This way they'll of course never experience the
capabilities of Ruby or get to know new ideas.

I think the key to "the Ruby way" is to forget the low-level procedural
style we all know from languages like Java or C. When you want to do
something with arrays in Java, you have to do it "by hand". You have to
actually go through the elements and put them into the desired place.
That's because Java arrays (and the derived classes) are basically
limited to "assign a value to an index" and "get the length".

Ruby, on the other hand, is much more expressive. It borrows many
powerful features from functional languages like Haskell ("map",
"select", "reduce", "take", "partition" etc.), and there are a lot of
specialized methods. So whatever you want to do, there's probably a
method or combination of methods for that. You rarely need to fumble
with indices and move variables around.

A good way of learning Ruby is to regularly read the API reference
(http://www.ruby-doc.org). After a while, you'll know the methods and
what you can do with them. You should also try to think in more abstract
terms. Instead of "I'll have this loop here and that counter there",
think about what you actually want to do like for example "I want to get
the first n elements of an array". In the long run, you might also look
into functional languages like the beautiful Haskell. It really teaches
you a different way of thinking, and I got a lot of insights from it.

Whatever you do, have fun. :slight_smile:

Regarding your code:

You can simplify the method a lot if you work with subarrays instead of
passing the original array and the current indices around.

So the methods should just be

def merge_sort list
  ...
end

def merge left, right
  ...
end

The main thing that makes the code long and complicated is obviously the
index arithmetic. But there's not really much you can do about it apart
from completely getting rid of it. Since the counters depend on the
order of the elements, you cannot use Ruby's counting methods ("upto",
"each"), because those all count "statically" in a fixed pattern.

By the way, method names in Ruby usually use underscores and not
camelCase like in Java. And you don't need to initialize arrays with a
certain length. Just start with an empty array: []. You should also use
the keywords "and" and "or" instead of "&&" and "||". The latter have a
high precendence and are rather used for technical stuff like setting a
default value. The keywords are what you'd use for "if" statements and
logical expressions.

I've attached a modified version of your code.

Attachments:
http://www.ruby-forum.com/attachment/7944/merge_sort_update.txt

···

--
Posted via http://www.ruby-forum.com/.

zubair a. wrote in post #1088326:

pls find the attachment, this is a merge sort algo implementation where
you can see i have to check values of i and j in while loop assuming
they are dividing the array into two halves.

actually i was trying to code something like:

  list=[1,2,3,4,5,6]
  (0..2,3..5).each do |i,j|
    do some task using i and j
  end
but ruby is not accepting this code as legal,

plz find the latest attachment,

Attachments:
http://www.ruby-forum.com/attachment/7942/MergeSort.rb

···

--
Posted via http://www.ruby-forum.com/.

Jan E. wrote in post #1088339:

Actually, you don't have to do anything with the indices in Ruby:

@Jan E.
man u r too good in your field of expertise....wow!!, your code is
utilizing core theme of ruby...i wish i could learn few things from
you.. :), waiting to get my code verified by you and experts like you..

···

--
Posted via http://www.ruby-forum.com/.

Jan E. wrote in post #1088354:

Thanks a lot.

And I find it great that you really want to learn Ruby in depth.

@Jan E.
very fruitful and pivotal suggestions....thanks dear, i am really happy
that you looked into my code...and i learnt many beautiful things in
ruby from you and others here...you guessed right from inside i am a
c/c++ and java coder, i am learning ruby by applying it on algo
implementation,
i certainly need help and hints from you and experts like you..

i found the code and no doubt it is self explainatory enough..very
crisp.
again if i stuck in something i will keep disturbing you :slight_smile: :slight_smile:

your suggestions is going to ease my learning path..thanks a lot dear :slight_smile:
:slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Do keep this in mind; don't get yourself stuck on an implementation in
the idioms from other languages. To me, ruby is most naturally
expressive of algorithms, moreso than other languages I've encountered
(and that list is long... been at this stuff a long time...)

···

On Sat, Dec 8, 2012 at 8:13 AM, zubair a. <lists@ruby-forum.com> wrote:

as ruby is very expressive so code written in ruby
should be read as we write pseudo-code :slight_smile: