Ad
  • Custom User Avatar

    According to PEP 8, Programming Recommendations:

    Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier:

    # Correct:
    def f(x): return 2*x
    
    # Wrong:
    f = lambda x: 2*x
    

    The first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)

  • Custom User Avatar

    Check lst of changes:

    • [x] Made calculator in OOP version.
    • [x] Change function names to be more descriptive.
    • [x] Changed calculate_power function to use for loop instead of while loop.
    • [x] Changed calculate_square_root, and calculate_cube_root functons to operate without third party modules.
    • [x] Changed get_pi to a CONSTANT ("PI").
    • [x] Added comments.
    • [x] Change testing framework from codewars to unittest.

    Thanks you for another kool kata, and the kind words! ☮

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    I couldn't quite get something that wan't a Lambda working, so I settled for organizing the existing code.

  • Custom User Avatar

    Yeah I'm going to release the episode 2 kumite soon!

  • Custom User Avatar

    Ever since Python 3.10 I've tried to make an effort to use Switch Case Statement instead of if-else statements.
    You make some fun katas, I look forward to some more!

  • Custom User Avatar

    Haha, nice, I was hoping someone would use a switch-like statement instead.

  • Default User Avatar
  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    Not a kata issue, you need to mutate the input as it's stated in the description:

    MUTATE the input array/list, don't return anything

    After this line:

    answer = redistribute_wealth(wealth_unequal)
    

    wealth_unequal value should be [5, 5] and so on wth the other tests.

  • Custom User Avatar

    I have some questions and concerns about the Sample Tests to this Kata. I believe the output of the redistribute_wealth function is not being tested correctly, code below:

    def tests():      
        # ...
        # unequal
        wealth_unequal = [0, 10]  # <---------------------------- "wealth_unequal" is assigned a list: [0, 10]
        answer = redistribute_wealth(wealth_unequal)
        if answer is None:
            test.fail("Don't return anything!")
        test.assert_equals(wealth_unequal, [5, 5])  #<----------- Therefore, assert_equals([0, 10], [5, 5]) ????
    
    

    My conern is weath_unequal is being assigned a value of [0, 10], therefore, the assert_equals is testing is test.assert_equals([0, 10], [5, 5]), correct? If so, I believe this test is set up incorrectly. And again with this testing block:

    # floating point result
    wealth_float = [3, 2, 2] # <-------------------------- "wealth_float" is assigned a list: [3, 2, 2]
    answer = redistribute_wealth(wealth_float)
    if answer is not None:
        test.fail("Don't return anything!")
    # (!): Therefore; test.assert_equals([3, 2, 2], [2.3333333333333335, 2.3333333333333335, 2.3333333333333335])
    test.assert_equals(wealth_float, [2.3333333333333335, 2.3333333333333335, 2.3333333333333335])
    
    

    If wealth_float is being assign a value of [3, 2, 2] then assert_equals is testing: test.assert_equals([3, 2, 2], [2.3333333333333335, 2.3333333333333335, 2.3333333333333335]), correct? If so, I believe this test is set up incorrectly.

    Furthermore, checking if answer is None to fail the test is not good idea in this situation:

    if answer is not None:
        test.fail("Don't return anything!")
    test.assert_equals(wealth_equal, [5, 5, 5, 5, 5])
    

    Because for examaple, if the answer is [5, 5, 5, 5, 5], then the answer is not None, therefore this test will always fail. I believe the author meant to set up the tests like so:

    wealth_unequal = [0, 10]
    answer = redistribute_wealth(wealth_unequal)
    test.assert_equals(answer, [5, 5], "Don't return anything!")
    
    

    If I am incorrect in any I mentioned, I apologize ~ Thanks for any feedback!

  • Custom User Avatar

    Ideally, you would want to minimize memory allocations and temporary strings. The previous version would make a new string and do a string concatenation on each loop iteration. There's definitely extra work there that could be removed.

    This version reserves memory ahead of time so there's never more than one memory allocation as the string grows. It also eliminates temporary string objects. Each call to push_back() is only copying a char. Also note the single quotes on the second push_back(). That indicates it's a single char ' ' instead of char array { ' ', '\0' }.

    I also added a reference for the input parameter. You don't need a deep copy of the input and it's generally inadvisable to pass in containers by value since a deep copy could be expensive.

  • Custom User Avatar
  • Loading more items...