Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
It should be
6 has 2 on bits (000...0110)
.Use spaces and words for numbers only. Not expecting any "and", or "-" or anything else you might think of.
Your example with/without "and" will have same sort order anyway though (if consistently applied)
This comment is hidden because it contains spoiler information about the solution
The random tests for php were wrong. Fixed.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
unset
an element, but the index is keep the original value. strange and funny.^_^
It's supposed to be a beginner's trap
@myjinxin2015 Exactly ^_^ So, you do know a bit of PHP (or maybe you're just very quick at learning a new programming language) ;) Funny enough, this exact behaviour of PHP arrays baffled me for at least an entire year since I first started learning PHP :p Ironically, the key to understanding arrays in PHP is to unlearn everything you have known about arrays/lists in every other programming language that you may know and realising that arrays in PHP are more like objects in JavaScript or dictionaries in Python than arrays/lists in those languages.
@donaldsebleung,
I'm not going to add descriptions for this situation. Because I think it is obvious that the two items can not be taken away, the result is obviously the 0. This should be a normal human thinking
^_^
.I'll add a basic testcase in the code and add an example in the description.
EDIT: I found this testcase already exist:
$this->assertEquals(0, knapsack_light(10, 2, 11, 3, 1));
So I only added an example in the description.
@Robin-Andrews,
I have a try by using your code ;-)
I add a
print_r
in your code:and I see this:
As you can see, then $list[0] is
unset
ed. there is only $list[1] left ;-)So the statement
return $list[0]['value']
will got an error.I don't know PHP, am i right?
Ah, I just took a detailed look at your code and I think I found a potential problem with the solution you posted here - note that array indices are preserved in PHP even if all keys are numeric so you cannot assume that
$array[0]
must exist ifcount($array) === 1
even if you initially defined your array as non-associative. Hope this helps :DYou can check the (randomized) input passed into your function by using
echo
. Anyway, heading to the PHP version of this Kata now just in case this is an Issue on my end ;)@Robin-Andrews,
If your knapsack is too small to carry either of the two items, then naturally the total value of items in your knapsack is
0
. Hope this answers your question :)Cheers,
donaldsebleung
P.S. @myjinxin2015 Upon reading your Kata Description again, it indeed does appear that it doesn't state what value should be returned if none of the two items can be carried in the knapsack. Perhaps you could edit the Description to address this edge case? If you do not intend to test users on this edge case then please do let me know and I will fix my translation. Cheers :)
Please waiting for the translator to check the code. I don't know PHP, sorry ;-)
@donaldsebleung,
please check it
^_^
Loading more items...