7 kyu
Breaking chocolate problem
17,985 of 51,783krikus
Loading description...
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This comment has been hidden.
Missing fixed and samples tests for following cases: (TODO: check in all languages ~~
m = 0, n = 0
m = 0, n = 1
m = 1, n = 0
In
Rust
, why is the input typeu32
instead of matching the output typeu64
?i guess because the output will always be bigger than the input. returning a
u64
allows the function to potentially handle very large inputsOk, I got it.
Setting the input type as
u32
is to ensure the data remains within that range.The output type is set to
u64
to prevent overflow in the result.However, the description doesn't specify the input range.
Therefore, using
u64
for the input type is also a valid choice.it's a bad desc!!!!!
noone can help you unless you determine your problems sir!
The trick is that you can solve it without understanding the problem. Just check at the suggested answer and produce an algorithm that will produce the expected answer. It's a no-brainer.
funny
This comment has been hidden.
Are you sure it's not 46x0 ?
This comment has been hidden.
Actual and expected are swapped in PHP
Fixed.
This comment has been hidden.
Ok, My code outputs the right answer but if I test it, i get passed 0, yet I have the same answer that should be asserted. Can someone kindly help.
Your solution is not correct, and the problems you are experiencing are not a kata issue.
Please see if this article is any helpful.
thanks I will check it out.
personal opinion but i feel that the problem should specify that each break can apply to one and only one piece of chocolate (i.e. you cannot have one break applying across multiple pieces of chocolate)
This task has really poorly written description, which lead you into wrong solution.
This comment has been hidden.
Same! Add in some Infinity, NaN, 'More please' etc. into the tests as well to get some basic validation and a bit of fun
Please review and approve, thanks !
.
No rand test in Scala
Are you sure? Random tests have been added to Scala translation a couple of hours ago.
Update to Scala 3 and add random tests
.
No random tests in Scala.
Fixed in this, also updates to Scala 3
Fixed
Rust translation
Approved!
D translation
Approved!
This comment has been hidden.
Please use a spoiler so that users who didn't solved the kata cannot see your message (I put the flag on for you this time).
This comment has been hidden.
This comment has been hidden.
"What If I Told You There is No Chocolate?: expected -1 to equal 0" made me laugh :D
This comment has been hidden.
You did it wrong on paper.
I dont agree. No one would try to break a 1x1 out of a 5x5. I hope nearly everyone would either first break 5x5 into 1x5 and 4x5 or directly into 5 times 1x5, which would only be 4 breaks. Therefore the rest would be broken in to each 1x1 with each 4 further breaks. Therefore resulting in 4 times 4 breaks, so 16 breaks total.
It's 4 breaks to get 5, not 4 pieces of 1x5 blocks, and then 4 breaks for each of those, that's 24. Or you could break it into 1x5 and 5 4x5 pieces, and you'll still have 24 breaks: 1 + 4 + 4 + 5 * 3 = 24
This comment has been hidden.
please include spoilers in code
Tests in Ruby generate floats for input, which makes no sense.
fixed
COBOL translation.
approved
This comment has been hidden.
I've just raised an issue about that.
Tests are broken in Python "ModuleNotFoundError: No module named 'codewars_test'". My code passes all basic tests.
Switch to Python 3.8.
Thanks!
No random tests in (at least) Java, CoffeeScript, Python and Ruby.
added for python, ruby, CS (still missing for Java, Scala)
@ejini战神 next time you decide to update the kata to properly support Python 3.8, make sure that everything works correctly. All the katas which had issues raised about
No module named 'codewars_test'
were messed up by your edits.@FArekkusu it's a CW glitch as it only sets 3.8 as default after republishing twice (I've alr fixed most of my edits previously)
Added Java translation with randoms, dunno about Scala, tho
Approved Java translation.
Closing this issue, opening a new one about Scala.
This comment has been hidden.
The tests in javascript (yes, you have to mention the language you try, so it can be checked) are ok and they expect 0 if either
m
orn
is 0, your code is wrong or you're reading the logs wrong. Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#print-inputwhen i tested my code i had got following error. Why? solution.c:1:1: error: unknown type name 'y_' y_ ^ In file included from solution.c:2: In file included from /usr/lib/llvm-8/lib/clang/8.0.1/include/stdint.h:61: In file included from /usr/include/stdint.h:27: /usr/include/x86_64-linux-gnu/bits/types.h:30:1: error: expected identifier or '(' typedef unsigned char __u_char; ^ 2 errors generated.
This comment has been hidden.
I get the error message 0 should equal 5
What does that mean?
That your code returned 0 instead of 5. Read this
OK, thanks. But is seems a bit strange. Almost every test case works.
I can't get the principle of this idea, and just to clarify the concept of "break", imagine a life scene, when you break a 2×2 chocolate, firstly break it down to two parts, then separately break each 1×2 part to 1×1, so total break is 3. The answer for 5×5 is always the same —— 24.
Even trying to split down the midlle (or near it) gives the same amount of breaks. A break is given by a pair of / \, so you can count either all / or all \
recursive solution for 7 kyu???
I receive "The expression (actual) == (expected) is false." on the last random_test. I cannot seem to find what the test is or what it tested. Any advice?
The C random tests were not implemented good. Try now.
Julia translation
This comment has been hidden.
completed in one minute :)
This comment has been hidden.
Based on the test cases, each piece has to be broken individually. Once a piece is broken it cannot be combined with other pieces in attempt to break a line across several pieces.
I did it, but I haven't Submit button. What shall I do?
Not an issue. See this.
Hi, I made a NASM and C translation for this kata. Please review and approve.
This comment has been hidden.
yes, the problem was elaborated wrong
R translation. Please, review and approve.
I enjoyed this kata! You do need to think about the problem in a practical way - how would you actually break chocolate? Answers that involve cutting neatly along the lines have missed the point. This seemed clear enough to me.
I didn't get why it's 79%. I actually like this kata.
Problems with the tests. No random tests and / or tests inconsistent with description. Some languages may have these problems, some may not, obviously.
When I do the sample tests, it works but in the attemp, when I run it, it gives an error saying "what if i told you there was no choolate? expected <-1>, but got <0>" and the instructions said to return 0 and there should have been no negative return value needed. Please fix.
Please read other posts before posting your own, java has actual and expected flipped, so it's your code what's returning -1. Currently it can't be fixed, so the next best thing I could do was adding a note.
Properly fixed now.
the minimum number of breaks needed for 5x5 is not 24 , it can be less.
This comment has been hidden.
You can break a 5x5 bar into 25 individual pieces with 15 breaks. Besides, chocolate breaks into bars along lines, not in squares. This question is dumb.
X 6 1 11 X 7 X 2 X 12 X 8 3 13 X 9 X 4 X 14 X 10 5 15 X
The java solution's assertions has its "expected" swapped with "actual" incorrectly.
Fixed.
This comment has been hidden.
Closing because there are nothing concrete besides a grumpy rant.
Having a 5x5 square would give you 8 breaks to get 25 1x1 squares?
This comment has been hidden.
This comment has been hidden.
The assertion parameters are in the wrong order. Please fix.
This comment has been hidden.
How many breaks would you need if there was no chocolate?
This comment has been hidden.
Say you have a chocolate bar that is
m
squares byn
squares. You want to make straight breaks in the chocolate until you have nothing but singular squares remaining.Also, this is considered one break, despite 8 squares being separated into two pieces of 4 squares apiece:
For 5X5 answer should be 17 only. But it is giving 24.
This is intended and 24 is the correct answer there.
The Haskell kata seems to be broken. I got a test error for input (-1, 1) - the test instructions clearly specify that we can assume non-negative integers as input. Also, I submitted the identical solution in Ruby, and it passed - presumably because Haskell uses Quickcheck to generate random tests, and the properties for these tests are broken for negative input. Please fix this - it's quite frustrating if you get test errors and cannot figure out why.
This comment has been hidden.
The description of this kata is confusing. From the kata, I would expect a 3x4 bar of chocolate to require 5 breaks, and not 11.
I agree, although these would probably be called "slices" rather than "breaks" since it's pretty hard to break multiple pieces at once. I would be interested to see a separate kata interpreting the problem in this manner.
No, this is the correct way to phrase it. You can only apply a break to one continuous piece at a time.
This comment has been hidden.
And why are you returning -1 exactly? The description clearly states
If input data is invalid you should return 0 (as in no breaks are needed if we do not have any chocolate to split)
.The expected and actual for the testcases are swapped around - so your actual is: "-1", but expected is: "0".
Also, you should make a case for when the parameters are negative.
I wanted to see that if I return -1 in case of 0, will that test case work or not. Also, description says Input will always be non-negative.
This comment has been hidden.
Yes! Came here to say this. The math is wrong on this!
This comment has been hidden.
I have seen the error of my ways. The solution seemed way too simple haha.
This comment has been hidden.
I'm sorry - I know that's cruel :(
My Haskell solution passes all tests but I cannot submit because of the following warning reported to STDERR:
/tmp/haskell1161115-18-1vpla1d.6d52xx0f6r/BreakingChocolate/Tests.hs:28:66: Warning: Tab character
The tests also expect non-zero value for negative input, which is incorrect.
so do i
I a getting this error aswell
Still getting this problem.
Fixed.
I dont understand , is this ment to be puzzle or a programming exercise. You have to explain how do you split nxm grid. There are many ways to do it. Obvious way is do it layer by layer and then multiply the results. If you do that then a 5x5 grid would need 8 cuts for first layer and 8 for second , 8 for 3rd and 8 for 4th and 4 for last. That means 36 cuts.
The math is wrong for this problem. But, so is yours. 5x5 becomes 5 1x5 pieces with 4 cuts. Then 4 cuts for each of the 1x5's to create 25 1x1's. 20 cuts total.
4 + 4 * 5
does not equal20
.It's a bit of both.
Also, your math is wrong. It would be 9, then 7, then 5, then 3.
TypeScript-Translation kumited!
https://www.codewars.com/kumite/582615c822be6e07fd000003
Please check and approve it! ;-)
such a bad question, solved it in about 2 minutes. COMPUTER SCIENCE IS MY LIFE AND I LOVE IT TOO BITS
Only 2 bits?
yeah boi
rockstar
Test case are confusing us, please change order of expected value and actual value
Please specify language.
Also, Haskell doesn't have Example Tests ..
I think it's java. Added a note.
How can we have a chocolate bar with dimensions (1, -1)? In the description for Haskell it says if there are no chocolates then the function ought to return 0, but my function that does return 0 is not accepted. Please tell us what results are expected exactly.
Why the test case says that it needs 24 cuts to make 1 by 1 squares of 5 by 5, it should be 8 instead ????
take a piece of paper and draw a 5x5 square. You can split the block into 5 rows of 5 with 4 cuts. But that then leaves you with 5 individual rows of 5 squares. Yes, theoretically if you hold all the rows together you can then cut it out in 8 cuts, but if you cant hold the 5 rows together, you then have to make 4 more cuts on each row. So a 5x5 with 4 cuts gives you 5 seperate rows which then have to be broken down individually. [ ] 5x5 => 4 cuts => | | | | | (each pipe is a row of 5 squares) => 4 cuts per row (4*5) => 24 cuts
Haskell kata has wrong tests. Expects different ouput than 0 for negative input (i.e. (-1, 1))
I don't understand how a 5x5 bar = 24 breaks.
You can only make one break in one continuous chunk at a time. You can't, for example, stack two chunks and break them at once.
A 5 * 5 bar looks like this:
You first break each row apart, which is 4:
Then you break each column, which is 5 * 4 = 20.
Add them up and you get 4 + 20 = 24.
JUnit tests are backwards for java.
assertEquals(Chocolate.breakChocolate(5, 5) , 24);
but it's method sigiture is:assertEquals(int expected, int actual)
This produces confusion, such as "expected<-1> but was: < 0 >"
Fixed.
Hi guys, i'm not getting it. To break down the 5x5 bar i need 24 breaks but the test case wants me to have 28 instead. running the test successes but i want to submit my solution it fails because of the issue above.
This comment has been hidden.
You cannot break multiple pieces simultaneously
Please define breakage clearly. I assumed originally that I was to 'snap' the block into two - after all that's what you would do in real life. The first test case confused me, since you can break a 5x5 into 1x1 pieces using only 20 breaks: break down one axis 4 times to get a 5 5x1's, 5*4 = 20.
Start: pieces = 1 5x5, break count = 0, total break count = 0 Break down one axis 4 times: pieces = 5 5x1's, break count = 4, total break count = 4 Break down each of the five pieces 4 times: pieces = 25 1x1's (finished), break count = 20, total break count = previous break count(4) + current break count(20) = 24
If you were to repeatedly snap chunks into two smaller chunks, you'd get 24. Only make one break in one continuous chunk at a time.
Also, in your math, you break each row 4 times and there are 5 rows, so that's 20 breaks, but you also broke the big block into rows, so add those 4 breaks to get 24 breaks total.
How to fix this "What If I Told You There is No Chocolate? expected:<-1> but was:<0>" Please help
You have to account for whether there is chocolate or not. If there isn't any chocolate, it should return 0.
This comment has been hidden.
User solved
This comment has been hidden.
Having this example in the instructions would help a lot in my opinion
This comment has been hidden.
This comment has been hidden.
There is not glory on spending several hours trying to solve a problem that you can google in 3 minutes. ;)
There is not honor in googling an answer.
What we're doing is we're looking at the mathematical patterns to the answers and finding a pure (mathematical) function that takes in
m
andn
and spits out the right answer. Because most langs handle math really really really well, that means we can just make our programming function do our math function. Doing this saves a lot of hassle.The haskell tests are broken for this problem.
breakChocolate 1 (-1)
should be invalid input (as stated in the problem description), but the test expects it to return-2
(!)The description needs to be updated.
I originally made a solution to do the minimum amount of splits rows and columns as this is what the description looked like it wanted. I passed but I am not sure what I calculated :S
My first solution did this
x|x|x|x
x|x|x|x
x|x|x|x This would be for 4x3 and would equal 5
You can only make one break in one continous chunk at a time.
This comment has been hidden.
Agreed. On all test cases for Java "expected" and "but was" should be reversed.
fixed some time ago
This kata is not correct for Haskell. Description says no negative values, but tests do use negatives. It doesn't even allow for expected behavior (treat negative amount of chocolate as invalid). Please fix this.
you can validate this, when n or m is <=0 return 0
This kata has not test code now (in haskell).
This comment has been hidden.
The reference implementation returns -2 for those inputs. This contradicts the kata description, which claims
and
My program passed all tests, but on Submit I get "What If I Told You There is No Chocolate? expected:<-1> but was:<0>". What does it mean "No Chocolate"? I suppose n=0 and m=0. In such a case "(0,0)" my program passes all tests and I get "-1", but on Submit I still get that error... I'm confused.
did you figure it out? I'm still stuck on this
From what I can tell, if n < 1 or m < 1.. you sould return 0. This includes negative falues for either (or both).
Test cases give input of negative values even when the description states that it won't. Also the expectations are reversed.
Help us help you. Tell us which language you're having this problem with.
Closing.
You explicitly say that input will consist of only positive integers, and then give a case where it's not.
Haskell tests fail with this message. Not clear what it means.
Random Values Falsifiable (after 3 tests and 1 shrink): (-1,1)
The breakage are not explicit enough. You must say that one can detach only one single piece at each break.
What is supposed to be returned on (1,-1) or (-1,1)? I thought it might be zero, according to kata's description. Why am I wrong?
Unfortunately the test cases do not match the description. There shouldn't be any negative input values at all. That said, it seems, that with the current (haskell) tests it is expected to treat negative values like any other value. Zero is only expected if one of the inputs is zero.
I think this kata needs better tests and a more precise description.
This comment has been hidden.
Fixed.
This kata would have been interesting if the results squares could be — as I initially assumed — of any size (3x3, 2x2, etc.). What you're asking is both unclear and uninteresting.
There are no test cases in the Haskell version, and the tests fired upon submission do not match the description: random tests not only include negative input values, but are also expecting nonzero answer in such cases.
This comment has been hidden.
I had the same problem, was wondering if i got it wrong, entered a similar solution in Python and then it passed. Something seems to be wrong with the Haskell tests.
This comment has been hidden.
Sorry but I did not enjoy this kata, especially the process of trying to guess what it expects of me.
You need to precisely define the operation of "breaking" a chocolate bar, which from what I deduced from your test cases sounds more like "slicing" (trust me, you can't break a 12 x 12 chocolate using your bear hands in 12 precise 1x12 bars.)
Also, you need to specify that no form or stacking is allowed, because it's very easy on the other hand to break two 2x1 bars into 4 small 1x1 squares using your bear hands, without damaging the squares. If I were to break a 4x1 bar into small pieces in the least number of moves I'd always break it in half then stack the 2x1 halves and break them in two again - two cuts, not three.
Other than that, the result is a very simple mathematical formula, so I personally see no fun or challenge in this kata.
I agree with this. If this hadn't been ranked at 7 kyu, I would have spent a long time working out how to optimally break chocolate in as few "operations" as possible. Clearly that turned out not to be necessary, but I shouldn't have to guess that based on the rating.
I agree, I'm still unsure of what's being asked. Maybe include an description of how you expect a chocolate bar would be broken assuming both dimensions are >1? I think that would help.
Add test cases.
I think you should specify whether or not we can assume that the given n and m are always non-negative integers. If not (for example if we're meant to deal with cases like n=4.5 or m=-7) then you should add test cases that check that 0 is returned.
This comment has been hidden.
Also, I believe your expected values are wrong. Cuttig a bar of chocolate can be done more efficiently by cutting the bar in half and then stacking the two parts. A second cut will then cut through both of those parts, giving four pieces with just two cuts instead of three.
Example: 5x5 chocolate bar. First cut it into 3x5 & 2x5. Stack those and make a cut so that now you have 1x5 & 1x5 & 1x5 & 2x5. Cut the 2x5 so now you have five 1x5. We have made three cuts but have five pieces. Then stack them all together and cut them each into 1x3 and 1x2 pieces. Stack all of those, cut, stack again and cut. 25 1x1 pieces with 6 cuts. Not the 24 cuts you expect.
Fixed.
Your second comment just describes real world cheating - I do not know anyone who does that :P Besides there is possibility that when you are stacking bars in layers like that, you won't be able to break them without puting extra 'inhuman' strenght or breaking/damaging the
1x1
pice into smaller parts.You need to explicitely state that stacking is not permitted. Also, you need to use better knives.
This kata really made me unreasonably angry. I finally figured out your thinking. The analogy of cutting a chocolate bar is rediculous. If you want to keep that analogy, I would change it to say something like "breaking off each peice one by one".
There is no creativity in finding this answer. There are other katas that provide the basics in education about simple math operations, so I don't see any need for this kata as it is.
Failed test cases should give hints about what inputs were provided. Please don't leave them out "on purpose". It makes it frustrating, not challenging in a positive sense, for kata authors to consistently not provide feedback on failed tests.
Ok i give up - changed some test cases to use assertEquals instead, so it should be a hint of some kind.
You should note that "small squares" are 1x1. It's unknown which square is small.
Thank you for giving it a try :) I have updated description.
I have no idea what I need to do for this kata. We're given the size of teh chocolate bar, and that's it?
That's exactly it - function gets dimension of chocolate bar
n
xm
and must return number of breaks needed to split it to blocks of1
x1
sizeOkay, I get it now.
The line
into small squares. Each square is of size 1x1 and unbreakable
is just confusing. Why not simply say "into 1x1 squares"?You should mention that
m
andn
will be non-negative integers. You should also give the expected output for the degenerate cases ofm = 0
orn = 0
: Should a degenerate chocolate bar require 0 breaks or nil?Yeah, you can't have a bar with zero pieces—it just doesn't make sense.
Thank you for input. I have updated description - you should just return 0 in that case :).