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!

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Tryph Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Tryph
Guest
Tryph

I was trying to share a more pythonic solution but it seems there is no way to keep code formatting in comments… Tough