PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Iterate through Array?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Iterate through Array?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 07h56   #1
Zephyr Pellerin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Iterate through Array?

I've been messing around with generating all possible combinations of a
set of letters, (Ala, a 6 letter set would start enumerating at "aaaaaa"
then move on to "aaaaab" so on and so forth, all the way down to
"zzzzz") by iterating through an array using slice() , Which is a pretty
good implementation for small tasks, But suppose I wanted to generate
larger sets, Is there a more efficient implementation than say

a=0
b=0
c=0
d=0
e=0
f=0
z = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

slice = z.slice(a) + z.slice(b) + z.slice(c) + z.slice(d) + z.slice(e) +
z.slice(f)

if...
else.... [Incrementing]

puts slice

Thank you in advance
  Réponse avec citation
Vieux 22/11/2007, 10h02   #2
Zephyr Pellerin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterate through Array?

Sebastian Hungerecker wrote:
> Zephyr Pellerin wrote:
>> I've been messing around with generating all possible combinations of a
>> set of letters, (Ala, a 6 letter set would start enumerating at "aaaaaa"
>> then move on to "aaaaab" so on and so forth, all the way down to
>> "zzzzz")

>
> ("aaaaaa".."zzzzzz").each do |string|
> puts string
> end
>
> This will run for a good while, but it won't hog memory the way a solution
> populating an array would.
>
>
> HTH,
> Sebastian



Impressive, Signifigantly less RAM usage.
  Réponse avec citation
Vieux 22/11/2007, 20h22   #3
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterate through Array?

Zephyr Pellerin wrote:
> I've been messing around with generating all possible combinations of a
> set of letters, (Ala, a 6 letter set would start enumerating at "aaaaaa"
> then move on to "aaaaab" so on and so forth, all the way down to
> "zzzzz")


("aaaaaa".."zzzzzz").each do |string|
puts string
end

This will run for a good while, but it won't hog memory the way a solution
populating an array would.


HTH,
Sebastian
--
NP: Lake of Tears - Sweetwater
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 22/11/2007, 20h25   #4
Thomas Wieczorek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterate through Array?

You might also try the Permutations gem at http://permutation.rubyforge.org/

  Réponse avec citation
Vieux 23/11/2007, 03h59   #5
Lloyd Linklater
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterate through Array?

What about something as simple as

i = 0
"aaaaa".upto("zzzzz") {|s| i += 1; puts "#{i}. #{s}"}

You could write to a file if you wish. Note that "aaaaa".upto("zzzzz")
is NOT the same as "a".upto("zzzzz")
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12910 seconds with 13 queries