If input is [1,2,3] then how get output [1,2,4]... | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 3

If input is [1,2,3] then how get output [1,2,4]...

Note that the numbers are in array..

18th Jun 2018, 4:25 AM
desai amber
desai amber - avatar
9 ответов
+ 5
In Python a = [1,2,3] b = [2**(i-1) for i in a] print(a) print(b)
19th Jun 2018, 5:26 PM
Muhammad Hasan
Muhammad Hasan - avatar
+ 8
arr[2] += 1 Without more context, this is all I can produce.
18th Jun 2018, 4:41 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
input : x output : 2^(x-1)
18th Jun 2018, 5:21 AM
Muhammad Hasan
Muhammad Hasan - avatar
+ 3
Assuming, input = [1, 2, 3, 4, 5,...] 1) Let (output) be an empty array [ ]. 2) Use a for loop and let (x) be the i'th item of the array (input). 3) Set i'th item of the (output) to : 3.1) 2 to the power of (x - 1). Then the output will be [1, 2, 4, 8, 16,...]. 3.2) (x + 1) if i is greater than equal to 2 and (x) if not. Then the output will be [1, 2, 4, 5, 6,...]. 3.3) (x + 1) if i is equal to 2 and (x) if not. Then the output will be [1, 2, 4, 4, 5,...] Depending on the output you want choose any one of 3.1, 3.2 or 3.3 and this concept will work on any language that supports assignment, if statement and for statement.
18th Jun 2018, 11:02 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 3
desai amber Please choose a specific programing language and an example of the output array with more than three values. Ex : python, [1, 2, 4, 8, 16...] Then we can help you to figure out the appropriate equation and also make you understand it.
19th Jun 2018, 5:42 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 2
This is the code for python: import math input = [1, 2, 3, 4, 5] output = [] for i in input: output.append(math.pow(2, i - 1)) print(output) This is the code for JavaScript: var input = [1, 2, 3, 4, 5]; var output = []; for (var i = 0; i < input.length; i++) { output.push(Math.pow(2, input[i] - 1)); }
19th Jun 2018, 5:54 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 1
I understand both of your answers.. Thanks Muhammad hasan and Cyrus Ornob Corraya..
19th Jun 2018, 7:14 PM
desai amber
desai amber - avatar
0
ok
18th Jun 2018, 4:43 AM
desai amber
desai amber - avatar
0
Please friend Cyrus Ornab Corraya.. Please type full programme in sequence.. I don't know about more in Array structure..
19th Jun 2018, 12:26 PM
desai amber
desai amber - avatar