#Python - Perfect numbers - fails 5 assertion tests

15 messages · Page 1 of 1 (latest)

leaden zephyr
#

Hi, all! I'm trying to solve https://exercism.org/tracks/python/exercises/perfect-numbers/ with this code:
`"""
A function to find if a number is perfect, abundant or deficient.
https://exercism.org/tracks/python/exercises/perfect-numbers
"""
def classify(number: int) -> str:
"""Find if a number is perfect, abundant or deficient based on its aliquot sum.
A perfect number equals the sum of its positive divisors, abundant number is less than
its aluquot sum, otherwise, it is a deficient number.

:param number: int a positive integer
:return: str the classification of the input integer
"""
if number <= 0:
    raise ValueError("Classification is only possible for positive integers.")

divisors = []
for i in range(1, number):
    if number % i == 0:
        divisors.append(i)
        
if sum(divisors) == 1 or sum(divisors) > number:
    return "deficient"
if sum(divisors) == number:
    return "perfect"
else:
    return "abundant"

`
The problem is that it fails couple of the tests and I am not sure why. I've applied screenshots to the assertion errors that are easy to see and that's why I can't understand why the answers are like that:
12 -> [1, 2, 3, 4, 6] -> the sum is 16 -> should be deficient
30 -> [1, 2, 3, 5, 6, 10, 15] -> the sum is 42 -> should be 'deficient'
4 -> [1, 2] -> the sum is 3 -> should be 'abundant'
32 -> [1, 2, 4, 8, 16] -> the sum is 31 -> should be 'abundant'
Can someone help me what am I missing here? The output of the tests is completely illogical to me.

Exercism

Can you solve Perfect Numbers in Python? Improve your Python skills with support from our world-class team of mentors.

#

Don't mind the last test with the classify(1), I fixed that.

drowsy light
#

Is there a possibility that 'deficient' and 'abundant' have been reversed?

vapid elm
#

Python - Perfect numbers - fails 5 assertion tests

leaden zephyr
#

I read it again and again and i don’t think that this is the problem

drowsy light
#

12 -> [1, 2, 3, 4, 6] -> the sum is 16 -> should be deficient
I thought 12 was 'abundant'.

leaden zephyr
#

ohhhhhhhhhy

#

i messed it completely

#

ok, so the number should be less than the sum, not the other way around

#

sorry, i understand it now

#

thanks, @drowsy light!

drowsy light
#

I hope the test goes well for you!

gloomy fossil
#

@leaden zephyr did you get it to work?

leaden zephyr
#

yes, sorry for the late response