Home > OS >  Program print numbers which sum is highest
Program print numbers which sum is highest

Time:12-16

Program go through the list and print number which digits have the highest sum. I have an idea of getting digits of numbers and saving it to a list and doing the same for the next number but in other list.Then compare these two lists and print the number with the highest list.

I tried something but it don't work how I wanted it.

brojevi = [21, 35, -43, 2, 80, -1, 7, 0]

tmax = []
tmax2 = []

for i in brojevi:
    broj1 = i
    broj2 = i  1
    while broj1 > 0:
        cifra1 = broj1 % 10
        tmax.append(cifra1)
        broj1 //= 10
        print(cifra1)
        print(broj1)
    print("-----------------")
    while broj2 > 0:
        cifra2 = broj2 % 10
        tmax2.append(cifra2)
        broj2 //= 10
        print(cifra2)
        print(broj2)
    print("-----------------")

zbir1 = sum(tmax)
print("-----------------")
zbir2 = sum(tmax2)
print(zbir1)
print(zbir2)
if zbir1 > zbir2:
    print("najveci: ", i)
print(tmax)
print(tmax2)

Prints in code are for test purposes.

CodePudding user response:

brojevi = [21, 35, -43, 2, 80, -1, 7, 0]

# see https://stackoverflow.com/a/14940026/1701600
def sum_digits(n):
    s = 0
    while n:
        s  = n % 10
        n //= 10
    return s
    
max_cross_sum = -1
max_num = None
for num in brojevi:
    cross_sum = sum_digits(num)
    
    if cross_sum > max_cross_sum:
        max_cross_sum = cross_sum
        max_num = num
        
print(f"max cross sum is ${max_cross_sum} for number ${max_num}")

CodePudding user response:

I would construct a new list with tuples containing the sum of each number and the original number. Then you can get the max sum from this list. Finally iterate over the list, compare if the sum in the tuple equals the max sum and then print the matching value.

brojevi = [21, 35, -43, 2, 80, -1, 7, 0]
totals = [(sum(int(digit) for digit in str(number) if digit.isdigit()), number) for number in brojevi]
print(totals)
max_sum = max(totals, key=lambda x: x[0])[0]
print(max_sum)
for entry in totals:
    if entry[0] == max_sum:
        print(f'Highest number is {entry[1]}')

Output (including debugging outputs):

[(3, 21), (8, 35), (7, -43), (2, 2), (8, 80), (1, -1), (7, 7), (0, 0)]
8
Highest number is 35
Highest number is 80

This approach only takes digits into account. The "-" is dropped.

CodePudding user response:

you can use the key parameter of the max function. This will allow you to get the number with the highest sum of digits by supplying the key parameter with a lambda (or function) to get the sum for an individual number's digits:

brojevi = [21, 35, -43, 2, 80, -1, 7, 0]

result = max(brojevi,key=lambda n:sum(map(int,str(abs(n))))) # 35
  • str(abs(n)) will convert the number n into a string (without the sign)
  • map(int,...) will convert each character of the string into an integer
  • sum(...) will add up the integers produced by map() giving the sum of digits
  • max(brojevi,key=lambda n:...) will find the number in brojevi that has the highest key value (in this case sum of digits)

CodePudding user response:

Use the sum_digits3 function from Sum the digits of a number

import numpy as np 

def sum_digits3(n):
    r = 0
    while n:
        r, n = r   n % 10, n // 10
    return r

brojevi = [21, 35, -43, 2, 80, -1, 7, 0]

sumed_brojevi = [sum_digits(abs(n)) for n in brojevi]
brojevi[np.argmax(sumed_brojevi)]

CodePudding user response:

Try max() method

newlist = max(yourlist)

print(newlist)

And if you want to get index of the maxvalue than

inlistmaxvalue = yourlist.index(newlist)
  • Related