programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Scott Selikoff
Bartenders:
• Piet Souris
• Jj Roberts
• fred rosenberger

# devideSelf challenge

Ranch Hand
Posts: 842
2
• Number of slices to send:
Optional 'thank-you' note:
I am working on below challenge
https://codingbat.com/prob/p165941

We'll say that a positive int divides itself if every digit in the number divides into the number evenly. So for example 128 divides itself since 1, 2, and 8 all divide into 128 evenly. We'll say that 0 does not divide into anything evenly, so no number with a 0 digit divides itself. Note: use % to get the rightmost digit, and / to discard the rightmost digit.

dividesSelf(128) → true
dividesSelf(12) → true
dividesSelf(120) → false

I wrote code as below

I am failing some tests. Can you please advise what mistakes i am doing here?

Sheriff
Posts: 17007
298
• Number of slices to send:
Optional 'thank-you' note:
Analyze your condition on line 6. What is that really saying?

Sheriff
Posts: 27235
87
• Number of slices to send:
Optional 'thank-you' note:
Put your test cases into two groups, one where the code returns true and the other where the code returns false. Do you find anything interesting?

sai rama krishna
Ranch Hand
Posts: 842
2
• Number of slices to send:
Optional 'thank-you' note:
Below finally worked

It passed all tests

x is the new assigned number of n which used for chop off and while loop
digit is reminder digit
I have to check n reminder by dividing with digit and chop off that x to get new digit every time in the for loop

Marshal
Posts: 75836
361
• 1
• Number of slices to send:
Optional 'thank-you' note:
This is what I got trying to avoid multiple return;:-If I divide up the expressions slightly, it might become easier to understand:-

sai rama krishna
Ranch Hand
Posts: 842
2
• Number of slices to send:
Optional 'thank-you' note:
Looks interesting.  I will take some time to digest above code.

Is it bad thing to have multiple returns?

Can I use for loop instead of while loop here?

can I use for loop interchangeably wherever while is used or there are any restrictions for for loop? I got used to for loop more than while loop

Campbell Ritchie
Marshal
Posts: 75836
361
• Number of slices to send:
Optional 'thank-you' note:

sai rama krishna wrote:Looks interesting. . . .

Thank you.

Is it bad thing to have multiple returns?

Only in theory. Remember that under the principles of structured programming, multiple return is never necessary, but most people see no harm in it.

Can I use for loop instead of while loop here?

Try it and see.

can I use for loop interchangeably wherever while is used . . .

Remember the while loop is the most basic type in theory and all other loops are derivatory forms. So, no. You can write a while loop to cover just about every case of iteration but trying to do it with a different kind of loop can be very awkward at best.

I got used to for loop more than while loop

In which case it would be good for you to learn the other kinds of iteration (for, for‑each, while, do‑while, with and without Iterators), recursion, and Streams.

Junilu Lacar
Sheriff
Posts: 17007
298
• Number of slices to send:
Optional 'thank-you' note:

sai rama krishna wrote:Is it bad thing to have multiple returns?

Not necessarily. Rather than ask that question, however, it's more useful to know when it's fine to have multiple returns/exit points.

1. When the method is small and the return statements are easy to see. In the case of your code that works, it's small enough to be able to see that there are two exit points.

2. It's easy to reason when any exit point is taken vs the other(s). Again, in your case, anyone familiar with the pattern will see that the return inside the loop is when a specific condition is met and further iterations of the loop are not necessary. You've found what you were looking for and returned a definitive result. The return outside the loop is the "fallback" or "fall through" condition, where you exhausted all possibilities and were unable to terminate the loop. In your case, the second return statement which gives back true is executed when no digits are found that do not evenly divide into the given number.

3. If there is/are guard clauses that can help you avoid performing more work than you need to or can simplify the structure of the code. Guard clauses go at the top and prevent execution of the rest of the method if certain conditions are not met.

Consider understandability and simplicity when deciding whether multiple exit points are appropriate. My general rule of thumb is that if the method is small (<= 10 lines of code) and it's clear what the exit conditions are, then multiple returns are fine.

sai rama krishna
Ranch Hand
Posts: 842
2
• Number of slices to send:
Optional 'thank-you' note:
Thank you Ritchie, Lacar

I never saw &= operator. What this represent?

If number of lines more if we put more return statements visibility is lost right?

What is meaning of guard clauses? Can you please give some simple example for that?

