Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permute
#1
Can you helpme with this function?

permute("abcd" 3 perm)

perm=

bcd
bdc
bac
bad
bcd
bca
cda
cad
cbd
cba
cda
cdb
dab
dba
dca
dcb
dab
dac
abc
acb
adb
adc
abc
abd
#2
Search the Internet for C source code and convert it to QM.
#3
I found this:

#include <stdio.h>
#include <stdlib.h>


#define forever for( ; ; )


int main(void)
{
int n, m, i, nuevo_valor, *comb;


n = 6;
m = 3;


comb = malloc(m*sizeof(int));
if (comb == NULL){
puts("Puntero nulo.");
return EXIT_FAILURE;
}
for(i = 0; i < m; ++i)
comb[i] = i + 1;


forever {
putchar('\n');
for (i = 0; i < m; ++i)
printf("%d", comb[i]);
for (i = m - 1; i > -1 && comb[i] == n - ((m - 1) - i); --i)
;
if (i == -1)
return EXIT_SUCCESS;
nuevo_valor = ++comb[i];
while (i < m)
comb[++i] = ++nuevo_valor;
}

but I don't know how convert it.
#4
I don't think there is something similar to permute function. Better try this:

Code:
Copy      Help
/* swap(s,t): swap characters.
* Swaps characters *s and *t. */
void swap(char *s, char *t)
{
  int c;

  /* Rotate through temp variable c. */
  c = (int) *s; *s = *t; *t = (char) c;
}

/* perm(s,n,l): print permutations.
* Prints string s for all permutations of characters
* in positions n through l-1.
*/
void perm(char *s, int n, int l)
{
    int i;

    if (n == l)
    {
        /* No characters to permute . . print string. */
        printf("%s", s);
    }
    else
    {
        /* Work through all characters of string. */
        for (i = n; i < l; i++)
        {
            /* Swap this character with the first one. */
            swap(s + i, s + n);
            /* Permute remainder of string. */
            perm(s, n+1, l);
            /* Swap back again. */
            swap(s + i, s + n);
        }
    }
}

Hints for converting C/C++ code to QM:
Here are two functions - perm and swap. That is, you need to create two QM items of Function type. But maybe better would be to integrate swap into perm.
char* in QM is lpstr.
(int), (char) and similar are type casts. In QM they can be omitted, or use + if gives error.
{ } marks code blocks. In QM, code blocks are marked by tab-indenting.
// and /* ... */ are comments.
, and ; in most cases can be leaved, although in QM they are not always necessary.
printf in QM is out.
for (i = n; i < l; i++) in QM is for i n l
s + i an similar arguments in QM must not contain spaces or must be enclosed: s+i or (s + i).
void is used instead of nothing, that is, the function does not return a value.
perm is function name, then follow (arguments) and {function body}.
Function argument definitions also must not contain spaces. Convert void perm(char *s, int n, int l) to function lpstr's int'n int'l or to shorter form function $s n l.
Convert *s to s[0], which is the same in C, but in QM lpstr is not interpreted as pointer.
#5
What's wrong?

Code:
Copy      Help
function $s n l
int i c
if(n=l)
,out "%s" s
else
,for i n l
,,c=s[i];s[i]=s[n];s[n]=c
,,perm(s,n+1,l)
,,c=s[i];s[i]=s[n];s[n]=c
#6
Works well, I think.

perm "abcd" 0 4

result:
abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc

Here is my version (permute), very similar but stores result into variable instead of printing.

Code:
Copy      Help
function $s str&sout [n] [l] ;;n is 0-based position of first char, l string length

;EXAMPLE
;str s
;permute("abcd" s)
;out s



if(l<1) l=len(s)

int i c

if n>=l
,;No characters to permute . . print string.
,sout.formata("%s[]" s)
else
,;Work through all characters of string.
,for i n l
,,;Swap this character with the first one.
,,c = s[i]; s[i] = s[n]; s[n] = c
,,;Permute remainder of string.
,,permute(s sout n+1 l)
,,;Swap back again.
,,c = s[i]; s[i] = s[n]; s[n] = c
#7
Thanks for all.


Forum Jump:


Users browsing this thread: 1 Guest(s)