convert javascript to python : looping for in python [closed] - javascript

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 5 years ago.
Improve this question
I have the javascript code and i want to convert it to python.
var dice = [1, 3, 4, 1]
for(let i = 0; i < dice.length; i++) {
if(dice[i] == 1 ) {
dice.splice(i, 1)
break;
}
}
whats wrong?
q = [1, 2,3, 4]
for i, dice in q:
if dice[i] == 1:
dice = splice(i, 1)
break
print dice
I want if the number one appears simultaneously then the number 1 is discarded.
How the python code for the js code above?

q = [1, 2,3, 4]
for dice in q:
if dice == 1:
q.remove(dice)
break
print(q);

Try:
dice = [1, 3, 4, 1]
newval = ''
for i in range(len(dice)):
if dice[i] == 1:
newval = dice[1:i]
print(newval)

Related

Want to Sort with Loop In JS [closed]

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
let fruits = [mango,banana,avocado,apple,orange,lychee];
let prices = [50,90,65,300,600,900]; // not constant value;
//Solution with If else
if(prices > 0 && prices <= 50) console.log("Mango#0-50")
if(prices > 51 && prices <= 65)console.log("Mango#0-50<br>Banana#51-65")
//So on
Is there any way to short it with loop?
This is how the result should look like
Mango#0-50
Banana#51-65
avocado#65-90
apple#91-300
orange#301-600
lychee#601-900
rest#>901
Note: I do not want to use If else;
let i = 1
fruits.map(fruit => `${fruit.name}#${i}-${i+=100}`);
You could map the fruits with their price range and slice the array by the wanted length and return a joined string.
function getValues(price) {
return temp
.slice(0, (prices.findIndex(p => price <= p) + 1) || prices.length + 1)
.join('<br>');
}
const
fruits = ['mango', 'banana', 'avocado', 'apple', 'orange', 'lychee'],
prices = [50, 90, 65, 300, 600, 900].sort((a, b) => a - b),
temp = [...fruits.map((f, i, { length }) => `${f}#${prices[i - 1] + 1 || 0}-${prices[i]}`), `rest#>${prices[prices.length - 1] + 1}`];
console.log(getValues(100));
console.log(getValues(300));
console.log(getValues(301));
console.log(getValues(1000));

Increase values of elements in an array [closed]

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 3 years ago.
Improve this question
Say I have an array like so...
let myArr = [0,0,2,0,0];
I want to create sort of a ripple effect such that the result of the array is [0,1,2,1,0]
This would give you the result you expect:
let myArr = [0, 0, 2, 0, 0];
createRippleArray = (myArr) => {
if (myArr.length % 2 === 0) {
console.error("createRippleArray: Array length needs to be odd number>1");
return [];
}
let midIndex = ~~(myArr.length / 2);
let mid = myArr[midIndex];
return myArr.map((e, i) => {
let res;
if (i < midIndex) {
return ~~(mid / Math.abs(midIndex - i + 1));
} else if (i === midIndex) {
return mid;
} else if (i > midIndex) {
return ~~(mid / Math.abs(midIndex - i - 1));
}
});
}
console.log(createRippleArray(myArr));
Hope this helps!

how to find the sum of all integers present in a string which is divisible by 3? [closed]

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
Find the sum of all the numbers in a string which is divisible by 3 and also find the last such number (Use JavaScript). Example “The best 6 of 8 will get 9 points”, sum = 15, last=9.
Sure - use split, reduce and filter with % (modulo) for divisibility:
const str = "The best 6 of 8 will get 9 points";
const strArr = str.split("");
const threesArr = strArr.filter(e => parseInt(e) % 3 == 0);
const sumOfThrees = threesArr.reduce((acc, curr) => acc + parseInt(curr), 0);
const allNumbers = strArr.filter(e => parseInt(e));
const lastNumber = allNumbers[allNumbers.length - 1];
console.log("Sum: " + sumOfThrees);
console.log("Last: " + lastNumber);

Convert integers to string and then adding using recursion [closed]

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 4 years ago.
Improve this question
Hi I am trying to solve a problem where the input for a function digital_root(n) will add the digits. I am not sure what I am doing wrong.
function digital_root(n) {
// ...
//1. separate n into array of digits
var nString = n.toString();
//[ '1', '2', '3', '4' ]
var numbersToAdd = [];
var total = 0;
for (var i = 0; i < nString.length; i++) {
numbersToAdd.push(+nString.charAt(i));
}
// result is [ 1, 2, 3, 4 ]
//2. add digits
for (var x = 0; x < numbersToAdd.length; x++) {
total += numbersToAdd[i];
//expected outputs
// total = 0 + numbersToAdd[0]--> 0+1--> total = 1
// total = 1 + numbersToAdd[1]-->1+2--> total = 3
// total = 3 + numbersToAdd[2]-->3+3--> total = 6
// total = 6 + numbersToAdd[3]-->6+3--> total = 9
}
return total;
}
console.log(digital_root(1234));
You need to use "x" instead of "i"
So changing
total += numbersToAdd[i];
to
total += numbersToAdd[x];
will fix an issue.
Also output should be 10 instead of 9, there is calculation mistake in your question

How do I make a numerical array's increase amount smaller in each step? [closed]

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 8 years ago.
Improve this question
I would like to make a numeric array which increases in every step.
[1,200,400,600,800,1000, .... , 10000]
But I need to make the amount by which it increases progressively smaller in every step. For example,
[1, 200, 300, 350, 325, 312.5, ....., 10000]
If anybody knows the solution, please give me some ideas.
Thank you.
Change the increment amount as you please...
var arr = [];
var i = 1;
var incrementAmt = 2000;
for(var j = 0; j < 1000; j++) {
var num = i + incrementAmt;
arr.push(num);
i = num;
i++;
incrementAmt = incrementAmt / 2; // cause incrementer to decrease each iteration
}
console.log(arr)

Categories

Resources