• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boolean condition

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehe.. Howdy! i am trying to get a better grasp of Boolean conditions so i can come up with solutions to problems.
While doing some programming exercises i ran into following assignment. Anyway, right to business:

Write a program that reads four integers and prints “two pairs” if the input consists
of two matching pairs (in some order) and “not two pairs” otherwise. For example,
1 2 2 1 two pairs
1 2 2 3 not two pairs
2 2 2 2 two pairs
x y z k

My attempt:

For the inputs that are shown as examples my Boolean condition enters the if block as it should. But in case i enter 1 3 3 3, or 3 1 3 3, or 3 3 1 3
the if statement is still entered, and it shouldn't be since there are no two pairs and, so the else statements should be executed.

Could someone help me out understand how i should come up with correct boolean condition.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is how I read your if condition:

if ( ( (any time x is part of a pair) AND ( y is part of a pair that doesn't include x)) OR (z is not the same as k) )

Let's see how that would evaluate an example that should fail:


What I would do is break down problem into discreet steps. The first thing you need to know is that, with 4 elements, in order to get two pairs, X must be in a pair with one other element:
(x is the same as y) OR (x is the same as z) OR (x is the same as k)
Given that, in order to have two pairs, there is a requirement on the only two remaining elements left - they must be equal as well, so there is a remaining condition that must be met:
((x is the same as y) WHAT (z is the same as k)) OR ((x is the same as z) WHAT (y is the same at k)) OR ((x is the same as k) WHAT (y is the same as z))
Your mission is to determing the word that should be used insted of WHAT, and then to translate that to JAVA.
 
S. Freeman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must say that i didn't expect such a fast and great response Steve, thank you it really helped, i was worried that my understanding of boolean operations sucked, but it seems that my approach to solving the problem and logic behind my thoughts was the primary reason for my fail.

Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic