Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to convert a list of digits to an integer add 1 and convert it back to a list of digits in Python

1 Answer

0 votes
import math

def convert_array_of_digits_to_int_number(lst):
    n = 0
    for digit in lst:
        n = n * 10 + digit
    return n

def convert_int_number_to_array_of_digits(digits, n):
    i = len(digits) - 1
    while n > 0:
        digits[i] = n % 10
        n //= 10
        i -= 1

lst = [9, 4, 6, 9]
n = convert_array_of_digits_to_int_number(lst)

n += 1

convert_int_number_to_array_of_digits(lst, n)

print(f"n = {n}")
print(lst)



'''
run:
  
n = 9470
[9, 4, 7, 0]

'''

 



answered May 7, 2024 by avibootz
...