Paul Clapham
Sheriff
Posts: 27235
87
• Number of slices to send:
Optional 'thank-you' note:

sai rama krishna wrote:I never saw &= operator. What this represent?

You know that "a += b" is the same as "a = a + b", right? Now I invite you to write a similar explanation for "&=".

If number of lines more if we put more return statements visibility is lost right?

I'm not sure what you mean by that. If you put more lines into a method, eventually it will be too long to fit into your text editor's screen and therefore the whole method will not be visible at one time. You could describe that as "visibility lost" but I suspect you didn't mean that.

What is meaning of guard clauses? Can you please give some simple example for that?

Here we have a guard clause whose purpose is to filter out trivial or even incorrect inputs. You'll find it at the beginning of a method, before the code gets serious.

sai rama krishna
Ranch Hand
Posts: 842
2
• Number of slices to send:
Optional 'thank-you' note:
Thank you Paul.

"a &= b" is the same as "a = a & b" right not a && b? This is first time I ever saw this expression. Is it in new edition of java or i just never got chance to look earlier i saw others like you mention more often a+=b and a-=b  and a*=b and a/=b

Gaurd clause is same as edge case? I hear people calling edge case for similar check at the beginning of method.

Regarding other question if number of lines of code in a method more than 10 lines say 15 lines if i use multiple returns what is the downside or disadvantage of it? As most of my challenges I used returns liberally without thinking about this type rule.

Paul Clapham
Sheriff
Posts: 27235
87
• Number of slices to send:
Optional 'thank-you' note:

sai rama krishna wrote:"a &= b" is the same as "a = a & b" right not a && b? This is first time I ever saw this expression. Is it in new edition of java?

Yes, of course. And it's not new, it's been in Java since Java 1. I've never found any use for it either.

Gaurd clause is same as edge case? I hear people calling edge case for similar check at the beginning of method.

Not exactly.  A guard clause is just there to take care of situations which aren't really part of the problem, usually. Imagine somebody going to a concert -- the first thing they encounter is a ticket-taker. If they don't have a ticket then they are just sent away. That's the guard clause. Otherwise the ticket-taker will continue with the regular routine, which might involve sending them to a particular part of the arena depending on their ticket, or checking the ticket against a list for special treatment, or whatever.

Whereas an edge case is just something which needs to be handled differently from the other cases. There might be extra handling (e.g. avoiding looking past the end of an array) or less handling, or different handling, or even no handling at all. If it's no handling, you could certainly implement it by putting the code at the beginning of the method. That would look like a guard clause if you only think of where the code is located, but from the design point of view it's not the same.

Junilu Lacar
Sheriff
Posts: 17007
298
• Number of slices to send:
Optional 'thank-you' note:
Guard clause is a common term in the industry. You can find many examples with a simple search: Guard clause

Campbell Ritchie
Marshal
Posts: 75836
361
• Number of slices to send:
Optional 'thank-you' note:

sai rama krishna wrote:. . . "a &= b" is the same as "a = a & b" right not a && b? . . .

I have never considered that question; I can only remember using &= once more than Paul C
Have a look in the Java® Language Specification (=JLS).

sai rama krishna
Ranch Hand
Posts: 842
2
• Number of slices to send:
Optional 'thank-you' note:
Thank you

I see some other interesting operators as below in that link which i never used or saw

AssignmentOperator:
(one of)
=  *=  /=  %=  +=  -=  <<=  >>=  >>>=  &=  ^=  |=

Paul Clapham
Sheriff
Posts: 27235
87
• Number of slices to send:
Optional 'thank-you' note:

sai rama krishna wrote:

AssignmentOperator:
(one of)
=  *=  /=  %=  +=  -=  <<=  >>=  >>>=  &=  ^=  |=

I may have used one or two of those in the past... it looks like any binary operator can be made into an assignment operator in that way. Or am I wrong?

Campbell Ritchie
Marshal
Posts: 75836
361
• Number of slices to send:
Optional 'thank-you' note:
Not && and ||. Nor can I find any evidence of instanceof=

Campbell Ritchie
Marshal
Posts: 75836
361
• Number of slices to send:
Optional 'thank-you' note:

Paul Clapham wrote:. . . I may have used one or two of those in the past... . . .

People use += frequently. As we have found, some of the others are used but very rarely.
Beware of those compound operators on different datatypes; as the JLS link I posted earlier says, there is an implicit cast in there somewhere.