Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
recursion
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
cyberco  
View profile  
 More options Nov 25 2005, 10:39 am
Newsgroups: comp.lang.ruby
From: "cyberco" <cybe...@gmail.com>
Date: 25 Nov 2005 07:39:54 -0800
Local: Fri, Nov 25 2005 10:39 am
Subject: recursion
I'm trying to print every combination of three characters from the
range (a..z). I thought recursion would be the most elegant solution,
but that got me quite confused :)  Any suggestions? Here's my
(erronous) attempt:

________________________

def addChar(str, level)
    ('a'..'z').each {|c|
        if level == 2
            puts str + c
        else
            str += c
            addChar(str, level + 1)
        end
    }
end

addChar("", 0)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Terpstra  
View profile  
 More options Nov 25 2005, 10:48 am
Newsgroups: comp.lang.ruby
From: Erik Terpstra <e...@terpnet.nl>
Date: Fri, 25 Nov 2005 16:48:36 +0100
Local: Fri, Nov 25 2005 10:48 am
Subject: Re: recursion
('aaa'..'zzz').to_a

Cheers,

Erik.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Terpstra  
View profile  
 More options Nov 25 2005, 10:50 am
Newsgroups: comp.lang.ruby
From: Erik Terpstra <e...@terpnet.nl>
Date: Fri, 25 Nov 2005 16:50:45 +0100
Local: Fri, Nov 25 2005 10:50 am
Subject: Re: recursion
Or 'a'..'zzz' if you want every combination 'up to' 3 characters.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan Smith  
View profile  
 More options Nov 25 2005, 10:57 am
Newsgroups: comp.lang.ruby
From: Nathan Smith <nathan.sm...@nist.gov>
Date: Sat, 26 Nov 2005 00:57:13 +0900
Local: Fri, Nov 25 2005 10:57 am
Subject: Re: recursion

Instead of

>             str += c
>             addChar(str, level + 1)

Use

addChar( str + c, level + 1 )

=)

On Nov 25, 2005, at 10:42 AM, cyberco wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian Schröder  
View profile  
 More options Nov 25 2005, 11:05 am
Newsgroups: comp.lang.ruby
From: Brian Schröder <ruby.br...@gmail.com>
Date: Sat, 26 Nov 2005 01:05:44 +0900
Local: Fri, Nov 25 2005 11:05 am
Subject: Re: recursion
On 25/11/05, cyberco <cybe...@gmail.com> wrote:

Thoug 'aaa'..'zzz' is certainly the most elegeant, maybe it would help
if you'd try to understand this recursion

def combinations(elements, length)
  return [] if length == 0
  return elements if length == 1
  result = []
  elements.each do | element |
    combinations(elements, length - 1).each do | combination |
      result << element + combination
    end
  end
  result
end

# What you want
puts combinations(('a'..'c').to_a, 3)

# Using the power of ducktyping
combinations([[1], [2], [3]], 3).each do | combination |
  p combination
end

# What does this calculate?
combinations((1..3).to_a, 3).each do | combination |
  p combination
end

cheers,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mathew  
View profile  
 More options Nov 25 2005, 5:48 pm
Newsgroups: comp.lang.ruby
From: mathew <m...@pobox.com>
Date: Fri, 25 Nov 2005 22:48:36 GMT
Local: Fri, Nov 25 2005 5:48 pm
Subject: Re: recursion

cyberco wrote:
> I'm trying to print every combination of three characters from the
> range (a..z). I thought recursion would be the most elegant solution,
> but that got me quite confused :)  Any suggestions?

Definitely not recursion.  Consider that your problem is equivalent to
printing every possible 3 digit number in base 26 :-)

Not that you can't do it recursively:

def f(lhs)
   if lhs.length == 3
     puts lhs
   else
     for c in 'a'..'z'
       f(lhs + c)
     end
   end
end

f('')

mathew
--
      <URL:http://www.pobox.com/~meta/>
My parents went to the lost kingdom of Hyrule
     and all I got was this lousy triforce.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cyberco  
View profile  
 More options Nov 27 2005, 7:37 am
Newsgroups: comp.lang.ruby
From: "cyberco" <cybe...@gmail.com>
Date: 27 Nov 2005 04:37:51 -0800
Local: Sun, Nov 27 2005 7:37 am
Subject: Re: recursion
Thanks! That is certaily a elegant solution, but I wonder what the
logic behind it is. I can't find any documentation on this usage of
ranges, any pointers?

Groeten, :)
Berco


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cyberco  
View profile  
 More options Nov 27 2005, 7:40 am
Newsgroups: comp.lang.ruby
From: "cyberco" <cybe...@gmail.com>
Date: 27 Nov 2005 04:40:15 -0800
Local: Sun, Nov 27 2005 7:40 am
Subject: Re: recursion
The recursive solution I found is:
__________________________
def addChar(str, level)
    if level == 2
        ('a'..'z').each {|c|
            p str + c
            @file << str + c + ' '
        }
    else
        ('a'..'z').each {|c|
            addChar(str + c, level + 1)
        }
    end
end
__________________________

I'm merely posting it here for future reference. Erik's solution
('aaa'..'zzz').to_a is ofcourse much more usable and elegant :)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
George Ogata  
View profile  
 More options Nov 27 2005, 8:50 am
Newsgroups: comp.lang.ruby
From: George Ogata <g_og...@optushome.com.au>
Date: Mon, 28 Nov 2005 00:50:22 +1100
Local: Sun, Nov 27 2005 8:50 am
Subject: Re: recursion

"cyberco" <cybe...@gmail.com> writes:
> Thanks! That is certaily a elegant solution, but I wonder what the
> logic behind it is. I can't find any documentation on this usage of
> ranges, any pointers?

Here's how you get there:

('aaa'..'zzz').to_a is clearly a call to Range#to_a.

* `ri Range` tells you that Range gets #to_a from Enumerable.
* `ri Enumerable#to_a` tells you that it returns the elements yielded by
  a call to #each.  (Oh wait, it doesn't.  It should... .  Really,
  it's no secret that all of Enumerable's methods are defined in terms
  of #each, but I can't find it anywhere in the rdoc comments.)
* `ri Range#each` tells you that the elements yielded come from
  successive calls to each element's #succ method, starting with the
  start object ('aaa' in this case).
* `ri String#succ` tells you that 'aaa'.succ will return 'aab' (and so
  on).
* Profit!  (Er... QED.)

HTH,
George.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gavin Kistner  
View profile  
 More options Nov 27 2005, 3:22 pm
Newsgroups: comp.lang.ruby
From: Gavin Kistner <ga...@refinery.com>
Date: Mon, 28 Nov 2005 05:22:41 +0900
Local: Sun, Nov 27 2005 3:22 pm
Subject: Re: recursion
On Nov 25, 2005, at 3:52 PM, mathew wrote:

> cyberco wrote:
>> I'm trying to print every combination of three characters from the
>> range (a..z). I thought recursion would be the most elegant solution,
>> but that got me quite confused :)  Any suggestions?

> Definitely not recursion.  Consider that your problem is equivalent  
> to printing every possible 3 digit number in base 26 :-)

Which leads to this alternative solution:

( 'aaa'.to_i(36)..'zzz'.to_i(36) ).map{ |i| (str=i.to_s(36)) =~ /^[a-
z]{3}$/ && str }.compact

Far less elegant than 'aaa'..'zzz', but there you have it. :)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google