Ad
  • Custom User Avatar

    @Lician
    For example finding fib(5):
    Work through it from the top down as per the tree diagram below. When the function returns a value at the bottom, work your way back up replacing each function call with the value it returns and you will reach the answer which is 5.

                                      fib(5-1)                     +                      fib(5-2)
                      fib(4-1)           +          fib(4-2)                 fib(3-1)        +        fib(3-2)
          fib(3-1)       +     fib(3-2)       fib(2-1) + fib(2-2)       fib(2-1) + fib(2-2)               1
    fib(2-1) + fib(2-2)            1              1     +    0              1     +     0               
        1    +    0  
    
    
  • Default User Avatar

    I don't understand how it should work...

  • Default User Avatar

    how did you calculate it?)

  • Default User Avatar

    thx) i'll remember this.

  • Custom User Avatar

    I am sorry to hear about your whole head.

    Your code is broken because it tries to get a token that is not there.

    The fault lies in this code when names >= 4

    while (st.hasMoreTokens()) {
        str1 += st.nextToken() + " ";
        str2 += st.nextToken() + " ";
    }
    

    You can see that if there is an ODD number of names (>= 4) then that last str2 assignment will fail when it runs out of tokens. E.g. if there are 5 names then:

    • loop #1 will read token1 and token2, then
    • loop #2 will read token3 and token4, then
    • loop #3 will read token5 .... and then crash because token6 does not exist.

    e.g. try this:

    assertEquals("Alex, Jacob and 3 others like this", Solution.whoLikesIt("Alex", "Jacob", "Mark", "Max", "Bob"));
    
  • Custom User Avatar