We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Python doesn't support interfaces(and doesn't need to). Python has powerful multiple inheritance anyways, so languages like Java which have single inheritance only have to cope by inculcating "implementable" interfaces.

Why does this get deleted? I want to see what was downvoted. Does no one believe in freedom of speech anymore that even when something obviously bad is written and downvoted by many members of the community we can't see what is not generally accepted? I want to see how NOT to think and this doesn't help that I can't see what they said that was downvoted so much.

"powerful multiple inheritance" which is more often abused than drugs leading to some of the most ridiculously structured and embarasing code. Responsible developers of other languages which support MI (like C++) caution against it's misuse; way too easy to go around shooting yourself in the foot.

I've actually used interfaces with Python. It's just creating a class that only has methods, and make sure not to initialize it, just inherit the methods. They are called mixins no?

Intefaces has totally different purposes than multiple inheritance. It's an important part of big project development. Interfaces similar to abstract classes, but has some difference for specific purposes. Interfaces more flexible than abstract classes, but still preserve implementation of some methods in inhereted class.

what is wrong in my code.Iam not get output
public int divisorSum(int n)
{
int sum =0;
int i=0;
while( i<=n)
{
if(n%i==0)
sum=sum+i;
i++;
}
return sum;
}

You're right, if we were talking prime numbers, sqrt(n) would make sense, but not for divisors.

[Edit]: Let me correct myself, if you add i and n / i at the same step, you're adding the other number that is also a divisor in same step, take for example 8, if i is 2, you'll end up adding sum += 2 + 8/2 which traduces to sum += 2 + 4, both 2 & 4 are divisors of 8, keep doing that and you'll soon notice that this way the i variable only needs to go up to sqrt(n).

Here is a tip, a number's divisor will always be 1 and the number itself.
So you can initialize the sum as n+1 itself.
And the start the loop from i=2 till i<=n/2 because the greatest divisor of the number will not be greater than its half. Hence by this you shorten your for loop.
Also place a condition at start for n=1 then just return 1. Otherwise that test case wont be successful

This one worked for me..
class Calculator implements AdvancedArithmetic{
public int divisorSum(int n){
int sum=n;
for(int i=1;i<=n/2;i++){
if(n%i == 0){
sum=sum+i;
}
}
return sum;
}
}

Because Python doesn't have (and doesn't need) a formal Interface contract, the Java-style distinction between abstraction and interface doesn't exist. If someone goes through the effort to define a formal interface, it will also be an abstract class. The only differences would be in the stated intent in the docstring.

And the difference between abstract and interface is a hairsplitting thing when you have duck typing.

Java uses interfaces because it doesn't have multiple inheritance.

This range doesn't make any sense.
After the middle you don't need to check all values. You should have taken the range(1, n//2+1).
And the beginning value of summ is n. Because the number can always be divided by itself.

basically u just need to make a fucntion to check if a n is divisible by a number from 1 to 1000 or not, and summ all the numbers that are divisors of n.

class Calculator(AdvancedArithmetic):
def divisorSum(self, n):
p=0
e=0
for i in range(1,1001):
e=(n%i)
if e==0:
p+=i
return p
pass

class Calculator(AdvancedArithmetic):
def divisorSum(self, n):
factor_sum = 0
for i in range(1, n+1):
if n % i == 0:
factor_sum += i
return(factor_sum)

## Day 19: Interfaces

You are viewing a single comment's thread. Return to all comments →

No Python?! :-(

My thoughts, too. I've done everything so far in Python 3, pity that I either switch to another language or forgo completing this programming problem.

Python doesn't support interfaces(and doesn't need to). Python has powerful multiple inheritance anyways, so languages like Java which have single inheritance only have to cope by inculcating "implementable" interfaces.

What does this have to do with interfaces in OOP?

You are confusing Interfaces and GUI for the same thing

this cracked me up... xD

"EASILY"

LOL........

Interfaces in OO != Interfaces in GUI!!!

Why does this get deleted? I want to see what was downvoted. Does no one believe in freedom of speech anymore that even when something obviously bad is written and downvoted by many members of the community we can't see what is not generally accepted? I want to see how NOT to think and this doesn't help that I can't see what they said that was downvoted so much.

I remember this. The guy was talking about GUI interfaces which has nothing to do with interfaces in object oriented programming.

He probably deleted his own comment, don't know.

They could have used the closest thing to interfaces in Python, i.e. Abstract Base Classes.

There was already an exercise for that though.

