Ad
  • Default User Avatar

    Thanks for the comment, I will revise the kata tomorrow.

  • Custom User Avatar

    It is quite typical in the electrical industry to have various sub-functions to calculate different parameters seperately.

    It should not be matter if the PF is 0.8499 or 0.84999 it is still below 0.85 p.u., therefore, a load should be stored in the list.

    Yes, but currently you have a fully documented function calculate_power_factor that says it returns the power factor rounded by 4 decimal places, and is also not tested. There are two problems with this:

    1. If we're supposed to use this function, then when input contains an item which PF calculates to be something like 0.8499 it'll be rounded up to 0.85 and hence is incorrectly removed from the results. So using this function with the behaviour it has been documented actually fails the kata. It's sort of supposed to be a helper function, except it's actually sabotaging the main function, so we're supposed and not supposed to use it at the same time?

    2. In TDD (Test-Driven Development), if you don't test something, it doesn't exist. So the kata requirement is telling us to implement something but it doesn't check it is actually implemented.

    Speaking of this, there should be a test case with input which PF is between 0.8495 and 0.95 exclusive.

  • Default User Avatar

    Answer to the first part:

    1. Thanks for pointing to the mistake. The edge case should P and S = 0. There should be no cases where P is some number, while S is 0.

    Answer to the second part of the comment:

    1. It is quite typical in the electrical industry to have various sub-functions to calculate different parameters seperately. In this kata, the seperate function is not required all calulcations can be done in one function.
    2. It should not be matter if the PF is 0.8499 or 0.84999 it is still below 0.85 p.u., therefore, a load should be stored in the list.
  • Custom User Avatar

    Remember to handle edge cases like when S is 0.

    Okay, so what should the power factor be when S = 0 then...?


    Returns a list of names of devices with power factors below 0.85

    But the kata tells us to use a sub-function which rounds by 4 decimal places. This means a power factor of 0.84999 will be considered not below 0.85, even though it shouldn't.

    But then, calculate_power_factor is not tested anyway, so why are we rounding in the first place? Is testing P/S < 0.85 so complicated?

  • Default User Avatar

    If I am not wrong, Unnamed was using a mathematical approach and it levarages the periodicity of the sequence in terms of odd and even terms. While most of us is using iterative approach.The provided solution is more efficient for larger values.

    Regarding you question:

    1. First part (n mod 6)
      If you know the parity of two numbers, you also know the parity of their sum. So the sequence is defined as: f(n) = f(n-1)+f(n-2)+f(n-3)+f(n-4)+f(n-5)
      Initial values: f(0)=0, f(1)=1, f(2)=1, f(3)=2, f(4)=4. If you compute the parity, you will be able to see a pattern (i.e.odd, odd, even, even, even, even, odd, odd, even, even, even, even,...). So you will be able to notice the pattern repeats every 6 terms.

    2. The list [0,1,2,2,2,2],
      The above pattern (section 1) gives us the cumulative count of odd numbers.
      For n, there are (n/6) complete segments of 6 terms. Therefore, 2x(n/6) gives the number of odd terms in these complete segments.
      The remainder, n mod 6, tells us where in the 6-term segment we are. The list [0,1,2,2,2,2] gives the cumulative count of odd numbers up to that point in the cycle.

    In summary, the sequence has a repeating pattern of parity due its linear definition and the properties of of parity over addition. Therefore, this repeating pattern allowed him, Unnamed, to use a mathematical approach.