r/stackoverflow • • Dec 02 '24

C Coding challenge: Convert JavaScript to C - without "artificial intelligence"

Convert JavaScript to C as demonstrated here https://www.codeconvert.ai/javascript-to-c-converter - without using "artificial intelligence".

Sample input

permutations.js

function array_nth_permutation(a = [0,1,2,3,4], n = 5) {
  let lex = n;
  let b = []; // copy of the set a.slice()
  for (let x = 0; x < a.length; x++) {
    b[x] = a[x];
  }
  let len = a.length; // length of the set
  const res = []; // return value, undefined
  let i = 1;
  let f = 1;

  // compute f = factorial(len)
  for (; i <= len; i++) {
    f *= i;
  }

  // if the permutation number is within range
  if (n >= 0 && n < f) {
    // start with the empty set, loop for len elements
    //let result_len = 0;
    for (; len > 0; len--) {     
      // determine the next element:
      // there are f/len subsets for each possible element,
      f /= len;
      // a simple division gives the leading element index
      i = (n - n % f) / f; // Math.floor(n / f);
      res.push(b.splice(i, 1)[0]);
      // reduce n for the remaining subset:
      // compute the remainder of the above division
      n %= f;
      // extract the i-th element from b and push it at the end of res
    }
    console.log(`[${lex}] ${JSON.stringify(res)}`);
  } else {
    console.log(`${n} >= 0 && ${n} < ${f}: ${n >= 0 && n < f}`);
  }
  // return the permutated set or undefined if n is out of range
  return 0;
}

Sample output

permutations.c

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

void array_nth_permutation(int a[], int len, int n) {
    int lex = n;
    int *b = (int *)malloc(len * sizeof(int)); // copy of the set
    for (int x = 0; x < len; x++) {
        b[x] = a[x];
    }
    
    int res[len]; // return value
    int i = 1;
    int f = 1;

    // compute f = factorial(len)
    for (; i <= len; i++) {
        f *= i;
    }

    // if the permutation number is within range
    if (n >= 0 && n < f) {
        // start with the empty set, loop for len elements
        for (; len > 0; len--) {
            // determine the next element:
            f /= len;
            // a simple division gives the leading element index
            i = (n - n % f) / f;
            res[len - 1] = b[i]; // store the result
            // shift elements left
            for (int j = i; j < len; j++) {
                b[j] = b[j + 1];
            }
            // reduce n for the remaining subset:
            n %= f;
        }
        printf("[%d] [", lex);
        for (int j = 0; j < sizeof(res)/sizeof(res[0]); j++) {
            printf("%d", res[j]);
            if (j < sizeof(res)/sizeof(res[0]) - 1) {
                printf(", ");
            }
        }
        printf("]\n");
    } else {
        printf("%d >= 0 && %d < %d: %d\n", n, n, f, n >= 0 && n < f);
    }
    
    free(b);
}

int main() {
    int a[] = {0, 1, 2, 3, 4};
    int n = 5;
    array_nth_permutation(a, sizeof(a) / sizeof(a[0]), n);
    return 0;
}

0 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/guest271314 Dec 03 '24 edited Dec 03 '24

That's not the question.

The question is can you write a program that at least equals the output of some random Web site's claims of JavaScript to C converter - that actually compiles JavaScript to C and works as expected?

In the example at OP we hard code the input.

Now, we can also write the code like this, to print dynamic lexicographic permutation and dynamic input, here using QuickJS, making use of that built in JavaScript engine we can pass JavaScript input on the command line

$ ~/bin/permutations-qjsc '[...Array(4).keys()].map((_, i)=>i)' 22
[22] [3,2,0,1]

const [, input, lex] = scriptArgs.map((arg, i) => !!i && eval(arg));

function array_nth_permutation(a = [0,1,2,3,4], n = 5) {
  let lex = n;
  let b = []; // copy of the set a.slice()
  for (let x = 0; x < a.length; x++) {
    b[x] = a[x];
  }
  let len = a.length; // length of the set
  const res = []; // return value, undefined
  let i = 1;
  let f = 1;

  // compute f = factorial(len)
  for (; i <= len; i++) {
    f *= i;
  }
  // if the permutation number is within range
  if (n >= 0 && n < f) {
    // start with the empty set, loop for len elements
    for (; len > 0; len--) {
      
      // determine the next element:
      // there are f/len subsets for each possible element,
      f /= len;
      // a simple division gives the leading element index
      i = (n - n % f) / f; // Math.floor(n / f);
      res.push(b.splice(i, 1)[0]);
      // reduce n for the remaining subset:
      // compute the remainder of the above division
      n %= f;
      // extract the i-th element from b and push it at the end of res
    }
    console.log(`[${lex}] ${JSON.stringify(res)}`);
  } else {
    console.log(`${n} >= 0 && ${n} < ${f}: ${n >= 0 && n < f}`);
  }
  //console.log("[" + lex + "]", JSON.stringify(res));
  // return the permutated set or undefined if n is out of range
  return 0;
}

array_nth_permutation(input, lex);