Skip to content

Sum of Digits

Testcase generation code for Sum of Digits task on CodeChef.

generate.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import logging

from testcase_maker.generator import TestcaseGenerator
from testcase_maker.values import ValueGroup, NamedValue, RandomInt, LoopValue, ValueRef

logging.basicConfig(level=logging.INFO)

# Firstly, create a new container to make the testcase stdin structure.
values = ValueGroup()

# The first line contains an integer T, the total number of testcases.
values.add(NamedValue(
    name="T",
    value=RandomInt(min=1, max=1000)
))

values.newline()

# Then follow T lines, each line contains an integer N.
values.add(LoopValue(
    value=RandomInt(min=1, max=1000000),
    amount=ValueRef(name="T"),
    delimiter="\n"
))


# Generate the testcases
generator = TestcaseGenerator(values=values, answer_script="./solution.py")
generator.generate()

solution.py

1
2
3
4
5
6
7
8
9
T = int(input())

for _ in range(T):
    n = int(input())
    res = 0
    while n:
        res += n % 10
        n //= 10
    print(res)