Ad
  • Custom User Avatar

    Hi @Rockkley.

    Lambdas are anonymous callable objects. If you want call it later, you must use a variable, no other choice to do that. And here (in codewars) if there no name, unit test cannot check the solution.

    For your information, in python, a def (and so, a fonction) defined like that create a variable:

    def foobar(): pass
    

    The variable created is foobar, the proof, you can do that:

    def foobar(): pass
    foobar = 1234
    

    It's the aim of the mechanism under the concept of decorators.

    If you prefer i can use these solutions instead of lambda, the solution will be acepted:

    # Common accepted solution
    def check(s, e): return e in s
    
    # Another solution
    def singleton(c): return c()
    @singleton
    class check:
      def __call__(self, s, e):
        return e in s
    
  • Custom User Avatar

    I know that lambda is anonymous function, and there's no need to create variable for lambda, but here we need to meet the interface requirements. Requirement here is that check is callable.

    In real world I would not create function for this one liner, that's why I'm showing solution with lambda.

  • Default User Avatar

    variable "check" holds the function. and ambda describes the oneliner function.

    mult = lambda a,b:a*b

    is the same as

    def mult(a,b):
    return a*b

  • Custom User Avatar

    What do you mean? Do you know how lambdas work? Do you know you can copy the code and make trials on your own to understand how it works? On your own computer, or just here, by clicking on Forkjust below any solution.

  • Custom User Avatar

    Why creating a variable for lambda?

  • Custom User Avatar

    You need more corner-case tests here

  • Default User Avatar

    This comment got deleted.

  • Custom User Avatar

    I'm not happy with this kata. Using builtin function is not cheating when in description is no information about not using this.
    Also this "anti-cheat" was not giving me option to name my variable max :/

  • Custom User Avatar

    Hi, message ValueError: sample larger than population simply says that population (from what we want build sample) is smaller than number of elements we want to select.
    In this case it suggests that [x for x in c.dict.keys() if not str(x).startswith('__')] is less than 1. And that means that you miss something in class created with your solution.

  • Custom User Avatar

    Hi, after attempts I have this error. Should it be visible or I just made something wrong?

    Traceback (most recent call last):
    File "main.py", line 60, in
    rand = sample([x for x in c.dict.keys() if not str(x).startswith('__')], 1)[0]
    File "/usr/local/lib/python2.7/random.py", line 325, in sample
    raise ValueError("sample larger than population")
    ValueError: sample larger than population