"powerful multiple inheritance" which is more often abused than drugs leading to some of the most ridiculously structured and embarasing code. Responsible developers of other languages which support MI (like C++) caution against it's misuse; way too easy to go around shooting yourself in the foot.

I've actually used interfaces with Python. It's just creating a class that only has methods, and make sure not to initialize it, just inherit the methods. They are called mixins no?

Intefaces has totally different purposes than multiple inheritance. It's an important part of big project development. Interfaces similar to abstract classes, but has some difference for specific purposes. Interfaces more flexible than abstract classes, but still preserve implementation of some methods in inhereted class.

in python interfaces are supported they are implicit though we could have built something equivelent it just would not be typed as an interface.

C++ has powerfull multiple inheritance too, but It don't thow away interfaces!

what is wrong in my code.Iam not get output public int divisorSum(int n) { int sum =0; int i=0; while( i<=n) { if(n%i==0) sum=sum+i; i++; } return sum; }

Your counter for the while loop starts at 0. It will compute the remainder modulo 0 in this first case, which does not make any sense.

change into int i = 1; insted of int i = 0;

Another point to note, is that After n/2, the only factor of n will be n itself. This reduces the running time of the whole algorithm

sqrt(n) will reduce it more.

just add i and n/i(if a pure int)

how is this correct for a number like 20? 10 is larger than sqrt(20), am confused here

You're right, if we were talking prime numbers,

`sqrt(n)`

would make sense, but not for divisors.[Edit]:Let me correct myself, if you add`i`

and`n / i`

at the same step, you're adding the other number that is also a divisor in same step, take for example`8`

, if`i`

is`2`

, you'll end up adding`sum += 2 + 8/2`

which traduces to`sum += 2 + 4`

, both`2`

&`4`

are divisors of`8`

, keep doing that and you'll soon notice that this way the`i`

variable only needs to go up to`sqrt(n)`

.yes i got it thank you very much

If n=1, then this would return 2, no? even though it should return 1.

Use proper format when u paste code.

Here is a tip, a number's divisor will always be 1 and the number itself. So you can initialize the sum as n+1 itself. And the start the loop from i=2 till i<=n/2 because the greatest divisor of the number will not be greater than its half. Hence by this you shorten your for loop. Also place a condition at start for n=1 then just return 1. Otherwise that test case wont be successful

i think this would fail for the case of 1, n+1 will be 2 where the correct answer is just 1

To account for a special case like 1, you one can leverage the ternary operator to assign value to sum like so:

This will mitigate logic errors in code later on.

thanks! this worked for me

This one worked for me.. class Calculator implements AdvancedArithmetic{ public int divisorSum(int n){ int sum=n; for(int i=1;i<=n/2;i++){ if(n%i == 0){ sum=sum+i; } } return sum; } }

put that i++ outside of if condition.

int sum=0; int i=1; while(i<=n) { if(n%i==0) { sum+=i; } i++; }

Sadly, I, too, had to fall back to annother language. I choose C# and I had to remember the semicolons and variable declaration.

This is what i found on stackoverflow

Because Python doesn't have (and doesn't need) a formal Interface contract, the Java-style distinction between abstraction and interface doesn't exist. If someone goes through the effort to define a formal interface, it will also be an abstract class. The only differences would be in the stated intent in the docstring.

And the difference between abstract and interface is a hairsplitting thing when you have duck typing.

Java uses interfaces because it doesn't have multiple inheritance.

http://stackoverflow.com/questions/372042/difference-between-abstract-class-and-interface-in-python

This is lame, they should ask us to answer with an abtract class in Python...

crying to see there's no such an option.....won't be able to complete all 30 questions, unless I turn back to learn Java T.T

This would do it in python:

x = int(input("your input"))

sum = 0 for i in range(1, x + 1): if x % i == 0: sum += i else: i += 1 return sum

PYTHON3 SIMPLE SOLUTION!

Simple one-liner:

or using an in-built function is_integer()

return sum([ii for ii in range(1, n+1) if (n/ii).is_integer()])

This range doesn't make any sense. After the middle you don't need to check all values. You should have taken the range(1, n//2+1). And the beginning value of summ is n. Because the number can always be divided by itself.

basically u just need to make a fucntion to check if a n is divisible by a number from 1 to 1000 or not, and summ all the numbers that are divisors of n.

class Calculator(AdvancedArithmetic): def divisorSum(self, n): factor_sum = 0 for i in range(1, n+1): if n % i == 0: factor_sum += i return(factor_sum)