As a newbie programmer

Exponentia! @ Dcoder

Dcoder icon

Problem: Create a code that allows people to learn about exponents.

The task is to write a code which takes an integer number as an input and returns the values ranging from that of 2(two) raised to power 0(zero) to 2(two) raised to power n(the number specified in input)

Input: Input contains the number 'n' for which all the values are returned from power of 2 from 0 to n.

Output: Output contains the values ranging from 2(two) raised to power 0(zero) to 2(two) raised to power n(input) separated by a comma.

Note: For testcases with negative number as input, 2 raise to power 0 will be considered as 1.0 instead of 1 whereas for other negative powers the trailing zeros in output will be truncated like 0.50 is 0.5 , 0.25 is 0.25, 0.1250 is 0.125

Constraints: -1000 ≤ n ≤ 1000

Sample Input: 5

Sample Output: 

1,2,4,8,16,32

n = int(input())
n += -1 if n < 0 else 1
if n < 0:
  print('1.0,' + ','.join([str(2**i).rstrip('0') for i in range(-1, n, -1)]))
else:
  print(','.join([str(2**i) for i in range(n)]))

How do you rate this article?

2


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Publish0x Publish0x

Reward the author with $0.01 in crypto, and earn yourself as you read!

20% to author / 80% to me.
Rewards are FREE. Publish0x pays them, not you.

Page not displaying correctly?