4 kyu
Memoized Log Cutting
779 of 1,497AlejandorLazaro
Loading description...
Dynamic Programming
Memoization
Refactoring
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.
Great kata to exercise dynamic programming concepts with, thank you.
python new test framework is required. updated in this fork
approved
This comment has been hidden.
old 4 kyu -> now 6kyu
there is no "example of the working code" in the description when selecting JavaScript. When selecting C++, both the algorithm and the "working code" are written in Python
Nice and simple intro to memoization, good kata!
I really enjoyed this one. Great kata, thanks.
There are dynamic problems of same difficulty at 6 kyu and some that are way harder at 5 kyu, good problem nonetheless.
Can you please also add a test case where n exceeds, the size of p?
Nice kata.
My coding is C++. Could have added some very stressing tests, IMHO. Wanted to compare algorithms and could not. EG what happens when you divide the inner loop iterations by 2 (see cocoderrr commment), but actually changed nothing in kata time execution...
BTW #idea: could lead to other katas: what happens if your saw device has a limitation in max cut length, (meaning the price list length is less than the log length) ?
Me solving this on a white board during an interview:
"This kata brings a whole new meaning to
O(log n)
complexity!"(the whole interview panel laughs, claps, and gives me the job on the spot)
:-)
Typescript translation https://www.codewars.com/kumite/63088f73f22754000fa5a983?sel=63088f73f22754000fa5a983
C++ translation https://www.codewars.com/kumite/6303b1e0b0817b0a6100a261
approved
Could you include the random test input and expected data in the failed results? My solution passes in all specified tests and most random tests, but annoyingly there are always a few random tests where my calculation is off by 1 or 2. So far I was not able to replicate this issu.
Haskell translation
Having solved this problem, I wanted to write that there is an inaccuracy in the description. But then I realized that this Kata imitates the real world. In the real world, you are distracted by stories about juniors, seniors, bosses, stackoverflow, etc. And no one says where the bug really is.
Code given in the description is syntactically incorrect:
Fixed
Very unclear description. Anybody can explain what should be done with p = [ 0, 1, 5, 8, 9, 10] ?
I'm pretty sure theres a bug in the random tests.
n=1 should return 1 but it's expecting 2 some how.
Dart
Also Testing for n = 10 Expected: <53> Actual: <30>
When you click at the full attempt, those prices change. You have several tests for each different set of prices.
There are no issues with random tests in Dart.
I think there are two wrong Expected: values in the Sample Tests. Below, I've are tables showing all possible prices for the two tests below, and have highlighted the maximum (optimal) price found in those tables:
Log
[0, 41] 0 + 121 = $121
[1, 40] 1 + 119 = $120
[2, 39] 5 + 116 = $121
[3, 38] 8 + 115 = $123
[4, 37] 9 + 112 = $121
[5, 36] 10 + 107 = $117
[6, 35] 17 + 104 = $121
[7, 34] 17 + 101 = $118
[8, 33] 20 + 99 = $119
[9, 32] 24 + 95 = $119
[10, 31] 30 + 91 = $121
[11, 30] 32 + 87 = $119
[12, 29] 35 + 85 = $120
[13, 28] 39 + 84 = $123
[14, 27] 43 + 81 = $124 <--
[15, 26] 43 + 80 = $123
[16, 25] 45 + 74 = $119
[17, 24] 49 + 70 = $119
[18, 23] 50 + 68 = $118
[19, 22] 54 + 65 = $119
[20, 21] 57 + 60 = $117
Expected: <125>
Actual: <124>
Log
[0, 50] 0 + 151 = $151 <--
[1, 49] 1 + 145 = $146
[2, 48] 5 + 143 = $148
[3, 47] 8 + 140 = $148
[4, 46] 9 + 135 = $144
[5, 45] 10 + 134 = $144
[6, 44] 17 + 131 = $148
[7, 43] 17 + 129 = $146
[8, 42] 20 + 125 = $145
[9, 41] 24 + 121 = $145
[10, 40] 30 + 119 = $149
[11, 39] 32 + 116 = $148
[12, 38] 35 + 115 = $150
[13, 37] 39 + 112 = $151 <--
[14, 36] 43 + 107 = $150
[15, 35] 43 + 104 = $147
[16, 34] 45 + 101 = $146
[17, 33] 49 + 99 = $148
[18, 32] 50 + 95 = $145
[19, 31] 54 + 91 = $145
[20, 30] 57 + 87 = $144
[21, 29] 60 + 85 = $145
[22, 28] 65 + 84 = $149
[23, 27] 68 + 81 = $149
[24, 26] 70 + 80 = $150
[25, 25] 74 + 74 = $148
Expected: <153>
Actual: <151>
Cheers, DW
language? If it's python, nope, all the expected values are correct.
dart
nope, all is fine in dart too (I just checked, translating the whole dart sample tests to python).
Thanks Blind4Basics and Steffan153 for your replies!
Since you confirmed the expected targets are correct, then my assumption that the log can be cut at most one time is not correct. I based that assumption on the example given where $5 + $8 = $13...
Ergo, if one were to order a 50 foot log from this outfit, one could, theoretically, receive a mishmash of logs, even 50 x 1-foot pieces. (I didn't see that one coming.)
Perhaps this could lead you to design another kata, namely, determine how many nails or how much glue would be required to build a log cabin from the all of the pieces.
:)
Cheers, DW
Hello donaldww, about issue "The optimal price for n = 41 should be $125" Your are right [14, 27] 43 + 81 = $124
but a log of 27ft also is [13, 14] 39 + 43 = $82
so you have at the end 3 logs, that means: [14,14,13] 43 + 43 + 39 = $125
This comment has been hidden.
Sure hope this isn't a stupid question. I'm passing all the sample tests except for the last one that was commented out. It says it should be 105, but I'm getting 104.
Can someone please explain to me why
cutLog(p, 35)
is supposed to return 105 and not 104? Even going through it manually, I can't find any pair that adds up to 105.Here's
p
:You must cut this log in pieces of length
[3, 6, 26]
(35
) and value[8, 17, 80]
(105
). Hope this helps! ;)From what you said it seems the misunderstanding is that you think you can cut the log only once. Of course you can cut it any time you want, that's what makes the difficulty of this kata (avoiding the
O(n²)
complexity)I'm getting failed for some very odd "correct" answers. What I will say is that the answers from my own code that have passed as correct work out at about 3$/foot for more than about five feet; however Testing for n = 11, apparently my answer should equal 110. Is that a glitch?
It's a random test case, so if you don't give us the associated
p
array I'm affraid no one will be able to help you ^^This comment has been hidden.
It's randomized.
Dart translation kumited!
In Python 3.4.3:
Traceback: in <module> in sol NameError: name 'array' is not defined
Should be fixed.
Approved
Maiden writing random tests, please wait warmly...
Finish!
The JS translation is rather lazy. Code example is still written in Python and the casing of identifiers is not in accordance with JS convention.
(It is possible to update the translation without invalidating existing solutions.)
Done.
I'm writing random tests for both versions atm.
Two solutions got invalidated anyway. Would
cutLog = cutLog || cut_log
work better?ETA: Oh. The other invalid solution is just wrong. I'll just resubmit my own without the
const
you're redefining (through identicalObject
s).ETA: I see you did exactly that in the Example tests, but something different in the Submit tests.
And no, it doesn't work. You'd have to declare and define an unrelated, new variable. That would not be very pretty.
I've just decided to cut off the compatibility fix (while I can) so people won't get thrown off when they're using
const
.The effect should be quite small at this stage.
The problem only appears when doing
const cut_log=cutLog
(or vice versa), in anticipation of a name change that would not preserve existing solutions.I had already resubmitted without being overly smart about this. You could just revert to
if ( ! cutLog ) cutLog = cut_log;
.The new function is a bit misleading. I didn't need it. Also it wasnt immediately clear that the tested function is not th e old one
Hey sepi,
I cleaned up the pre-loaded solution. It shouldn't be confusing anymore
It's not.
For people not very well familiar with economics, management and/or english language, it would be very nice to have a description of the business logic involved in this kata, what is a "log", what is a "price table" and what is the result expected. on for them to easily calculate the most profitable price for a log of length n using price table p as follows:
I have added in an example price table to the Details of the Kata as well as an example of expected output. Let me know if there is anything else that can be clarified. Thanks for your feedback!
Yeah, it's ok now. Thanks!
Nice kata! :)
Just as a heads up, this problem could ALSO include the solution as being the pieces that need to be printed out. Ex: The price for 5 is 13, but the optimal pieces to be cut are [2, 3] since 5 + 8 = 13.
Do you think I should include that extra requirement into the solution, or possibly just append that problem into a later kata? (The solution is a very simple modification of the finished code, although it may not be immediately apparent)
I don't think that's necessary.
I think you should make it a new kata. Modifying my solution so that it would print out the pieces would be non-trivial.