r/stackoverflow • u/guest271314 • 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
View all comments
Show parent comment
1
u/guest271314 Dec 03 '24
Tried all of these (all on GitHub). So far. Don't work as expected.
QuickJS's
qjscworks very well, at the cost of embedding the QuickJS engine into the executable, or WASM application.What's close, from my perspective, is Facebook's Static Hermes
-emit-coption. I'm pretty sure the JavaScript interpreter is not embedded in the resulting executable when compiled withgcc. That's not "idiomatic C", if I use that term loosely. Though compiles to a 39.8 KB executable usinggcc, whereas theqjsccode compiled withgccis 1.2 MB, accounting for QuickJS engine in there, too.