【每日一题0902】累加求和

:woman_mage:给定一个正整数,请编写一个python函数,将它的字面数字进行累加总和,并列出算式。例如1234 ,那么返回1 + 2 + 3 + 4 = 10

题目难度:简单
题目来源:codewars

def sum_of_digits(num: int) -> str:         
    pass

assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
1 个赞
def sum_of_digits(num: int):
    s = 0
    y = ''
    for i in range(len(str(num))):
        s += int(str(num)[i])
        y += str(num)[i] + " + "

    return y[:-2] + f'= {s}'


assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
assert sum_of_digits(80) == "8 + 0 = 8"
2 个赞
def sum_of_digits(num: int) -> str:
    sum = 0
    result_list = list()
    size = len(str(num))
    for i in range(size):
        sum += int(str(num)[i])
        result_list.append(str(num)[i])
        result_list.append(" ")
        if i == size - 1:
            break
        result_list.append("+")
        result_list.append(" ")
    result_str = "".join(result_list)
    return result_str + "=" + " " + str(sum)

print(sum_of_digits(1234))
assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
1 个赞
    public String sumOfDigits(Integer integer) {
        String res = "";
        Integer sum = 0;
        String s = Integer.toString(integer);
        String[] split = s.split("");
        for (String s1 : split) {
            sum+= Integer.parseInt(s1);
            res += s1 + " + ";
        }
        String substring = res.substring(0, res.length() - 2);
        substring  += "= " + sum;
        return substring;
    }

    @Test
    public void TestSumOfDigits() {
        assert sumOfDigits(1234).equals("1 + 2 + 3 + 4 = 10");
        assert sumOfDigits(64323).equals("6 + 4 + 3 + 2 + 3 = 18");
        assert sumOfDigits(8).equals("8 = 8");
    }
1 个赞
def sum_of_digits(num: int) -> str:
    list_num = list(str(num))
    result = sum([int(i) for i in list_num])
    str_num = f"{' + '.join(list_num)} = {result}"
    return str_num

assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
1 个赞
def sum_of_digits(arg):
    # 列表生成式,将输入参数的组成元素放到列表中
    digits_list = [int(i) for i in str(arg)]
    # 列表求和
    sum_digits = sum(digits_list)
    # 用 + 号连接参数的每个元素
    list_str = ' + '.join(str(arg))
    # 格式化输出
    return f'{list_str} = {sum_digits}'


assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
2 个赞
def sum_of_digits(num: int) -> str:
    ss=""
    a =0
    strings = str(num)
    num = list(strings)
    for i in num:
        if i==num:
            return num +" = "+num
        ss=ss + " + "+str(i)
    for i in num:
        a =a+int(i)
    zong = ss + " = "+str(a)
    sss = zong[3:]
    return sss
def test_sum_of_digits():
    assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
    assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
    assert sum_of_digits(8) == "8 = 8"
1 个赞
def sum_of_digits(num: int) -> str:
    return  f"{' + '.join(list(str(num)))} = {sum(map(int,list(str(num))))}"


assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
1 个赞

assert sum_of_digits(1234) == “1 + 2 + 3 + 4 = 10”

def sum_of_digits(num: int) → int:
sum = 0
expression = ‘’
for i in range(len(str(num))):
sum = sum + int(str(num)[i])
expression = expression + “+” + str(num)[i]
expression = expression[1:] + “=” + str(sum)
return expression

在此处键入或粘贴代码
1 个赞
int a = 1234;
        String str = String.valueOf(a);
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            num += Integer.parseInt(String.valueOf(str.charAt(i)));
        }
        System.out.println(num);
2 个赞

1 个赞

def sum_of_digits(num):
prefix =
sum = 0
for i in range(0, len(num)):
shang, yushu = divmod(int(num), 10)
sum += yushu
prefix.append(str(yushu))
num = shang
prefix.reverse()
print(("+".join(prefix)+"={}").format(sum))

if name == ‘main’:
num = input(“请输入一个整数:”)
if not num.isdigit():
print(“输入错误!请重新输入!”)
sum_of_digits(num)

1 个赞

package com.company;

public class Main {

public static void main(String[] args) {
    sum_of_digits(1234);
}
public static void sum_of_digits(int num){
    String str_num = "" + num;
    int length_num = str_num.length();
    int sum = 0;
    StringBuffer sb = new StringBuffer();
    for(int i=0;i<length_num;i++){
        int shang = num/10;
        int yushu = num%10;
        sum += yushu;
        num=shang;
        sb.append(yushu);
    }
    String result_prefix = "";
    for(int i=sb.length()-1;i>=0;i--){
        if(i==0) {
            result_prefix += String.valueOf(sb.charAt(i));
        }
        else {
            result_prefix += sb.charAt(i) + "+";
        }
    }
    System.out.println(result_prefix+"="+sum);
}

}

1 个赞
def sum_of_digits(num):
    prefix = []
    sum = 0
    for i in range(0, len(num)):
        shang, yushu = divmod(int(num), 10)
        sum += yushu
        prefix.append(str(yushu))
        num = shang
    prefix.reverse()
    print(("+".join(prefix)+"={}").format(sum))

if __name__ == '__main__':
    num = input("请输入一个整数:")
    if not num.isdigit():
        print("输入错误!请重新输入!")
    sum_of_digits(num)
1 个赞
def sum_of_digits(num: int) -> str:
    list1=list(str(num))
    result=sum([int(i) for i in list1])
    s=f"{' + '.join(list1)} = {result}"
    return s



assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
def sum_of_digits(num: int) -> str:
    nums = str(num)
    num2 = ' + '.join(list(nums))
    sum = 0
    for i in range(0, len(nums)):
        num1 = nums[i:i + 1]
        sum += int(num1)
        i += 1
    result = str(f"{num2} = {sum}")
    return result


assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"

def sum_of_digits(num: int) → str:
sum = 0
for item in list(str(num)):
sum += int(item)
return ’ + '.join(list(str(num))) + ’ = ’ + str(sum)

assert sum_of_digits(1234) == “1 + 2 + 3 + 4 = 10”
assert sum_of_digits(64323) == “6 + 4 + 3 + 2 + 3 = 18”
assert sum_of_digits(8) == “8 = 8”

def sum_of_digits(num: int) -> str:
    sum = 0
    for item in list(str(num)):
        sum += int(item)
    return ' + '.join(list(str(num))) + ' = ' + str(sum)

assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
def sum_of_digits(num: int) -> str:
    sum_list = list(str(num))
    return ' + '.join(sum_list) + ' = ' + str(sum([int(i) for i in sum_list]))


assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"
def sum_of_digits(num: int) -> str:
    res = " + ".join(data for data in str(num))
    return f"{res} = {eval(res)}"


assert sum_of_digits(1234) == "1 + 2 + 3 + 4 = 10"
assert sum_of_digits(64323) == "6 + 4 + 3 + 2 + 3 = 18"
assert sum_of_digits(8) == "8 = 8"