FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Combinaciones de letras
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM
Combinaciones de letras
Posted: Mon Dec 05, 2022 05:54 PM

Hola a todos...

Necesito armar un algoritmo que me devuelva todas las combinaciones posibles de una serie de letras formando combinaciones mayores o iguales a 3 caracteres.

Ejemplo tomando la palabra "ESTEBAN"

Obtener como resultado:

EST

ESTE

ESTEB

ESTEBAN

SET

SETB

SETBA

SETBAN

Etc, etc etc...

A alguien se le ocurre como encarar este algoritmo?

Desde ya muchas gracias.

Saludos,

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 10:14 AM
Estimado Esteban,

Un primer intento:
Code (fw): Select all Collapse
function Main()

聽 聽local cWord := "ESTEBAN", cChar, cResult, n
聽 聽local aResults := {}
聽 聽
聽 聽for each cChar in cWord
聽 聽 聽 for n = 3 to Len( cWord )
聽 聽 聽 聽 聽if Len( cResult := SubStr( cWord, cChar:__enumIndex(), n ) ) >= 3
聽 聽 聽 聽 聽 聽 if AScan( aResults, cResult ) == 0
聽 聽 聽 聽 聽 聽 聽 聽AAdd( aResults, cResult )
聽 聽 聽 聽 聽 聽 endif 聽 
聽 聽 聽 聽 聽endif 聽 聽 聽 聽 
聽 聽 聽 next 聽 聽 
聽 聽next
聽 聽
聽 聽? ASort( aResults )

return nil
Puedes probarlo desde aqui: copia el c贸digo y selecciona Run - Execute en el menu
https://www.fivetechsoft.com/counter/modpro.php
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 10:27 AM
Le acabo de pedir a la inteligencia artificial de chatGPT https://chat.openai.com/chat lo siguiente:
write a C program that given a word will provide all the three letters or more combinations
y ha respondido esto :-)
Code (fw): Select all Collapse
#include <stdio.h>
#include <string.h>

// Function to print all possible combinations of three or more letters
// in the given word
void print_combinations(char *word)
{
聽 聽 int len = strlen(word);

聽 聽 // Loop over all possible combinations of three or more letters
聽 聽 // in the given word
聽 聽 for (int i = 0; i < len; i++)
聽 聽 {
聽 聽 聽 聽 for (int j = i + 1; j < len; j++)
聽 聽 聽 聽 {
聽 聽 聽 聽 聽 聽 for (int k = j + 1; k < len; k++)
聽 聽 聽 聽 聽 聽 {
聽 聽 聽 聽 聽 聽 聽 聽 printf("%c%c%c\n", word[i], word[j], word[k]);
聽 聽 聽 聽 聽 聽 }
聽 聽 聽 聽 }
聽 聽 }
}

int main()
{
聽 聽 char word[100]; // Store the word

聽 聽 printf("Enter a word: ");
聽 聽 scanf("%s", word);

聽 聽 printf("All possible combinations of three or more letters in '%s' are:\n", word);

聽 聽 // Call the function to print all combinations
聽 聽 print_combinations(word);

聽 聽 return 0;
}
Y adem谩s te explica lo que ha hecho:
This program uses a nested loop to generate all possible combinations of three or more letters in the given word. The outer two loops iterate over all pairs of letters in the word, and the inner loop iterates over all remaining letters in the word to generate a combination of three letters. The combination is then printed on a new line.
Estoy viendo si genera c贸digo Harbour tambi茅n :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 10:30 AM
Increible, sabe programar en Harbour !!! :-D
escribe un programa en Harbour que dada una palabra genere todas las posibles combinaciones de 3 o m谩s letras que haya en la palabra
Code (fw): Select all Collapse
// Funci贸n para imprimir todas las posibles combinaciones de 3 o m谩s letras
// en la palabra dada
PROCEDURE print_combinations(cWord)
聽 聽 LOCAL cCombination[4], i, j, k, nLen

聽 聽 // Obtener la longitud de la palabra
聽 聽 nLen := LEN(cWord)

