Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to convert the following javascript code to python.
let speed = 50;
const lightCount = 1;
const lights = [[200, 10];
const isRed = (speed, dist, dur) =>
(18 * dist) % (10 * speed * dur) >= (5 * speed * dur);
for(let i = 0; i < lightCount; i++) {
if (isRed(speed, lights[i][0], lights[i][1])) {
speed--; // Reduce speed
i = -1; // Start again
}
}
console.log(speed);
I tried the following in python, but it is not working:
speed = 50
light_count = 1
lights = [[200, 10]
def is_red(speed, dist, dur):
if (18 * dist) % (10 * speed * dur) >= (5 * speed * dur):
return True
return False
for i in range(light_count):
if is_red(speed, lights[i][0], lights[i][1]):
speed -= 1
i = -1
print(speed)
The answer is supposed to be 36 but it is showing 49.
It always loops only once.
Please suggest a solution.
Thanks in advance
Editing i will not step the loop back. ints are immutable, so you are just editing the state of a temporary variable. You can try using a while loop, and just incrementing back i.
Example:
speed = 50
light_count = 1
lights = [[200, 10]
def is_red(speed, dist, dur):
if (18 * dist) % (10 * speed * dur) >= (5 * speed * dur):
return True
return False
i = 0
while i < light_count:
if is_red(speed, lights[i][0], lights[i][1]):
speed -= 1
# i -= 1
#i += 1
else:
i += 1
del i
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I don't know what is the problem with this code I just need to know
how many number from Start to end that divide by 7 or has 7.
const divideOrHasSeven = (start, end) => {
var x = 0;
for (i = start; i <= end; i++) {
if (i % 7 || i.toString().indexOf("7")) { // if it is divide by 7 or has 7
x += 1;
}
}
return x;
};
The problem is i.toString().indexOf('7') will always return a truthy value EXCEPT (ironically) when 7 is actually in the number (inthe first position - index zero)
Change your conditional to if (i%7===0 || i.toString().indexOf('7')>-1)
You had some problems with your code.
If you compare with this, I think you will understand where.
This i.toString().indexOf('7') will return -1 if 7 is not found in string, which will be evaluated as true.
You also had some syntax errors.
const divideOrHasSeven = (start, end) => {
var x = 0;
for (i = start; i <= end; i++) {
if (i%7 === 0 || i.toString().indexOf('7') >= 0) {
x += 1;
}
}
return x
}
console.log(divideOrHasSeven(1, 200));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
You need to always be true in the console)
Without gaps, without rounding to the integer, without changing the comparison operator and the general calculation logic, as well as the loop
condition )
P.S. This is an interview task for junior level ...
for (let i = 0; i <= 10; i++) {
console.log(i + ':' + ((i * 0.1) == (i / 10)));
}
// needed to always true in console.log
I would just create a function to compare the difference to a threshold e.g. 0.001.
/**
* Determines if two floating value are equal within a threshold
* #param {Number} a - First floating value
* #param {Number} b - Second floating value
* #param {Number} [t=0.001] - Threshold of difference
* #return {boolean} Whether the difference is less than the threshold
*/
const equalsFloat = (a, b, t = 0.001) => Math.abs(a - b) < t;
for (let i = 0; i <= 10; i++) {
console.log(`${i}: ${equalsFloat(i * 0.1, i / 10)}`);
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How do i calculate the number of trailing zeros in a factorial of a given number.
N! = 1 * 2 * 3 * 4 ... N
Any Help on this?
Because zeros come from factors 5 and 2 being multiplied together, iterate over all numbers from 1 to the input number, adding to a cumulative count of fives and twos whenever those factors are found. Then, return the smaller of those two counts:
function zeroCount(n) {
let fives = 0;
let twos = 0;
for (let counter = 2; counter <= n; counter++) {
let n = counter;
while (n % 2 === 0) {
n /= 2;
twos++;
}
while (n % 5 === 0) {
n /= 5;
fives++;
}
}
return Math.min(fives, twos);
}
console.log(zeroCount(6)); // 720
console.log(zeroCount(10)); // 3628800
It is very simple, This will help you.
function TrailingZero(n)
{
var c = 0;
for (var i = 5; n / i >= 1; i *= 5)
c += parseInt(n / i);
return c;
}
Let me know if you need help to understand this function.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The issue I am having is I'm trying to get the alert to tell the person if the random number generated is in the ranges to be an A,B,C,D, or F. All that happens is it says all the numbers are F's not matter what.
var grade = (Math.floor(Math.random() * 100 + 1));
document.write(Math.floor(Math.random() * 100 + 1))
if (grade >= 90) {
alert("A");
} else if (grade >= 80) {
alert("B");
} else if (grade >= 70) {
alert("C");
} else if (grade >= 60) {
alert("D");
} else {
alert("F");
}
The problem is that you're checking a different number than you're writing with document.write here:
var grade = (Math.floor(Math.random() * 100 + 1));
document.write(Math.floor(Math.random() * 100 + 1))
This would need to be:
var grade = (Math.floor(Math.random() * 100 + 1));
document.write(grade);
In order to accurately reflect what is happening.
The problem is that your not generating a random value properly.
Math.random() returns a random number between 0 (inclusive) and 1 (exclusive). Try using this statement.
var grade = Math.floor((Math.random() * 100));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to pass a floating-point number as the second argument (exponent) to Math.pow(). I always get NaN when the passed number is not a whole number, like 0, 2, 7, you name it. Is there a working way in javascript to make this work?
(function () {
var notice = document.getElementById('notice');
var value = 0.0;
var interval = 0.02;
var timeInterval = 10;
function interpolation(x) {
var y = Math.pow(Math.e, x); // <<< HERE >>>
console.log(x, y);
return y;
}
function animation() {
var callAgain = true;
if (value >= 1) {
value = 1.0;
callAgain = false;
}
notice.style['opacity'] = interpolation(value);
notice.style['marginTop'] = (value * 20 + 20) + 'px';
value += interval;
if (callAgain) {
setTimeout(animation, timeInterval);
}
}
animation();
})();
PS: Please don't comment, that an opacity greater than 1 doesn't make any sense. I know that e^x; x > 0 will yield values greater than 1. I'll insert a proper function when I got this working.
The constant is Math.E, not Math.e