i'm converting C source code into javascript but i got some problem, I don't know how to write this c method in javascript
void create_board(int r, int c, int w) {
board = malloc((r * c) * sizeof(int));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
board[i * c + j] = -1;
}
}
num_rows = r;
num_cols = c;
num_win = w;
}
anybody can help me to write this code in javascript?
i've tried many solution but nothing works for me.
create_board(r,c,w) {
// this.#board = [];
this.#board = this.#num_rows * this.#num_cols * ;
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
this.#board[i * c + j] = -1;
}
}
this.num_rows = r;
this.#num_cols = c;
this.num_win = w;
}
The c code emulates a 2d array so what about:
function create_board(r,c,w) {
this.board = new Array(r)
for (let i = 0; i < r; i++) {
this.board[i] = new Array(c).fill(-1);
}
this.num_rows = r;
this.num_cols = c;
this.num_win = w;
}
console.log(new create_board(3,3,3));
and here the output from the above:
create_board {
board: [ [ -1, -1, -1 ], [ -1, -1, -1 ], [ -1, -1, -1 ] ],
num_rows: 3,
num_cols: 3,
num_win: 3
}
Related
i write code that combinating all possibe words.
and here is code.
const fs = require('fs');
const MAX = 9
var arr = new Array(MAX).fill(0);
var visited = new Array(MAX).fill(0);
word = []
N = 0
M = 0
fn = 0
function test(cnt, k, pos, ltr) {
console.log("6.", cnt, k, pos, ltr)
tmp = [];
if (cnt == M) {
for (i = 0; i < M; i++) {
tmp.push(k[arr[i] - 1]);
//console.log("1.",tmp);
}
j = 0;
for (i = 0; i < fn; i++) {
//console.log("1-1.",pos[i],ltr[j])
tmp.splice(pos[i] - 1, 0, ltr[j]);
//console.log("2",tmp);
j += 1;
}
s = tmp.join("");
//console.log("3",s);
word.push(s);
//console.log("4.",word);
return;
}
for (i = 1; i <= N; i++) {
if (visited[i] == 0) {
visited[i] = 1;
arr[cnt] = i;
test(cnt + 1, k, pos, ltr);
visited[i] = 0;
console.log("5.", visited, arr);
}
}
}
k = process.argv[2];
M = Number(process.argv[3]);
N = k.length;
fn = Number(process.argv[4]);
X = N + M;
pos = [];
ltr = [];
console.log(k, M, fn, N);
for (i = 0; i < fn; i++) {
pos[i] = process.argv[5 + 2 * i];
ltr[i] = process.argv[6 + 2 * i];
}
//console.log(pos);
//console.log(ltr);
test(0, k, pos, ltr)
for (i of word) {
console.log(i);
}
this code algorithm is worked different language like python,c++,but not javascript.
for example, i give a argument "visage 2 1 3 n",
other langaues prints "agn ain asn avn ean ..." but in this code only prints "vin vsn van ven"
i don't know where is the problem and how to fix it. so help me.
+) also inclde c++ code
#include <vector>
#include <string>
#include <stdlib.h>
#include <algorithm>
/*
#ifdef _WIN32
#include <windows.h>
#endif
*/
#include <fstream>
using namespace std;
const int MAX = 8 + 1;
int fn;
int N, M, X;
int arr[MAX];
bool visited[MAX];
vector<string> word;
string cmd = "node checktest.js";
ofstream file("./input.txt");
void func(int cnt, string k, int pos[], char ltr[])
{
cout<<cnt<<" "<<k<<"\n";
vector<char> tmp;
if (cnt == M)
{
for (int i = 0; i < M; i++)
tmp.push_back(k[arr[i] - 1]);
int j = 0;
for (int i = 0; i < fn; i++)
{
tmp.insert(tmp.begin() + pos[i] - 1, ltr[j]);
j++;
}
string s(tmp.begin(), tmp.end());
word.push_back(s);
return;
}
for (int i = 1; i <= N; i++)
if (!visited[i])
{
visited[i] = true;
arr[cnt] = i;
func(cnt + 1, k, pos, ltr);
visited[i] = false;
}
}
int main(int args,char * argv[])
{
/*
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif*/
string k(argv[1]);
N = k.length();
M=atoi(argv[2]);
fn=atoi(argv[3]);
X = N + M;
int *pos = new int(fn);
char *ltr = new char(fn);
for (int i = 0; i < fn; i++)
{
pos[i]=atoi(argv[4+2*i]);
ltr[i]=argv[5+2*i][0];
}
func(0, k, pos, ltr);
sort(word.begin(), word.end());
word.erase(unique(word.begin(), word.end()), word.end());
for (auto i : word)
{
file<<i<<"\n";
}
file.close();
return 0;
}
You are given an array of integers a and two integers l and r. You task is to calculate a boolean array b, where b[i] = true if there exists an integer x, such that a[i] = (i + 1) * x and l ≤ x ≤ r. Otherwise, b[i] should be set to false.
function boundedRatio(a, l, r) {
let b = []
for (let i = l; i <= r; i++) {
//
for (let j = 0; j < a.length; j++) {
let result = (j + 1) * i
if ((j + 1) * i === a[j] && l <= i <= r) {
b.push(true)
} else {
b.push(false)
}
}
}
return b
boundedRatio([8, 5, 6, 16, 5], 1, 3)
// [false, false, true, false, true]
Your main loop should be over the array a. For each of them, you need a loop over l through r. In that loop you should just set a variable to inidcate whether the bounded value is found. Once you find it you can break out of the loop. Then you push that onto the result.
The main loop and push can be combine with map().
function boundedRatio(a, l, r) {
let b = a.map((el, i) => {
let bounded = false;
for (let x = l; x <= r; x++) {
if ((i + 1) * x == el) {
bounded = true;
break;
}
}
return bounded;
})
return b;
}
console.log(boundedRatio([8, 5, 6, 16, 5], 1, 3));
Here's how it looks with a for loop instead of map().
function boundedRatio(a, l, r) {
let b = [];
for (let i = 0; i < a.length; i++) {
let bounded = false;
for (let x = l; x <= r; x++) {
if ((i + 1) * x == a[i]) {
bounded = true;
break;
}
}
b.push(bounded);
}
return b;
}
console.log(boundedRatio([8, 5, 6, 16, 5], 1, 3));
You can write down this code in C# also like that.
bool[] solution(int[] numbers, int left, int right) {
bool [] result=new bool[numbers.Length];
for(int i =0; i < numbers.Length; i++ )
{
bool bounded = false;
for(int x = left; x <= right; x++)
{
if((i+1)*x == numbers[i])
{
bounded= true;
break;
}
}
result[i] = bounded;
}
return result;
}
I am writing a program to calculate euclidean distance and then display the lines based, with the below code:
function discreteFrechet(X, Y) {
var M = X.length;
var N = Y.length;
var S = [
[],
[]
];
var backpointers = [
[],
[]
];
var backpaths = [];
var idx;
var path = [];
var paths;
var back = [
[],
[]
];
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
S[i][j] = 0;
backpointers[i][j] = 0;
}
} /* populates S array */
/*sanity check*/
S[0, 0] = euclidian(X, Y, 0, 0);
opt1 = [-1, 0];
opt2 = [0, -1];
opt3 = [-1, -1];
backpaths.push(opt1);
backpaths.push(opt2);
backpaths.push(opt3);
/*backpaths populated*/
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
options = [];
if (i != 0 || j != 0) {
if (i > 0) {
options[0] = S[i - 1, j];
}
if (j > 0) {
options[1] = S[i - 1, j];
}
if (i > 0 && j > 0) {
options[2] = S[i - 1, j];
}
idx = Math.min(options);
backpointers[i][j] = idx;
S[i][j] = Math.max(options[idx], euclidian(X, Y, i, j));
}
}
}
console.log(S);
paths = [
[M - 1, N - 1]
];
path = [
[],
[]
];
path.push(paths);
//Create "path"
i = M - 1;
j = N - 1;
count = 0;
while ((path[path.length - 1][0] != 0) || (path[0][path[1].length - 1] != 0)) {
back[0][1] = backpaths[backpointers[i], [j]];
i += back[0];
j += back[1];
path.push([i, j]);
if (count > 1000) {
console.log("too many loops");
break;
}
count += 1;
}
path.push([0, 0]);
path.reverse();
//returns bottleneck and the path
}
As I am testing, I am running into a problem with an infinite while loop (hence the break statement) any help or suggestions would be greatly appreciated! The goal is to append indicies into the path element, such that I can then take those path indicies and the bottleneck and use them to plot with d3.
So... If I input:
4 1 5 3
INSTEAD OF 1,3,4,5
I GET [ 4, 1, 5, 3 ]
Following is the code for merge sort but for the last comparison the program doesn't fetch updated (1,4) (3,5) value rather (4,1) (5,3) thus giving the wrong result.
var a = [4, 1, 5, 3];
q(a);
function q(a) {
var start = 0;
var n = a.length;
var length = parseInt(n / 2);
if (n < 2) {
return n;
}
var l = [], r = [];
for (i = 0; i < length; i++) {
l[i] = a[i]; //left array
}
for (i = 0, j = length; j < n; i++ , j++) {
r[i] = a[j]; //right array
}
q(l); //merge sort left array
q(r); //merge sort right array
comp(l, r);
}
function comp(l, r) {
var k = [], m = 0, i = 0, j = 0;
while (i < ((l.length)) && j < ((r.length))) {
if (l[i] < r[j]) {
k[m] = l[i];
i++;
m++
}
else {
k[m] = r[j];
j++;
m++
}
}
while (i != (l.length)) {
k[m] = l[i];
m++;
i++;
}
while (j != (r.length)) {
k[m] = r[j];
m++;
j++;
}
console.log(k); //for final output it is [ 4, 1, 5, 3 ] instead of [1,3,4,5]
}
You have a couple small problems. The main one is that you are returning the wrong thing from your edge condition:
if (n < 2) {
return n; // n is just a length; doesn't make sense to return it.
}
n is the length, you really want to return the small array here:
if (n < 2) {
return a; // return the array instead
}
Also, you need to pass the result of the recursive call to your comp function. Right now you're just returning the original lists with:
comp(l, r)
Something like this would work better:
let l_sort = q(l); //merge sort left array
let r_sort = q(r); //merge sort right array
return comp(l_sort, r_sort); // merge the arrays when recursion unwinds.
And you need to return things for recursion to work.
Put all together:
function q(a) {
var start = 0;
var n = a.length;
var length = parseInt(n / 2);
if (n < 2) {
return a;
}
var l = [],
r = [];
for (i = 0; i < length; i++) {
l[i] = a[i]; //left array
}
for (i = 0, j = length; j < n; i++, j++) {
r[i] = a[j]; //right array
}
let l_sort = q(l); //merge sort left array
let r_sort = q(r); //merge sort right array
return comp(l_sort, r_sort);
}
function comp(l, r) {
var k = [],
m = 0,
i = 0,
j = 0;
while (i < ((l.length)) && j < ((r.length))) {
if (l[i] < r[j]) {
k[m] = l[i];
i++;
m++
} else {
k[m] = r[j];
j++;
m++
}
}
while (i != (l.length)) {
k[m] = l[i];
m++;
i++;
}
while (j != (r.length)) {
k[m] = r[j];
m++;
j++;
}
return k
}
console.log(q([4, 1, 5, 3]).join(','));
console.log(q([5, 4, 3, 2, 1]).join(','));
console.log(q([2, 3]).join(','));
console.log(q([3, 2]).join(','));
console.log(q([1]).join(','));
I am trying to adapt an established dynamic programming Matrix Chain Multiplication algorithm to compare Javascript's performance to other languages.
This is the Java version found on Wikipedia from which I am adapting to Javascript:
void matrixChainOrder(int[] p) {
int n = p.length - 1;
m = new int[n][n];
s = new int[n][n];
for (int L = 1; L < n; L++) {
for (int i = 0; i < n - L; i++) {
int j = i + L;
m[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
Below is the version I am adapting and it is the same as far as I can see, but the console logs the following error message: TypeError: m[(k + 1)] is undefined
var matrixChainOrder = function(p) {
var n = p.length - 1;
var m = [[,]];
var s = [[,]];
for (var L = 1; L < n; L++) {
for (var i = 0; i < n - L; i++) {
var j = i + L;
m[i][j] = Number.MAX_SAFE_INTEGER;
for (var k = i; k < j; k++) {
var q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
var array = [15, 12, 16, 17, 19];
var d = new Date();
var start = d.getMilliseconds();
matrixChainOrder(array);
var end = d.getMilliseconds();
console.log(end - start);
I don't understand why a TypeError is being thrown... hoping someone else can spot the problem and help me out.