Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslating a lambda to a normal function in Python
    primarykey
    data
    text
    <p>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.</p> <p>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. </p> <p>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.</p> <p>Let's take this sentence for example:</p> <pre><code>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)&lt;=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:]) </code></pre> <p>This has been used in the Facebook hacker cup, I couldn't solve this problem as I was lost in the loops.</p> <p>This sentence takes a few words, let's say "Stackoverflow rocks and it is great"</p> <p>The problem statement in Facebook is :</p> <p>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):</p> <p>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).</p> <p>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.</p> <p>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</p> <p>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.</p> <p>Constraints 5 ≤ N ≤ 25 Sentences will contain no more than 100 characters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload