In this article, we will create a python function that will produce an array with the numbers 0 to N-1 in it.
For example, the following code will result in an array containing the numbers 0 to 4:
arr(5) // => [0,1,2,3,4]
There are a few rules we need to follow here:-
- when the user passes in 0 to the above function, the function will return an empty list.
- when the user passes in an empty argument into the above function, the function will also return an empty list.
- any other positive number will result in an ascending order array.
Below is the full solution to the above problem.
def arr(n=None): li = [] if n == 0 or n == None: return li else: for i in range(n): li.append(i) return li
Write down your own solution in the comment box below this post
from Planet Python
via read more
No comments:
Post a Comment