Ad
  • Custom User Avatar

    Please correct me if I am wrong, but I don't think it is possible for me to add a Python solution to a pre-existing kata. If there were a way to merge this kata with the one referenced above I probably wouldn't be opposed.

    As I write this there are only 13 kata availible in Python. Just trying to expand the Python offerings.

  • Custom User Avatar

    I think python is far too dynamic for any kind of static analysis to catch this kind of trickery.

    I expect that it is possible to outwit the code above, but if it is wrapped up in a nice API for us to use it should be relatively easy to plug any holes as they are found.

  • Custom User Avatar

    Clever hack :)

    At the risk of playing whack-a-mole I have blocked that workaround:

    import __builtin__
    orig_import = __builtin__.__import__
    orig_reload = __builtin__.reload
    
    def disabled(*args):
      print "itertools.permutations has been disabled for this Kata"
    
    def filter_unwanted_imports(*args, **kwargs):
      if 'permutations' in args[0]:
        return disabled
      if 'imp' == args[0]:  # No legit reason to load this module in this Kata
        return None
      results = orig_import(*args, **kwargs)
      if hasattr(results, 'permutations'):
        results.permutations = disabled
      return results
    
    def filter_unwanted_reloads(*args, **kwargs):
      results = orig_reload(*args, **kwargs)
      if hasattr(results, 'permutations'):
        results.permutations = disabled
      if hasattr(results, 'reload'):
        results.reload = filter_unwanted_reloads
      if hasattr(results, '__import__'):
        results.__import__ = filter_unwanted_imports
      return results
    
    __builtin__.__import__ = filter_unwanted_imports
    __builtin__.reload = filter_unwanted_reloads
    

    It would be nice if there was an easy/standard way for kata authors to block access to certain library functions...

  • Custom User Avatar

    The referenced kata is a Javascript only, and thus not a duplicate of this Python only kata.

    Additionaly this kata is meant to excercise some specific Python features (generators) and pitfalls (lists are passed by reference - be aware of side-efects when modifying them).

  • Custom User Avatar

    @cmgerber:
    Your itertools solution fails because the function returns an iterator rather than a generator. All generators are iterators, but not all iterators are generators.

    I have updated the description of the kata to emphasize this.

    I have also disabled itertools.permutations as it makes the kata too easy :)

    @xcthulhu:
    the permutations function only needs to take one argument - the 2nd argument being passed was a bug which has since been corrected.

  • Custom User Avatar

    There was a bug in the validation routine.

    I have published a new version. Give it a try.

  • Custom User Avatar

    Much better.

  • Custom User Avatar

    This solution certainly solves the kata.

    I only commented because this solution has a number of "best practices" votes.

    Per the kata description:
    "...a function that takes in a list and returns a list with the revese order."

    In a function with that description modifying the passed in list is not "best practices"

    If I could cast a down vote for "best practices" I would have done that instead of leaving a comment.

  • Custom User Avatar

    The description of the python version is not very good.

    As rmunn mentions the API is not specified.

    It should be noted in the description that add and subtract should return new vectors (as aposed to modifying the object in place).

    "norms" needs to be defined.

    I propose that the description of the python version of this kata be changed to the following:

    Description:

    Create a Vector class that supports addition, subtraction, dot products, and norms.

    The Vector class should support the following methods:

    add:       returns a new Vector that is the result of adding the passed in vector to this vector.
    subtract:  returns a new Vector that is the result of subtracting the passed in vector from this vector.
    dot:       returns the dot product of this vector and the passed in vector. (a1*b1 + a2*b2 ...)
    norm:      returns the magnitude of the vector (sqrt(v1*v1 + v2*v2 ...))
    __str__:   returns "(1,2,3)" for the Vector([1,2,3])
    equals:    returns True if the components of this vector are equal to the components of the passed in vector.
    

    If you try to add, subtract, or dot two vectors with different lengths, you must throw an error!

    The test cases will utilize the user-provided equals method.

  • Custom User Avatar

    This one has the side effect of modifying the passed in list.

    I would be much better to copy the list before reversing it.

  • Custom User Avatar

    Need to explain that the report object is a mutable object that updates as new calls are made.