聽 聽 // Recorrer todas las posibles combinaciones de 3 o m谩s letras
聽 聽 // en la palabra dada
聽 聽 FOR i := 1 TO nLen
聽 聽 聽 聽 FOR j := i + 1 TO nLen
聽 聽 聽 聽 聽 聽 FOR k := j + 1 TO nLen
聽 聽 聽 聽 聽 聽 聽 聽 // Generar la combinaci贸n de 3 letras
聽 聽 聽 聽 聽 聽 聽 聽 cCombination := SUBSTR(cWord, i, 1) + SUBSTR(cWord, j, 1) + SUBSTR(cWord, k, 1)

聽 聽 聽 聽 聽 聽 聽 聽 // Imprimir la combinaci贸n
聽 聽 聽 聽 聽 聽 聽 聽 ? cCombination
聽 聽 聽 聽 聽 聽 NEXT
聽 聽 聽 聽 NEXT
聽 聽 NEXT
RETURN NIL

// Programa principal
PROCEDURE Main()
聽 聽 LOCAL cWord[100] // Almacenar la palabra

聽 聽 // Pedir la palabra al usuario
聽 聽 INPUT "Ingrese una palabra: ", cWord

聽 聽 // Imprimir el t铆tulo
聽 聽 ? "Todas las posibles combinaciones de 3 o m谩s letras en '", cWord, "' son:"

聽 聽 // Llamar a la funci贸n para imprimir las combinaciones
聽 聽 print_combinations(cWord)

聽 聽 RETURN
Y la explicaci贸n ;-)
Este programa utiliza un bucle anidado para generar todas las posibles combinaciones de 3 o m谩s letras en la palabra dada. Los dos primeros bucles recorren todos los pares de letras en la palabra, y el bucle interno recorre todas las letras restantes en la palabra para generar una combinaci贸n de 3 letras. Luego se imprime la combinaci贸n en una nueva l铆nea.
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 10:49 AM
Estoy impresionado :-)

He probado el c贸digo y fallaba asi que le he dicho que lo corrigiese, en dos intentos ha corregido el c贸digo y ahora funciona bien. Lo cual quiere decir que aprende y es capaz de corregir sus propios fallos !!!
sigue generando palabras de solo 3 letras. Por favor corrigelo

Aqu铆 est谩 una versi贸n actualizada del programa en Harbour que genera todas las posibles combinaciones de 3 o m谩s letras en una palabra dada:
Code (fw): Select all Collapse
// Funci贸n para imprimir todas las posibles combinaciones de 3 o m谩s letras
// en la palabra dada
PROCEDURE print_combinations(cWord)
聽 聽 LOCAL cCombination[100], i, j, k, l, nLen

聽 聽 // Obtener la longitud de la palabra
聽 聽 nLen := LEN(cWord)

聽 聽 // Recorrer todas las posibles combinaciones de 3 o m谩s letras
聽 聽 // en la palabra dada
聽 聽 FOR i := 1 TO nLen
聽 聽 聽 聽 FOR j := i + 1 TO nLen
聽 聽 聽 聽 聽 聽 FOR k := j + 1 TO nLen
聽 聽 聽 聽 聽 聽 聽 聽 // Generar la combinaci贸n de 3 letras
聽 聽 聽 聽 聽 聽 聽 聽 cCombination := SUBSTR(cWord, i, 1) + SUBSTR(cWord, j, 1) + SUBSTR(cWord, k, 1)

聽 聽 聽 聽 聽 聽 聽 聽 // A帽adir m谩s letras a la combinaci贸n si es posible
聽 聽 聽 聽 聽 聽 聽 聽 FOR l := k + 1 TO nLen
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 cCombination += SUBSTR(cWord, l, 1)
聽 聽 聽 聽 聽 聽 聽 聽 NEXT

聽 聽 聽 聽 聽 聽 聽 聽 // Imprimir la combinaci贸n
聽 聽 聽 聽 聽 聽 聽 聽 ? cCombination
聽 聽 聽 聽 聽 聽 NEXT
聽 聽 聽 聽 NEXT
聽 聽 NEXT
RETURN NIL

// Programa principal
PROCEDURE Main()
聽 聽 LOCAL cWord[100] // Almacenar la palabra

聽 聽 // Pedir la palabra al usuario
聽 聽 // INPUT "Ingrese una palabra: ", cWord
聽 聽 cWord = "ESTEBAN"

聽 聽 // Imprimir el t铆tulo
聽 聽 ? "Todas las posibles combinaciones de 3 o m谩s letras en '", cWord, "' son:"

