• 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

PosNeg Problem

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying one of codingbat problem

I am trying one codinbat problem.  get some tests fail with below code



But below code pass all tests

What is difference between above two code snippets. To my eyes they look same except separate if statement in second code snippet?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you say in English what algorithm the code is supposed to be implementing?

Note: a == 0 is NOT negative
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes cleaning up your code helps. The difference is when 'negative' is false. (That's not very clear, is it?)
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the problem
https://codingbat.com/prob/p159227

Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.


posNeg(1, -1, false) → true
posNeg(-1, 1, false) → true
posNeg(-4, -5, true) → true


I should consider this  statement "return true if one is negative and one is positive." first
or this statement first "Except if the parameter "negative" is true, then return true only if both are negative."
Like more specific condition to generic or more generic condition to specific condition?

If negative true then return true if both numbers are negative
if one is negative and other is positive then also return true

 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. Negative is false not clear
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is difference between

below code snippet 1

and below code snippet 2
if( negative )
   {
       if( ( a <= 0 && b <= 0 ) )

Both above code snippet 1 and code snippet 2 means same?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They differ under what condition execution jumps to the 'else' statement.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why below code snippet 1 fails some tests


where as below code snippet 2 does not fail any tests


is it always good idea to put one additional if statement as in code snippet 2 than compressing code in one if statement as in code snippet 1?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:is it always good idea to put one additional if statement as in code snippet 2 than compressing code in one if statement as in code snippet 1?

Absolutly NOT. It depends on the relationships to any 'else' statements.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:why below code snippet 1 fails some tests

The answer would be more obvious if you knew which test(s) failed.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of these are wrong?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one passes the test.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got '0' in my output but your code and the problem description doesn't talk about '0'.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The test in codingbat is of course incorrect and incomplete because it doesn't allow for 0 as an input.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without '0'

 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I use below code snippet



It is failing below tests



\
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All failed cases are when negative is true.
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sai rama,

the condition you have is that you are dependent on that third argument:

if (!arg3) then !arg1 | !arg2
else !arg1 & !arg2

Les





sai rama krishna wrote:I am trying one of codingbat problem

I am trying one codinbat problem.  get some tests fail with below code



But below code pass all tests

What is difference between above two code snippets. To my eyes they look same except separate if statement in second code snippet?

 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which are arg1 and arg2 and arg3 ?
arg1 you mean negative
arg2 you mean a<=0
arg3 you mean b<=0
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if (!arg3) then !arg1 | !arg2
else !arg1 & !arg2

is related to code snippet 1 or code snippet 2?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This call posNeg(-1, 1, true) returns true when it should have returned false.

If you start with this code and then substitute T/F for each comparison for this set of data which is causing you a failure you get.

Does this help you see that the way the first 'if' is written causes the execution to jump  to the 'else', and then how the second 'if' returns 'true', which is incorrect?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This call posNeg(-1, 1, true)


else if((a<=0 && b>=0) || (b<=0 && a>=0) ){



 else if( ( T && T ) || ( F && F ) ) {

You mean above should be as below right as -1 is T case and 1 is F case?

 else if( ( T && T ) || ( F && T ) ) {
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:This call posNeg(-1, 1, true)


else if((a<=0 && b>=0) || (b<=0 && a>=0) ){



 else if( ( T && T ) || ( F && F ) ) {

You mean above should be as below right as -1 is T case and 1 is F case?

 else if( ( T && T ) || ( F && T ) ) {

I'm saying the 'else-if' should NEVER have been called in the first place which is how your corrected code behaved.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are times you can't make a long compound logical expression that will work. This is one of those times, as you found out when you modified it to the working example.

You had asked if it is a general rule that compound logic should be broken up, and the answer is no. But there are cases, like this one, where the answer is yes. In this case the compound expression caused a jump to an 'else' that we didn't want.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have give this as your signature: public boolean posNeg(int a, int b, boolean negative) so

arg1 would be your a,
arg2 would be your b and
arg3 would be your negative

if you premise is accurate then the given algorithm is the answer you should be testing against, and discussion of other code irrelevant.  if your premise is not accurate, discussion of any solution is futile.


sai rama krishna wrote:which are arg1 and arg2 and arg3 ?
arg1 you mean negative
arg2 you mean a<=0
arg3 you mean b<=0

 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any good examples on more compound logical expressions to get my head wrap around those concepts quickly? Most of google and youtube tutorials just talk about simple if else if else  expressions nothing much on compound logical expressions?
Now i see as if T && T & T  is not satisfied for 1, -1, true jumping to else if where  T &&T ||....always true irrespective of what is after ||

How to make use of XOR  ^ operator in this special case?



Above fails below tests

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are having problems because you are guessing about what expression to return. You can guess 1,000,000× and it is likely that some of those guesses will be correct. In this case it will be impossible to verify which is correct because the test seems not to use 0. That will make it easier for you to get the tests all to pass since you can test for non‑negative numbers instead of positive.
Work out the problem on paper/pseudocode/a text editor or similar before you try it on codingbat.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic