Thursday, January 31, 2019

codingdirectional: Add one to the last element of a list

Hello and welcome to another one day one answer series. In this article, we will create a python method which will do the following actions.

  1. Takes in a list of numbers.
  2. If the number is less than 0 or it is a double digits number then returns None.
  3. If it is an empty list then returns None.
  4. Else joins the numbers in the list together to create a larger number, for example, if a list consists of [1, 2, 3] then the larger number will become 123. Next, add one to that new number, so 123 becomes 124.
  5. Finally turns that new number back to a list, so 124 becomes [1, 2, 4] and returns that new list.

Below is the solution for this question.

def up_array(arr):

    str_ = ''
  
    if(len(arr) == 0):
        return None
        
    for num in arr:
        if(num < 0 or num > 9):
            return None
        else:
            str_ += str(num)
    
    total = str(int(str_) + 1)
    arr = []

    for number in total:
        arr.append(int(number))

    return arr

That is it, hope you like this article.

Announcement:

I have decided to restart one of mine old websites again starting from today onward. This website is not about programming but if you are interested in online stuff and software then do visit this site regularly through this link! I have just created my first article about bitcoin on this site which you can read through this link. Like, subscribe to this new site if you want to! This new site has a lot of goodies that I would like to show it to you all!



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