• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simplify if condition  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m trying to simplify an if condition as much as possible and i m curious if it can be made more short then i managed to .The problem is simple , a integer number with 3 digits is generated and the user is asked to input another 3 digits integer number ... One of the conditions that need to be verified is if all the digits in each number match but not exactly ( aka 123 and 132 ) .Using only an if statement to verify this what is the most short condition that can be checked to identify when the digits in a 3 digits number match the digits of another 3 digits integer number (not interested if they match exactly) .

The simplest condition i managed to find is :



Is there a better(shorter ) condition that can be used ?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of problem can be made much simpler using some logic and the Java API

Here is one proposed way you can accomplish such a task:

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:This sort of problem can be made much simpler using some logic and the Java API

Here is one proposed way you can accomplish such a task:



I m using a book to learn and in that book this problem is meant to be resolved using only an if statement ( even for statement / char data type ware not introduced yet and i dont know allot about java arrays at my current study level) . Can you look for a shorter condition inside a simple if statement ?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see ... let me think
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this


But i wonder if you will find it shorter
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No strings
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work anyway - if the guess is 222 and the lottery number is 123 it will return true.

A similar problem occurs in your solution Tiberius.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are perfectly right Tony. I think i needed to check the other way to like:


which makes it too long :-)

Is it correct now?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:which makes it too long :-)

YES.

Guys (specifically Paul and Tiberius): DontWriteLongLines. They make your thread very hard to read.

And that applies whether you write them yourself OR include them in a quote.

I've broken up all of yours THIS TIME. Any more, and you're on your own.

Winston
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

DontWriteLongLines

Ok, Winston. I will try to stick to the rule next time.
Regards
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:Using only an if statement to verify this what is the most short condition that can be checked to identify when the digits in a 3 digits number match the digits of another 3 digits integer number (not interested if they match exactly) .

This is a simple permutation problem, and doesn't jive with your other statement:
"I m trying to simplify an if condition as much as possible"

If you're NOT allowed to use anything other than an if statement involving the three separate digits, then you can't simplify it. The permutations are what they are, even if digits are duplicated (and checking for that, while it might make the logic work faster, is also likely to make it bigger).

There are, however, several ways of simplifying it if you CAN use other logic. My suggestion:
1. Put the digits in Sets (java.util.Set).
2. Check the lengths of those sets. If they're different, the digits can't possibly match.
3. (if needed) Put the digits in Lists (java.util.List), sort them, and check if they are identical. Hopefully, that one is self-explanatory.

Winston


[Edit] Actually, there is ONE check you could do (although it's an addition to all the others): check if the numbers that the digits come from are the same before you embark on your "digit permutation odyssey".
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There are, however, several ways of simplifying it if you CAN use other logic. My suggestion:
1. Put the digits in Sets (java.util.Set).
2. Check the lengths of those sets. If they're different, the digits can't possibly match.
3. Put the digits in Lists (java.util.List), sort them, and check if they are identical. Hopefully, that one is self-explanatory.


Winston, i am waiting to hear from Tiberius. I am almost sure he wouldn't like to use Sets ot Lists as he said that he has has just begun the language. By the way, could you post some code that achieves what you had said?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could simply do:

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid any more confusion i m going to post the whole exercise .Also remember that while it can be solved with strings/arrays or other objects the problem is in the third chapter and strings/ characters / objects / the for statement has not been introduced . What was introduced is the if statement and all the logical operators . My level of knowledge ends at objects (no knowledge of arrays /stacks because i didnt get that far yet.


*3.15 (from the book Introduction to Java Programming, Tenth Edition by Daniel Liang)
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a three-digit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:

1. If the user input matches the lottery number in the exact order, the award is $10,000.

2. If all digits in the user input match all digits in the lottery number, the award is $3,000.

3. If one digit in the user input matches a digit in the lottery number, the award is $1,000

This is Lottery.java :



 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct solution(at list i think so ) that can verify the rule 2 is :


If you tell me it cannot be a more simple condition in the if statement i believe you !
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The existence of so many != operators entails a simpler solution in my opinion.
You are doing it wrongly by trying to solve the problem in code. What you should do is solve the problem on a sheet of paper. Write down exactly how you would solve it on paper, and then when you have got it nice and simple, you can turn it into code easily.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And i will say that it is better that your if statement reads:

to avoid confusion
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:And i will say that it is better that your if statement reads:

to avoid confusion


Think it is incorrect in the sense 111 and 100 will pass the condition ...
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Think it is incorrect in the sense 111 and 100 will pass the condition


You are right, i am making the same mistake as previously. What about this:
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This I think works but the one posted earlier by me is simpler i think ( the one with != )
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:And i will say that it is better that your if statement reads:

Paul,

After all my work, and my post (and your reply), you post ANOTHER piece of code that is FAR TOO LONG.

Please edit it, because this thread is now (again) unreadable for me.
(Unlike you, I don't have 20:10 vision )

Winston

[Edit] And I notice that it has now been quoted by both of you.
If you simply want to swap replies, carry on; if you want to get answers from the people here:
EDIT YOUR POSTS AND BREAK UP THOSE LINES (and now you'll both have to do it).
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This I think works but the one posted earlier by me is simpler i think ( the one with != )


I think the one using != is not correct. You are also doing the same mistake.
You need to apply a ! to your if condition (like if(!expression)) for it to work.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , your correct . Forgot to negate the whole condition .
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pancil and paper. When you have got it worked out on paper, then you can easily translate it into code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!