Ad
  • Custom User Avatar

    It should be 6 has 2 on bits (000...0110).

  • Custom User Avatar

    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)

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    The random tests for php were wrong. Fixed.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    unset an element, but the index is keep the original value. strange and funny.^_^
    It's supposed to be a beginner's trap

  • Custom User Avatar

    @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.

  • Custom User Avatar

    @donaldsebleung,

    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?

    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.

  • Custom User Avatar

    @Robin-Andrews,
    I have a try by using your code ;-)
    I add a print_r in your code:

    if (count($list) == 1){
        print_r ($list); echo '<br />';
    

    and I see this:

    Array
    (
        [1] => Array
            (
                [weight] => 2
                [value] => 21
            )
    )
    

    As you can see, then $list[0] is unseted. 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?

  • Custom User Avatar

    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 if count($array) === 1 even if you initially defined your array as non-associative. Hope this helps :D

  • Custom User Avatar

    You 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 ;)

  • Custom User Avatar

    @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 :)

  • Custom User Avatar

    Please waiting for the translator to check the code. I don't know PHP, sorry ;-)

  • Custom User Avatar

    @donaldsebleung,
    please check it ^_^

  • Loading more items...