I am facing problem in the below code...
This code is of recursive merge sort but the array which gets printed has repetitive elements from the array.
help me in identifying the problem.
void merge(int arr[], int l, int mid, int h) {
int i = l;
int j = mid + 1;
int k = l;
int b[h + 1];
while (i <= mid && j <= h) {
if (arr[i] < arr[j])
b[k++] = arr[i++];
else
b[k++] = arr[j++];
}
while (i <= mid)
b[k++] = arr[i++];
while (j <= h)
b[k++] = arr[j++];
for (int i = 0; i <= h; i++)
arr[i] = b[i];
}
void Rmerge_sort(int arr[], int l, int h)
{
if (l < h) {
int mid = (h + l) / 2;
Rmerge_sort(arr, l, mid);
Rmerge_sort(arr, mid + 1, h);
merge(arr, l, mid, h);
}
}
int main() {
int arr[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 }, n = 10;
Rmerge_sort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
You define array b with size h + 1 instead of h - l + 1. The merge loops copy the elements to the index values l to h, but the final copy loop takes elements from 0 to h, copying elements from an uninitialized part.
Here is a corrected version:
void merge(int arr[], int l, int mid, int h) {
int len = h - l + 1;
int i = l;
int j = mid + 1;
int k = 0;
int b[len];
while (i <= mid && j <= h) {
if (arr[i] < arr[j])
b[k++] = arr[i++];
else
b[k++] = arr[j++];
}
while (i <= mid)
b[k++] = arr[i++];
while (j <= h)
b[k++] = arr[j++];
for (int i = 0; i < len; i++)
arr[l + i] = b[i];
}
Note however that a better approach to this problem is to save only the left half of the slice to merge and to consider h as the index of the first element after the end of the slice. This avoid confusing and error prone +1/-1 adjustments and reduces the number of copies:
void merge(int arr[], int low, int mid, int hi) {
int len1 = mid - lo;
int b[len1];
int i, j, k;
for (i = 0; i < len1; i++)
b[i] = a[low + i];
for (i = 0, j = mid, k = lo; i < len;) {
if (j >= hi || arr[i] <= arr[j])
arr[k++] = b[i++];
else
arr[k++] = arr[j++];
}
}
void Rmerge_sort(int arr[], int low, int hi) {
if (hi - low > 1) {
int mid = low + (hi - low) / 2; // avoid arithmetic overflow
Rmerge_sort(arr, low, mid);
Rmerge_sort(arr, mid, hi);
merge(arr, low, mid, hi);
}
}
int main() {
int arr[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
Rmerge_sort(arr, 0, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
im new to R and im trying to solve for the minimum number of moves for a knight visit all the moves in a chess board.
I got the python code from:
https://www.geeksforgeeks.org/the-knights-tour-problem-backtracking-1/
and i tried to translate it to r.
But i am always getting the error and I don't know where I went wrong.
This is my code:
chess = rep(-1, times = 64)
board = matrix(data = chess, nrow = 8, ncol = 8, byrow = TRUE)
move_x = c(2, 1, -1, -2, -2, -1, 1, 2)
move_y = c(1, 2, 2, 1, -1, -2, -2, -1)
board[1, 1] = 0
pos = 1
valid_move <- function (x, y, board) {
if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
return (T)
}
return (F)
}
solve <- function (board, curr_x, curr_y, move_x, move_y, pos) {
if (pos == 64) {
return (T)
}
for (i in seq(1:8)) {
new_x = curr_x + move_x[i]
new_y = curr_y + move_y[i]
if (valid_move(new_x, new_y, board)) {
board[new_x, new_y] = pos
if (solve(board, new_x, new_y, move_x, move_y, pos+1)) {
return (TRUE)
}
board[new_x, new_y] = -1
}
}
}
main <- function() {
sims = 10
ctr = 0
number_of_moves = c()
solve(board, 1, 1, move_x, move_y, pos)
print(paste("Minimum number of moves: ", pos))
}
main()
Thanks!
The problem is that the python code relies on short-circuiting to prevent out-of-bounds errors. & will not short-circuit so you need to use &&.
Here is an example
FALSE && stop()
#> [1] FALSE
FALSE & stop()
#> Error:
Update valid_move to this
valid_move <- function (x, y, board) {
# Changed to && to allow short-circuiting
# if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
if (x >= 1 && y >= 1 && x <= 8 && y <= 8 && board[x, y] == -1) {
return (T)
}
return (F)
}
I have an Angular 7 app with a home page containing a large coloured block (enough to fill the page) at the top with a header and some images. I want to put some lava effect animations into the background similar to this
code in case link is removed:
HTML:
<canvas id="lamp-anim" class="lamp-anim" width="1034" height="613"></canvas>
CSS:
body {
background: #f857a6; /* fallback for old browsers */
background: -webkit-linear-gradient(to top, #ff5858, #f857a6); /* Chrome
10-25, Safari 5.1-6 */
background: linear-gradient(to top, #ff5858, #f857a6); /* W3C, IE 10+/
Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
JS:
window.lavaAnimation = function() {
"use strict";
var t, i = {
screen: {
elem: null,
callback: null,
ctx: null,
width: 0,
height: 0,
left: 0,
top: 0,
init: function(t, i, s) {
return this.elem = document.getElementById(t), this.callback = i || null, "CANVAS" == this.elem.tagName && (this.ctx = this.elem.getContext("2d")), window.addEventListener("resize", function() {
this.resize()
}.bind(this), !1), this.elem.onselectstart = function() {
return !1
}, this.elem.ondrag = function() {
return !1
}, s && this.resize(), this
},
resize: function() {
var t = this.elem;
for (this.width = t.offsetWidth, this.height = t.offsetHeight, this.left = 0, this.top = 0; null != t; t = t.offsetParent) this.left += t.offsetLeft, this.top += t.offsetTop;
this.ctx && (this.elem.width = this.width, this.elem.height = this.height), this.callback && this.callback()
}
}
},
s = function(t, i) {
this.x = t, this.y = i, this.magnitude = t * t + i * i, this.computed = 0, this.force = 0
};
s.prototype.add = function(t) {
return new s(this.x + t.x, this.y + t.y)
};
var h = function(t) {
var i = .1,
h = 1.5;
this.vel = new s((Math.random() > .5 ? 1 : -1) * (.2 + .25 * Math.random()), (Math.random() > .5 ? 1 : -1) * (.2 + Math.random())), this.pos = new s(.2 * t.width + Math.random() * t.width * .6, .2 * t.height + Math.random() * t.height * .6), this.size = t.wh / 15 + (Math.random() * (h - i) + i) * (t.wh / 15), this.width = t.width, this.height = t.height
};
h.prototype.move = function() {
this.pos.x >= this.width - this.size ? (this.vel.x > 0 && (this.vel.x = -this.vel.x), this.pos.x = this.width - this.size) : this.pos.x <= this.size && (this.vel.x < 0 && (this.vel.x = -this.vel.x), this.pos.x = this.size), this.pos.y >= this.height - this.size ? (this.vel.y > 0 && (this.vel.y = -this.vel.y), this.pos.y = this.height - this.size) : this.pos.y <= this.size && (this.vel.y < 0 && (this.vel.y = -this.vel.y), this.pos.y = this.size), this.pos = this.pos.add(this.vel)
};
var e = function(t, i, e, n, a) {
this.step = 5, this.width = t, this.height = i, this.wh = Math.min(t, i), this.sx = Math.floor(this.width / this.step), this.sy = Math.floor(this.height / this.step), this.paint = !1, this.metaFill = r(t, i, t, n, a), this.plx = [0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0], this.ply = [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1], this.mscases = [0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 0, 2, 1, 1, 0], this.ix = [1, 0, -1, 0, 0, 1, 0, -1, -1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1], this.grid = [], this.balls = [], this.iter = 0, this.sign = 1;
for (var o = 0; o < (this.sx + 2) * (this.sy + 2); o++) this.grid[o] = new s(o % (this.sx + 2) * this.step, Math.floor(o / (this.sx + 2)) * this.step);
for (var l = 0; e > l; l++) this.balls[l] = new h(this)
};
e.prototype.computeForce = function(t, i, s) {
var h, e = s || t + i * (this.sx + 2);
if (0 === t || 0 === i || t === this.sx || i === this.sy) h = .6 * this.sign;
else {
h = 0;
for (var r, n = this.grid[e], a = 0; r = this.balls[a++];) h += r.size * r.size / (-2 * n.x * r.pos.x - 2 * n.y * r.pos.y + r.pos.magnitude + n.magnitude);
h *= this.sign
}
return this.grid[e].force = h, h
}, e.prototype.marchingSquares = function(t) {
var i = t[0],
s = t[1],
h = t[2],
e = i + s * (this.sx + 2);
if (this.grid[e].computed === this.iter) return !1;
for (var r, n = 0, a = 0; 4 > a; a++) {
var l = i + this.ix[a + 12] + (s + this.ix[a + 16]) * (this.sx + 2),
d = this.grid[l].force;
(d > 0 && this.sign < 0 || 0 > d && this.sign > 0 || !d) && (d = this.computeForce(i + this.ix[a + 12], s + this.ix[a + 16], l)), Math.abs(d) > 1 && (n += Math.pow(2, a))
}
if (15 === n) return [i, s - 1, !1];
5 === n ? r = 2 === h ? 3 : 1 : 10 === n ? r = 3 === h ? 0 : 2 : (r = this.mscases[n], this.grid[e].computed = this.iter);
var p = this.step / (Math.abs(Math.abs(this.grid[i + this.plx[4 * r + 2] + (s + this.ply[4 * r + 2]) * (this.sx + 2)].force) - 1) / Math.abs(Math.abs(this.grid[i + this.plx[4 * r + 3] + (s + this.ply[4 * r + 3]) * (this.sx + 2)].force) - 1) + 1);
return o.lineTo(this.grid[i + this.plx[4 * r] + (s + this.ply[4 * r]) * (this.sx + 2)].x + this.ix[r] * p, this.grid[i + this.plx[4 * r + 1] + (s + this.ply[4 * r + 1]) * (this.sx + 2)].y + this.ix[r + 4] * p), this.paint = !0, [i + this.ix[r + 4], s + this.ix[r + 8], r]
}, e.prototype.renderMetaballs = function() {
for (var t, i = 0; t = this.balls[i++];) t.move();
for (this.iter++, this.sign = -this.sign, this.paint = !1, o.fillStyle = this.metaFill, o.beginPath(), i = 0; t = this.balls[i++];) {
var s = [Math.round(t.pos.x / this.step), Math.round(t.pos.y / this.step), !1];
do s = this.marchingSquares(s); while (s);
this.paint && (o.fill(), o.closePath(), o.beginPath(), this.paint = !1)
}
};
var r = function(t, i, s, h, e) {
var r = o.createRadialGradient(t / 1, i / 1, 0, t / 1, i / 1, s);
return r.addColorStop(0, h), r.addColorStop(1, e), r
};
if (document.getElementById("lamp-anim")) {
var n = function() {
requestAnimationFrame(n), o.clearRect(0, 0, a.width, a.height), t.renderMetaballs()
},
a = i.screen.init("lamp-anim", null, !0),
o = a.ctx;
a.resize(), t = new e(a.width, a.height, 6, "#3494E6", "#EC6EAD")
}
return {
run: n
}
}();
if (document.getElementById('lamp-anim')) {
lavaAnimation.run();
}
setTimeout(function() {
$('.js-works-d-list').addClass('is-loaded');
}, 150);
Is it possible to convert/do this in angular animations? Are they flexible enough to do this sort of (what id call advanced) animation?
I think the question of 'can I convert this to Angular' is a bit off because Angular runs on Typescript, which is a language built from javascript. So, yes you can do all this in Angular or rather using Typescript within an Angular app.
We're always here to help once you get some code written in an Angular app! But in general, we are here to help you were you get stuck in code and help you solve the problem. It's a bit more challenging to say 'yes it will work' without seeing how you implement it in your project and can't really guide or help you until we see how your angular components are written.
Short answer: Yeah, I think it can work. But it also depends how you implement this code into your Angular app.
I am trying to find the probability that the chain jumps from state k-1 to state 1 before it hits state k.
Can anyone spot my mistake?
I tried to simulate the markov chain, but i want to make a code that allows me to find probability of k ={1, 2, 3, ........17}. But I can really not get the code.
This is the error message I always get
Error in while (X[i] > 1 && X[i] < k) { :
missing value where TRUE/FALSE needed
Here is my code:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{ i <- 1 #no. of steps
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
I think the line
i <- 1 #no. of steps
should not be there. Try this:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
You're setting X to k-1. In R, that's treated as a vector of length 1. As soon as i reaches 2, X[i] return an index error, because X does not have a second element.
Further notes: using the same index in two different nesting levels is very bad form. Also, when you start having a massive list of if-then-else statements, it's time to rethink your code. In this case, you could just subset 1:17 on p1[i] >=Y, take the minimum value, and then set X to that.
I am trying to code following recursion problem. Every time I run the test case it gives me out of bound error on the else if line. How do I fix the out of bound error. under the code is the test case.
static boolean allEqual(int[] a, int start, int end) {
if( a[start + 1]>=a.length){return false;}
if (start == end && a[start] == a[end]) {
return true;
}
///recursive
else if (a[start] == a[start + 1]) // Error over here
{
return allEqual(a, start + 1, end);
}
// else if (start == end && a[start] == a[end]) {
// return true;
else {
return false;
}
}
// Following is the test case
Random rng = new Random(SEED);
for(int i = 0; i < RUNS; i++) {
int[] a = new int[i];
int v = rng.nextInt();
for(int j = 0; j < i; j++) {
a[j] = v;
}
assertTrue(rp.allEqual(a, 0, i-1));
for(int j = 1; j < i; j++) {
assertTrue(rp.allEqual(a, j, i-1));
assertTrue(rp.allEqual(a, 0, j));
a[j] = a[j] - 1;
assertTrue(rp.allEqual(a, 0, j-1));
assertTrue(rp.allEqual(a, j + 1, i-1));
assertFalse(rp.allEqual(a, 0, i-1));
a[j] = a[j] + 2;
assertTrue(rp.allEqual(a, 0, j-1));
assertTrue(rp.allEqual(a, j + 1, i-1));
assertFalse(rp.allEqual(a, 0, i-1));
a[j] = a[j] - 1;
}
}