0

uVA 382(https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=318) is rather simple: Given a number, say whether it's a perfect, deficient or abundant number. But it seems that I have a problem with the formatting. Here's what the problem wants:

The first line of output should read ‘PERFECTION OUTPUT’. The next N lines of output should list for each input integer whether it is perfect, deficient, or abundant, as shown in the example below. Format counts: the echoed integers should be right justified within the first 5 spaces of the output line, followed by two blank spaces, followed by the description of the integer. The final line of output should read ‘END OF OUTPUT’.

Here's my code:

def sum_divisors(n):
    sm = 0
    for i in range(1, n):
        if n % i == 0:
            sm += i
    return sm
n = list(map(int, input().split()))
n.pop()
print("PERFECTION OUTPUT")
for i in n:
    sm = sum_divisors(i)
    if sm == i:
        state = "PERFECT"
    if sm < i:
        state = "DEFICIENT"
    if sm > i:
        state = "ABUNDANT"
    spaces = ' ' * (5 - len(str(i)))
    print("{}{}  {}".format(spaces, i, state))
print("END OF OUTPUT")

Now, I've already tried using uDebug with many different outputs and I've gotten the right answer and I don't think the problem is with the algorithm. I think it's about the formatting of the output but don't know what I got wrong.

1 Answers1

0

Is there a specific error message that faults your formatting? I don't see a problem with it offhand. Depending on how stringent the testing is, consider the difference between inputs:

15 28 6 56 60000 22 496 0

and:

15 28 6 56 60000 0 22 496

and how your code handles them. Strictly speaking, we end the input list of numbers at the zero. But your solution would include it and toss 496 instead.

Here's a rework of your code to fix that issue as well as style suggestions:

def sum_divisors(n):
    summation = 0

    for divisor in range(1, n // 2 + 1):
        if n % divisor == 0:
            summation += divisor

    return summation

numbers = map(int, input().split())

print("PERFECTION OUTPUT")

for number in numbers:
    if number == 0:
        break

    summation = sum_divisors(number)

    if summation < number:
        status = "DEFICIENT"
    elif summation > number:
        status = "ABUNDANT"
    else:
        status = "PERFECT"

    print("{:5}  {}".format(number, status))

print("END OF OUTPUT")

See if that works any better for you. Otherwise, include the exact error message that your original program produces.

cdlane
  • 40,441
  • 5
  • 32
  • 81