• Needs random tests
    • Needs to disable reload for obvious reasons ;-)
    • Duplicate!
  • I think itertoools.product could be disabled as well, in order to make it a bit more challenging (and avoiding the recipes that Python's doc gives)

  • You can't return "ERROR". "ERROR" is potentially valid output - <78d#Z;?> decodes to ERROR.

    Use throw new Error() instead.

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

  • I had similar problems with the example test cases provided, with an error saying "Unhashable type: list". I agree set(lists) is not the right example test case to provide. Good kata for recursive generator practice, though!

  • I would strongly suggest adding a test case like this:

    results = [tuple(p) for p in permutations([1, 2, 2])]

    expected_results = [(1, 2, 2), (2, 1, 2), (2, 2, 1)])

    Test.assert_equals(sorted(expected_results), sorted(results))

    And please review your reference implementation for passing this test case. It doesn't pass, neither do a lot of successful code submissions.

    A good permutation generator shouldn't rely on some external code which removes duplicate occurences. In your case it's set, it's highly recommended to use sorted(...) instead of set(...), thus you will see duplicates instead of hiding them

  • It is possible to add another language to a kata in beta (you need 1500 rep... I personally think this is too big, specially when adding a new language). This is not possible for an already implemented kata. It seems that it is a technical problem that the devs are working on.

    For the moment I don't think it is a good idea to publish multiple katas for different languages as it will probably be harder to merge kata than just edit the old ones.

    It is not my role to give a final answer to this question so I removed the issue. I still think you will have to work on a merge with the already existing kata when this will be possible.

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

  • There are differences between languages, like there may be multiple solutions for a given language. But I think that is the point of kata having multiple solutions, not having a kata for each solution.

    I am not convinced this method is fundamentally different.

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

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

  • 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).

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

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

  • @cmgerber: I think electricjay's permutations takes a second argument, which behaves differently than the builtin's second argument.

    Personally, I'd rather electricjay's permutations function have the same API as the builtin, but then this exercise would be trivial...

  • Loading more items...