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 > Combinatorics and permutations? ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Combinatorics and permutations? ?

Réponse
 
LinkBack Outils de la discussion
Vieux 11/03/2008, 12h06   #1
David Palm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Combinatorics and permutations? ?

Hi,
I have a problem involving combinatorics and permutations and my brain hurts.

The problem: I have a hash of products in input. I then build an array of equivalent products for each input product. I end up with an array such as this:
{
:input_prod_1 => [:p1_1, :p1_2, ... , p1_x],
:input_prod_2 => [:p2_1, :p2_1, ..., p2_y],
...
:input_prod_m => [:pm_1, :pm_1, ..., pm_z]
}

I need all the unique combinations of each product from the first group with each of the second. For example:
given: [ [:p1, :p2], [:p3, p4] ]
I need to obtain: [ [:p1, :p3], [:p1, :p4], [:p2, :p3], [:p2, :p4] ]

Given: [ [:p1], [:p2, :p3, :p4] ]
I need: [ [:p1, :p2], [:p1, :p3], [:p1, :p4] ]

The number of input products is in the order of tens (max); the same goes for the number of equivalent products. This means the total number of partitions is pretty large and I need to weed out the equivalent ones soon/fast enough to reduce the total number.

I've been making attempts with the permutation gem all morning; looks like it's doing the "right thing", but frankly my maths skills are lacking and I can't really figure out how to use it (should I?).

Also: order is not important for the result arrays.

This is easy, right? :-/

  Réponse avec citation
Vieux 11/03/2008, 12h22   #2
Emmanuel Oga
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combinatorics and permutations? ?

http://blade.nagaokaut.ac.jp/~sinara/ruby/math/

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

  Réponse avec citation
Vieux 11/03/2008, 12h37   #3
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combinatorics and permutations? ?

On Mar 11, 5:06 am, David Palm <dvd...@gmail.com> wrote:

> I need all the unique combinations of each product from the first group with each of the second. For example:
> given: [ [:p1, :p2], [:p3, p4] ]
> I need to obtain: [ [:p1, :p3], [:p1, :p4], [:p2, :p3], [:p2, :p4] ]
>
> Given: [ [:p1], [:p2, :p3, :p4] ]
> I need: [ [:p1, :p2], [:p1, :p3], [:p1, :p4] ]


p [[1,2],[3,4]].inject([[]]){|old,lst|
lst.inject([]){|new,e| new + old.map{|c| c.dup << e}}}
  Réponse avec citation
Vieux 11/03/2008, 13h11   #4
David Palm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Combinatorics and permutations? ?

On Tue, 11 Mar 2008 20:40:11 +0900, William James wrote:
> p [[1,2],[3,4]].inject([[]]){|old,lst|
> lst.inject([]){|new,e| new + old.map{|c| c.dup << e}}}


awesome. Totally rocks.

  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 03h44.


É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,10756 seconds with 12 queries