recursion

Place positive numbers before negative

自作多情 提交于 2021-02-17 05:13:29
问题 So I have this code which I found on the internet which takes an array of negative and positive numbers and rearranges the array so all negative numbers are before the positive ones. But the position of appearance of each number has to remain the same. So for example if i have -2 5 -9 ..... , in the organized array -2 still has to be the first number of the negative ones and -9 has to be the second of the negative ones, also 5 has to be the first number of the positive ones. Example: Input:

Place positive numbers before negative

半城伤御伤魂 提交于 2021-02-17 05:13:05
问题 So I have this code which I found on the internet which takes an array of negative and positive numbers and rearranges the array so all negative numbers are before the positive ones. But the position of appearance of each number has to remain the same. So for example if i have -2 5 -9 ..... , in the organized array -2 still has to be the first number of the negative ones and -9 has to be the second of the negative ones, also 5 has to be the first number of the positive ones. Example: Input:

How to group objects in an array and sort them depending on an object property

强颜欢笑 提交于 2021-02-17 04:30:53
问题 groupSelectedQuestions(selectedQuestions){ var questions = [ { q: 'why?', group: 'no-group', date: '1' }, { q: 'what?', group: 'group 1', date: '1' }, { q: 'when?', group: 'group 2', date: '2' }, { q: 'where?', group: 'group 1', date: '2' }, { q: 'which', group: 'group 2', date: '3' } ], result = questions.reduce(function (r, a) { r[a.group] = r[a.group] || []; r[a.group].push(a); return r; }, {}); /** more code here. Here I would put if statements that check for all condtions I have stated

Recursive function call in Python

最后都变了- 提交于 2021-02-16 20:54:45
问题 I am a beginner in Python and want to solve the following problem: Given a list of n integer numbers and an integer result the correct operators (+, -, *, /) shall be found that solve a corresponding simple math equation. Example: For numbers = [2, 3, 4, 1] and result=13 the following solution should be found: 2 + 3 * 4 - 1 = 13. I wrote the following code which finds a solution for n =4 by doing a brute force approach with 3 nested for loops in which the operators are alternated. I struggle

Recursive function call in Python

那年仲夏 提交于 2021-02-16 20:54:39
问题 I am a beginner in Python and want to solve the following problem: Given a list of n integer numbers and an integer result the correct operators (+, -, *, /) shall be found that solve a corresponding simple math equation. Example: For numbers = [2, 3, 4, 1] and result=13 the following solution should be found: 2 + 3 * 4 - 1 = 13. I wrote the following code which finds a solution for n =4 by doing a brute force approach with 3 nested for loops in which the operators are alternated. I struggle

Recursion output

元气小坏坏 提交于 2021-02-16 20:33:49
问题 I was just introduced to recursion and I was given the following lines of code: public class RecursionThree { public void run(int x ) { if(x<5) run(x+1); out.println(x); } public static void main(String args[] ) { RecursionThree test = new RecursionThree (); test.run(1); } } and the output is supposed to be: 5 4 3 2 1. I get why it would print 5 (because 5<5 would equal false and it would print x, which is 5). However, I do not understand why it prints 4 3 2 1 too. Thanks for your help 回答1:

Getting range numbers using recursion in JavaScript

江枫思渺然 提交于 2021-02-16 20:33:32
问题 I am trying to get the range of numbers using recursion. Can someone explain to me why it isn't working? function range(x,y){ var results = []; if(x === y){ return results; } return results.push(range(x + 1,y)); } range(1,5); 回答1: Try this: function rangeOfNumbers(startNum, endNum) { if (startNum - endNum === 0) { return [startNum]; } else { const numbers = rangeOfNumbers(startNum + 1, endNum); numbers.unshift(startNum); return numbers; } }; 回答2: The beauty of recursion is that you don't need

Track nested object keys with end value recursively

戏子无情 提交于 2021-02-16 20:24:10
问题 Given an object shaped like the following, (which can have unknown number of nested properties) const theme = { fonts: { primary: 'Arial', secondary: 'Helvetica' }, colors: { primary: 'green', secondary: 'red', }, margin: { small: '0.5rem', medium: '1rem', large: '1.5rem' } } Im trying to achieve the following: Loop through recursively until i hit a value that isn't an object When i hit this value i would like to have access to all the keys that lead up to it, along with the end value.

How does Recursion actually work in Java?

ぃ、小莉子 提交于 2021-02-16 19:44:40
问题 I'm just new to this Recursion and I know at least that it is a technique by which a method calls itself during execution but I am quite confused on how it actually works. From the book at school, implementing the factorial function is the first example that was given. //int n = 5; private static int factorial(int n) { if(n == 0) { return 1; } else return n * factorial(n - 1); } I get how this factorial works somehow but I am not completely sure if I understood it correctly. For every call

Recursively Search in Array with a For Loop

感情迁移 提交于 2021-02-16 15:18:12
问题 I know there are better ways to search an array, but I really want to understand how to return when the value is found in a recursive call. Logging when found isn't a problem, but I can't seem to make this return true when found. The problem is basic. Fully search multi-dimensional array for a value and return true if found or false if not found, using a for loop and recursion. I've tried returning the recursive function and everything else I can think of, but nothing works fully. function