聽 聽 // Llamar a la funci贸n para imprimir las combinaciones
聽 聽 print_combinations(cWord)

聽 聽 RETURN
ETEBAN
ETBAN
ETAN
ETN
EEBAN
EEAN
EEN
EBAN
EBN
EAN
STEBAN
STBAN
STAN
STN
SEBAN
SEAN
SEN
SBAN
SBN
SAN
TEBAN
TEAN
TEN
TBAN
TBN
TAN
EBAN
EBN
EAN
BAN
Y la explicaci贸n de lo que ha hecho:
En esta versi贸n del programa, se agreg贸 un cuarto bucle que recorre todas las letras restantes despu茅s de las primeras tres letras y las a帽ade a la combinaci贸n si es posible. De esta manera, se pueden generar combinaciones de cualquier longitud mayor o igual a 3
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 02:01 PM

Espectacular.

As铆 a simple vista parece algo sencillo pero se me hizo una laguna y no sab铆a por donde encarar el problema.

Muy bueno Antonio.

Muchas gracias.

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 02:24 PM

Antonio...

Estuve analizando las respuestas, y creo que el resultado no es el esperado.

Es decir, faltan combinaciones.

Por ej no veo que me devuelva las combinaciones que comiencen con "N".

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 02:30 PM
Prueba con chatGPT y ve dandole indicaciones hasta que funcione de forma deseada :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM
Re: Combinaciones de letras
Posted: Tue Dec 06, 2022 02:37 PM

Muchas gracias Antonio.

Saludos,

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 1364
Joined: Wed Jun 21, 2006 12:39 AM
Re: Combinaciones de letras
Posted: Wed Dec 07, 2022 12:30 PM

Hola, eso se llama combinatoria en matem谩ticas y hay f贸rmulas para resolverlo.

Saludos

Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM
Re: Combinaciones de letras
Posted: Thu Dec 08, 2022 01:14 PM

Entiendo que las formulas de combinatorias nos dicen cuantas combinaciones tenemos.

Pero yo necesito saber cuales son esas combinaciones.

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: Combinaciones de letras
Posted: Thu Dec 08, 2022 02:19 PM
Creo que puede ser algo asi:
Code (fw): Select all Collapse
// Funci贸n para imprimir todas las posibles combinaciones de 3 o m谩s letras
// en la palabra dada
PROCEDURE Main()
聽 聽 LOCAL cCombination[100], i, j, k, l, nLen, cWord := "ESTEBAN", aArray := {}

聽 聽 // Obtener la longitud de la palabra
聽 聽 nLen := LEN(cWord)

聽 聽 // Recorrer todas las posibles combinaciones de 3 o m谩s letras
聽 聽 // en la palabra dada
聽 聽 FOR i := 1 TO nLen
聽 聽 聽 聽 FOR j := 1 TO nLen
聽 聽 聽 聽 聽 聽IF j <> i
聽 聽 聽 聽 聽 聽 FOR k := 1 TO nLen
聽 聽 聽 聽 聽 聽 聽 聽IF k <> j .and. k <> i
聽 聽 聽 聽 聽 聽 聽 聽 // Generar la combinaci贸n de 3 letras
聽 聽 聽 聽 聽 聽 聽 聽 cCombination := SUBSTR(cWord, i, 1) + SUBSTR(cWord, j, 1) + SUBSTR(cWord, k, 1)
聽 聽 聽 聽 聽 聽 聽 聽 IF ASCAN(aArray,cCombination) == 0
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽AADD(aArray,cCombination)
聽 聽 聽 聽 聽 聽 聽 聽 ENDIF 聽 
聽 聽 聽 聽 聽 聽 聽 聽 // A帽adir m谩s letras a la combinaci贸n si es posible
聽 聽 聽 聽 聽 聽 聽 聽 FOR l := 1 TO nLen
聽 聽 聽 聽 聽 聽 聽 聽 聽 if l <> k .and. l <> i .and. l <> j
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 cCombination += SUBSTR(cWord, l, 1)
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 IF ASCAN(aArray,cCombination) == 0
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 AADD(aArray,cCombination)
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ENDIF 聽 
聽 聽 聽 聽 聽 聽 聽 聽 聽 endif 聽 聽 聽 聽
聽 聽 聽 聽 聽 聽 聽 聽 NEXT 聽 聽 聽 聽 聽 聽 聽 聽
聽 聽 聽 聽 聽 聽 聽 endif 聽 聽
聽 聽 聽 聽 聽 聽 NEXT
聽 聽 聽 聽 聽 ENDIF 聽 聽 聽 聽
聽 聽 聽 聽 NEXT
聽 聽 NEXT
聽 聽 xbrowse(aArray)
RETURN NIL
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Combinaciones de letras
Posted: Thu Dec 22, 2022 08:52 AM
jrestojeda wrote:Hola a todos...
Necesito armar un algoritmo que me devuelva todas las combinaciones posibles de una serie de letras formando combinaciones mayores o iguales a 3 caracteres.

