Sunday, October 11, 2020

IslandT: Write a python function that produces an array with the numbers 0 to N-1 in it

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:-

  1. when the user passes in 0 to the above function, the function will return an empty list.
  2. when the user passes in an empty argument into the above function, the function will also return an empty list.
  3. 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

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...