Skip to content

Python. Find max sum of digits without arrays




The task is. You have an array of numbers, you should find the max sum of the digits. The numbers are natural.

Constraints: you can not use arrays.

The most interesting part of this code is to get single digits from a number.

#The task is. You have an array of numbers, you should find the max sum of the digits. The numbers are natural.
#Constraints: you can not use arrays

n = 5
max_sum = 0

for i in 123,334,555,1222:
  sum = 0
  is_parsed = False
  step = 0
  const_i = i 
  while(is_parsed == False):
    if step == 0:
      dig = i % 10
    else :
      dig = int((const_i // 10 **step) %10)      
    i = i / 10
    if i < 1:
      is_parsed = True    
    step += 1
    sum += dig
  if sum > max_sum:
    max_sum = sum
      
print('Max sum of digits is: ', max_sum)
     

The link for my tutorial tasks is here: https://replit.com/@alexbbell/Module10#module10_7.py




No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment