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;
}
Related
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
}
I am trying to make Pascal's Triangle in JavaScript but there are lots of errors. I have know idea why errors are happening but there are some.
Code:
function triangle() {
this.rows = [[1]];
this.genRow = function() {
this.rows.push([]);
this.rows[this.rows.length-1].push(1);
for (var i = 0; i < this.rows[this.rows.length-1].length; i++){
var u = [this.rows[this.rows.length-1][i-1], this.rows[this.rows.length-1][i], this.rows[this.rows.length-1][i+1]];
var f = function(e) {
return e != undefined;
};
function s() {
var sum=0;
for (var index = 0; i < this.legnth; i++){
sum =+ this[i];
}
return sum;
}
u = u.filter(f).s();
this.rows[this.rows.length-1].push(u);
}
this.rows[this.rows.length-1].push(1);
}
}
var t = new triangle();
t.genRow();
console.log(t.rows);
Thanks.
Please try this code,To Pasqual's triangle gone wrong
#include <stdio.h>
int binomialCoeff(int n, int k);
void printPascal(int n)
{
for (int line = 0; line < n; line++)
{
for (int i = 0; i <= line; i++)
printf("%d ",
binomialCoeff(line, i));
printf("\n");
}
}
int binomialCoeff(int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
int main()
{
int n = 7;
printPascal(n);
return 0;
}
I hope this code will be useful.
Thank you.
const pascalsTriangle = (rows = 1) => {
let res = [];
for (let i = 1; i <= rows; i++) {
if (i == 1) {
res.push([1]);
}
else if (i == 2) {
res.push([1, 1]);
}
else {
let arr = [1];
let lastArr = res[i - 2];
for (let index=0; index<lastArr.length-1; index++) {
arr.push(lastArr[index] + lastArr[index + 1]);
}
arr.push(1);
res.push(arr);
}
}
return res;
};
This will work perfectly. You can refer it here. https://github.com/omkarsk98/Exercism/blob/master/javascript/pascals-triangle/pascals-triangle.js
Here is an approach in JS.
function getNextLevel(previous) {
const current = [1];
for (let i = 1; i < previous.length; i++) {
current.push(previous[i] + previous[i - 1]);
}
current.push(1);
return current;
}
function pascalTriangle(levels = 1) {
let currentRow = [1];
while (levels--) {
console.log(currentRow.join(" "));
currentRow = getNextLevel(currentRow);
}
}
pascalTriangle(10);
i have a project which use the same data, which in my c++ code it need 17sec to train 100 data, meanwhile in javascript code from this project
https://github.com/CodingTrain/Toy-Neural-Network-JS
it running only about 10sec to train 2400data
please someone help me whats wrong, and i need to complete my project for my undergraduate thesis.
ive already build 2 project, which one of them(this) is the same neuralnetwork in c++ from that javascript code(kinda), but still giving the same results
NeuralNetwork::NeuralNetwork(int a,int b,int c)
{
this->numInput = a;
this->numHidden = b;
this->numOutput = c;
std::vector<double> vec(a, 0.1);
for (int i = 0; i < b; ++i) {
this->weightIH.push_back(vec);
}
std::vector<double> vec2(b, 0.1);
for (int i = 0; i < c; ++i) {
this->weightHO.push_back(vec2);
}
}
NeuralNetwork::~NeuralNetwork()
{
}
std::vector<double> NeuralNetwork::tambahbias(std::vector<double> a) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = a[i] + 1;
}
return a;
}
std::vector<double> NeuralNetwork::activate(std::vector<double> a) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = a[i] / (1 + abs(a[i]));
}
return a;
}
std::vector<double> NeuralNetwork::derivation(std::vector<double> a) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = a[i] * (1 - a[i]);
}
return a;
}
std::vector<double> NeuralNetwork::hitungError(std::vector<double> a, std::vector<double> b) {
int size = a.size();
for (int i = 0; i < size; ++i) {
a[i] = b[i] - a[i];
}
return a;
}
void NeuralNetwork::train(std::vector<double> a, std::vector<double> target) {
std::vector<double> hidden(numHidden);
for (int i = 0; i < numHidden; ++i) {
for (int j = 0; j < numInput; ++j) {
hidden[i] += a[j] * weightIH[i][j];
}
}
hidden = tambahbias(hidden);
hidden = activate(hidden);
std::vector<double> output(numOutput);
for (int i = 0; i < numOutput; ++i) {
for (int j = 0; j < numHidden; ++j) {
output[i] += hidden[j] * weightHO[i][j];
}
}
output = tambahbias(output);
output = activate(output);
std::vector<double> errorO(numOutput);
errorO = hitungError(output, target);
std::vector<double> gradO(numOutput);
gradO = derivation(output);
for (int i = 0; i < numOutput; ++i) {
gradO[i] = gradO[i] * errorO[i] * 0.1;
}
for (int i = 0; i < numOutput; ++i) {
for (int j = 0; j < numHidden; ++j) {
weightHO[i][j] += (gradO[i] * hidden[j]);
}
}
std::vector<double> gradH(numHidden);
std::vector<double> derH(numHidden);
derH = derivation(hidden);
for (int i = 0; i < numHidden; ++i) {
for (int j = 0; j < numOutput; ++j) {
gradH[i] = gradO[j] * weightHO[j][i];
}
gradH[i] = gradH[i] * derH[i] * 0.1;
}
for (int i = 0; i < numHidden; ++i) {
for (int j = 0; j < numInput; ++j) {
weightIH[i][j] += (gradH[i] * a[j]);
}
}
}
You're copying all your std::vectors into functions:
void NeuralNetwork::train(std::vector<double> a, std::vector<double> target)
use references instead:
void NeuralNetwork::train(const std::vector<double>& a, const std::vector<double>& target)
Copying a vector is an O(n) operation in both space and time, using a reference is O(1) in both.
A const std::vector reference can't be modified, when you're copying the vector in and out again after modifying it:
std::vector<double> NeuralNetwork::derivation(std::vector<double> a)
use a non-const reference instead:
void NeuralNetwork::derivation(std::vector<double>& a)
it turns out im just an idiot, who doesnt know about debug / release, make this program to release just solve the problem, thank you everyone for your help
Does anyone know a way to convert base 10 and base 255 strings in JavaScript exceeding the Number.MAX_SAFE_INTEGER value without using a big number library?
For something like:
var base10 = '23456786543234567876543234567876543267';
var base255 = base10ToBase255(base10);
To base-255 or from base-255 as:
var base255 = new Uint8Array(20);
for (var i = 0; i < 20; i++) base255[i] = 254 - i;
var base10 = base255ToBase10(base255);
EDITED: changed to allow for other bases (<=256)
It always boils down to using a big integer, sorry. But you do not need much, it's just about 100 lines of code for what you want (string to base 256 and back).
"use strict";
var COMMON_BASE = 255; // must be 256 at most!
function copyA(a){
var ret = new Uint8Array(a.length);
for(var i = 0;i<a.length;i++){
ret[i] = a[i];
}
return ret;
}
function isZero(a){
for(var i = 0;i<a.length;i++){
if(a[i] !== 0)
return false;
}
return true;
}
function clampA(a){
var alen = a.length;
var i=0;
while(a[alen - 1] === 0)alen--;
var ret = new Uint8Array(alen);
for(var i = 0;i<alen;i++){
ret[i] = a[i];
}
return ret;
}
function addD(a,d) {
var tlen = a.length;
var carry = 0;
var ret = new Uint8Array(tlen +1);
if(d === 0)
return copyA(a);
var i = 0;
var temp = carry;
temp += a[i] + d;
carry = Math.floor(temp / COMMON_BASE);
ret[i] = temp % COMMON_BASE;
for (i = 1; i < tlen; i++) {
temp = carry;
temp += a[i];
carry = Math.floor(temp / COMMON_BASE);
ret[i] = temp % COMMON_BASE;
}
if (carry) {
ret[i] = carry;
}
ret = clampA(ret);
return ret;
};
function mulD(a,d){
var tlen = a.length;
var carry = 0;
var ret = new Uint8Array(tlen + 1);
var k = 0;
var tmp;
if(isZero(a))
return copyA(a);
if(d === 0)
return new Uint8Array(tlen);
for (; k < tlen; k++) {
tmp = a[k] * d + carry;
ret[k] = tmp % COMMON_BASE;
carry = Math.floor(tmp / COMMON_BASE);
}
if (carry) {
ret[k] = carry;
}
ret = clampA(ret);
return ret;
}
function divRem(a,d){
var divrem = function(u, m, v, q, B) {
var k = 0,
t;
for (var j = m - 1; j >= 0; j--) {
k = (k * COMMON_BASE) ;
k += u[j];
if (k >= v) {
t = Math.floor(k / v);
k -= t * v;
} else {
t = 0;
}
q[j] = t;
}
return k;
};
var Q = new Uint8Array(a.length);
var R = divrem(a,a.length, d, Q, 8);
Q = clampA(Q);
return [Q,R];
}
// Assuming 's' being a string with decimal digits
function base10ToBase256(s){
var blen = 0;
// checks&balances omitted
var out = new Uint8Array(1);
for(var i=0;i<s.length;i++){
out = mulD(out,10);
out = addD(out,parseInt(s[i],10) );
}
return out;
}
// Assuming b being a Uint8Array
function base256ToBase10(a){
var s = "";
var t = copyA(a);
var qr = [];
var i = a.length;
while(!isZero(t)){
qr = divRem(t,10);
s = s + qr[1].toString(10);
t = qr[0];
}
return s.split("").reverse().join("");
}
var str = "8716418673416734167345634096788356249857";
//base10ToBase256(str).join(",");
base256ToBase10(base10ToBase256(str));
var str = "8716418673416734167345634096788356249857";
console.log(base10ToBase256(str).join(","));
console.log(base256ToBase10(base10ToBase256(str)));
Here the LSB is at position zero.
It's a rough hack (way too much copies etc.) but it'll do it.
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.