Jump to content

Help with A Logic Problem

- - - - -

  • Please log in to reply
12 replies to this topic

#1
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
So there's a website called CodingBat that people can use to improve their Java and Python programming skills. There's a specific problem that I just can't get through, the "makeBricks" problem on the Java Logic-2, the very first one. Here's my solution so far:

public boolean makeBricks(int small, int big, int goal) {
  if (small >= goal || (big * 5) >= goal)
     return true;
  else if (small + (big * 5) >= goal)
     return true;
  
  return false;
}
Any help is greatly appreciated.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
There's a logic error with (big * 5) >= goal.

Suppose our goal is 13, and we pass in 3 big bricks into the function. since 3*5 = 15, and 15 is >= 13, the function would return true, even though it is not possible to construct a goal of 13 inches using only 5 inch bricks.

#3
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
Ooh wait, I think I got it now. When I was doing the problem, I was thinking that it doesn't matter how the 1-inch bricks and the 5-inch bricks would combine, as long as it's at least equal to the goal.

But, when I change it to == instead of >=, I just get more errors.


UPDATE:
I believe I get the concept now, but I'm just trying to figure out how to select just the right amount of 1-inch bricks and 5-inch bricks and get the goal. So, let's say the goal is 18, and there's 4 1-inch bricks and 5 5-inch bricks. So the program should only take 3 1-inch bricks and 3 5-inch bricks. But how do I make the program do that... hmm...

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your first if statement is not needed.

You need to:
1) Check if there are enough inches to fill the goal.
2) Check if there are enough small bricks at the end to satisfy goal (because five inch bricks can't satisfy 13, so is there 3 small blocks left?) Hint: The modulus operator will help.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I just checked. Alexander's solution works.

#6
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
So are you saying I can use the modulus operator to see how many 5-inch bricks the program should get? Using the example I gave above, the program should find that only 3 out of the 5 available 5-inch bricks should be used, so that would be 15 inches, and there's 4 1-inch bricks, so there's enough to reach the goal. But how exactly can the modulus operator be used to find that 3?

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
goal % 5 will essentially do this: goal / 5, then tell you the remainder. If your goal is 13, then the remainder will be 3. Do you have enough small bricks to fill 3? You would need to add that condition to your code as well.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
By the way, can we put an if statement inside an else-if statement?

UPDATE:
Phew, I finally got it! Here's the solution that I found:
public boolean makeBricks(int small, int big, int goal) {
   if (small == goal || big * 5 == goal)
      return true;
   else if ((big * 5) != goal && goal % 5 <= small && (big * 5) + small >= goal)
      return true;
   
   return false;
}
If there's a simpler, or easier-to-read, solution, please let me know.

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your first if statement is unneeded because the second would handle if there is enough for the goal.. Your second portion is correct! Although you can combine the first and last part in to
if(small + (big * 5) >= goal && goal % 5 <= small)
First checking if there are enough big and small bricks, second purely checking if there are enough small ones to satisfy results that are above or below five.

By the way, I am hooked on the site now!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I've been looking for this site! I ran across it while I was in high school.
I'm glad you solved your problem.

#11
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts

Alexander said:

Your first if statement is unneeded because the second would handle if there is enough for the goal.. Your second portion is correct! Although you can combine the first and last part in to
if(small + (big * 5) >= goal && goal % 5 <= small)
First checking if there are enough big and small bricks, second purely checking if there are enough small ones to satisfy results that are above or below five.

By the way, I am hooked on the site now!
Oh, that's right, thanks!

lethalwire said:

I've been looking for this site! I ran across it while I was in high school.
I'm glad you solved your problem.
I know, this site's awesome! It's like a game!

#12
heartybowl

heartybowl

    Newbie

  • Members
  • PipPip
  • 17 posts
wow I just noticed that all my homework assignments for my java class in college have been coming from that site! lol w t f

anyways this is my teacher's answer to that problem

if (goal / 5 <= big)

{

    if (goal % 5 <= small)

          result = true;

    else

          result = false;

}

else

{

    if (goal - big * 5 <= small)

         result = true;

    else

         result = false;

}

}




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users