3

While trying to understand how to use the lambda, I came across one reply in which the poster said that nothing you can do using lambda you can't do using normal functions.

I have been trying so hard to call a function from within itself in Python, not expert though, yet I'm learning, and I came across few problems where you need to use recursive functions, call multiple times to get a certain answer.

A guy has used the lambda function to do that, I tried to understand it but I failed, so I though if the functions can be implemented using normal functions, it would be easier to start understanding the lambda from that point on.

Let's take this sentence for example:

print"\n".join(" ".join([(lambda f:(lambda x:f(lambda*r:x(x)(*r)))(lambda x:f(lambda*r:x(x)(*r))))(lambda f:lambda q,n:len(q)<=n and q or f(q[len(q)/2:],n)+f(q[:len(q)/2],n))(k,z+1)for z,k in enumerate(i[:-1].split())]) for i in list(s)[1:])

This has been used in the Facebook hacker cup, I couldn't solve this problem as I was lost in the loops.

This sentence takes a few words, let's say "Stackoverflow rocks and it is great"

The problem statement in Facebook is :

You've intercepted a series of transmissions encrypted using an interesting and stupid method, which you have managed to decipher. The messages contain only spaces and lowercase English characters, and are encrypted as follows: for all words in a sentence, the ith word (1-based) word is replaced with the word generated by applying the following recursive operation f(word, i):

If the length of word is less than or equal to i, return word. Otherwise, return f(right half of word, i) + f(left half of word, i).

If word is of odd length, it is split such that the right side is longer. You've decided to have a little fun with whoever is sending the messages, and to broadcast your own messages encrypted in the same style that they are using.

Input Your input will begin with an integer N, followed by a newline and then N test cases. Each case consists of an unencrypted sentence containing only spaces and lowercase letters, and cases are newline-separated. There will be no leading or trailing spaces in a sentence and there will be at most 1 space character between any otherwise-adjacent characters

Output Output, for each case and separated by newlines, the contents of the encrypted sentence after applying the encoding method describe above to it. You may ignore traditional capitalization rules and stick to all lowercase letters.

Constraints 5 ≤ N ≤ 25 Sentences will contain no more than 100 characters.

4
  • 3
    Interesting problem (the one from facebook). The problem (the one you have understanding that solution) is that n-times nested lambdas, overly long lines, short names and many other things make it a unreadable mess. Commented Jan 23, 2011 at 23:29
  • The example you quote is not from the real world, and you don't ask a question ... Commented Jan 23, 2011 at 23:55
  • 4
    This has less to do with lambda and more to do with unrolling code obfuscation. Commented Jan 24, 2011 at 0:00
  • Please forgive me, I have copied the WRONG problem for the right code. I have edited the original post and included the RIGHT facebook problem. Sorry about that, honestly Commented Jan 24, 2011 at 1:21

1 Answer 1

3

Python lambdas are simply syntactic sugar. "Regular" functions have the same capabilities such as closures, because, remember, you can define them inside another function, just as lambda does.

def some_func():
  some_expr_using(lambda args: 42)

# becomes:

def some_func():
  def unique_name(args):
    return 42
  some_expr_using(unique_name)

Except that when inspecting the lambda object, its name is set to "<lambda>", rather than unique_name as above, and other superficial details relating to how the actual source code is spelled rather than as it behaves.

Your code could be written as:

def y(f):
  def a(x):
    def b(*r):
      return x(x)(*r)
    return f(b)
  return a(a)

def fx(f):
  def x(q, n):
    # changed "a and b or c": different semantics if b can be falsy
    if len(q) <= n:
      return q
    else:
      return f(q[len(q) / 2:], n) + f(q[:len(q) / 2], n)
  return x

print "\n".join(
  " ".join(y(fx)(k, z + 1) for z, k in enumerate(i[:-1].split()))
  for i in list(s)[1:])

(But only if I've translated it correctly; double-check. :P)

This code is an example of a fixed-point combinator, which I only barely understand, and it's hard to give better names without knowing more context (I didn't try to decipher the actual problem statement). It can be unraveled to a recursive function which calls itself by name directly.

Sign up to request clarification or add additional context in comments.

3 Comments

Please forgive me, I have copied the WRONG problem for the right code. I have edited the original post and included the RIGHT facebook problem. Sorry for that, honestly
@ma2moun: My answer is based on the line of code rather than the problem statement, and, since it appear that you didn't change that line of code, this should still apply.
I have noticed that and marked your answer as the answer, but I commented with the apology to all answers just in case. brilliant, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.