Ejemplo tomando la palabra "ESTEBAN"
Obtener como resultado:

EST
ESTE
ESTEB
ESTEBAN
SET
SETB
SETBA
SETBAN

Etc, etc etc...

A alguien se le ocurre como encarar este algoritmo?
Desde ya muchas gracias.

Saludos,
From your posting, what you are looking for are not just combinations but all permutations of
( 3 of 7 ) + ( 4 of 7 ) + ( 5 of 7 ) + ( 6 of 7 ) + ( 7 of 7 ) characters.

If each character in the source string is unique, we get a total of 13,650 permutations (from 99 combinations).

Forumla:
Code (fw): Select all Collapse
fact(7)/fact(4) + fact(7)/fact(3) + fact(7)/fact(2) + fact(7) + fact(7) // --> 13,650
FACT(n) is a function in CTLIB, which gives factorial of n.

However the alphabet "E" is repeated twice resulting in some duplicate results. Ignoring such duplicates, we get 95 combinations finally resulting in 8,553 total permutations.

Program to generate all unique combinations and permutations:
Code (fw): Select all Collapse
#include "fivewin.ch"

static aPrm := {}
static aCmb := {}
static aResult := {}

function Main()

聽 聽local cSrc 聽:= "ESTEBAN"
聽 聽local n

聽 聽for n := 3 to Len( cSrc )
聽 聽 聽 Combinations( n, "", cSrc )
聽 聽next

聽 聽for n := 1 to Len( aCmb )
聽 聽 聽 aPrm 聽:= {}
聽 聽 聽 Permutations( "", aCmb[ n ] )
聽 聽 聽 AMERGE( aResult, aPrm )
聽 聽next

聽 聽XBROWSER aCmb 聽 聽SHOW RECID TITLE "COMBINATIONS"
聽 聽XBROWSER aResult SHOW RECID TITLE "PERMUTATIONS"

// 聽 ? fact(7)/fact(4) + fact(7)/fact(3) + fact(7)/fact(2) + fact(7) + fact(7)

return nil

function Combinations( r, cCmb, cSrc )

聽 聽local nLen 聽:= Len( cSrc )
聽 聽local n

聽 聽if r == 0
聽 聽 聽 if AScan( aCmb, cCmb ) == 0
聽 聽 聽 聽 聽AAdd( aCmb, cCmb )
聽 聽 聽 endif
聽 聽else
聽 聽 聽 for n := 1 to ( nLen - r + 1 )
聽 聽 聽 聽 聽Combinations( r - 1, cCmb + SUBSTR( cSrc, n, 1 ), SUBSTR( cSrc, n + 1 ) )
聽 聽 聽 next
聽 聽endif

return nil

function Permutations( cLeft, cRight )

聽 聽local nLen 聽:= Len( cRight )
聽 聽local n

聽 聽if nLen == 0
聽 聽 聽 if AScan( aPrm, cLeft ) == 0
聽 聽 聽 聽 聽AAdd( aPrm, cLeft )
聽 聽 聽 endif
聽 聽else
聽 聽 聽 for n := 1 to nLen
聽 聽 聽 聽 聽Permutations( cLeft + SUBSTR( cRight, n, 1 ), ;
聽 聽 聽 聽 聽 聽 聽 聽Left( cRight, n - 1 ) + SubStr( cRight, n + 1 ) )
聽 聽 聽 next
聽 聽endif

return nil


So far, this is of mathematical interest.
But I am wondering what can be the practical use of this huge result-set.

Formulae of interest:

Total permutations of r out of n = n! / ( n - r )!
Total combinations of r out of n = n! / ( r! * ( n - r )! )
where n! is Factorial( n )
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion