Longest string challenge: Difference between revisions
Content deleted Content added
No longer draft. Removed note on clarification from main page. Spoke more about out-of-the box and creative aspect in task desc. Moved task desc above restrictions. |
|||
Line 490: | Line 490: | ||
(push 'Buf (cons Len (cons Line))) ) ) ) ) |
(push 'Buf (cons Len (cons Line))) ) ) ) ) |
||
(mapc prinl (cdr (maxi car Buf))) )</lang> |
(mapc prinl (cdr (maxi car Buf))) )</lang> |
||
=={{header|Pike}}== |
|||
things of note: |
|||
the comparison of strings is done by converting the string into an array of indices: (<code>"abc"</code> becomes <code>({ 1,2,3 })</code>) |
|||
the - operation is the set operation <math>A/B</math> and not a numerical subtraction. it removes all the elements in the second array from the first. |
|||
if there are any left, we know that the string is longer. |
|||
now, once a longer string is found we call <code>write()</code> to print it. |
|||
however we don't write it out directly, but instead we store the call in queue of pikes backend. the backend is used to handle callbacks for non-blocking I/O, and it provides a facility to call functions after a delay of time. (<code>call_out(write, 5, "foo");</code> calls write after 5 seconds with the argument foo) |
|||
before we add the new call to write, we remove all older calls to write since we don't want them anymore. |
|||
<code>return -1;</code> starts the backend, which allows pike to execute the remaining call_outs and exit. |
|||
<lang Pike>int main(int argc, array argv) |
|||
{ |
|||
string longest = ""; |
|||
foreach(Stdio.stdin.line_iterator();; string line) |
|||
{ |
|||
if( sizeof(indices(line) - indices(longest))) |
|||
{ |
|||
while(!zero_type(remove_call_out(write))); |
|||
longest = line; |
|||
call_out(write, 0, line+"\n"); |
|||
} |
|||
else if( !sizeof(indices(longest) - indices(line))) |
|||
{ |
|||
call_out(write, 0, line+"\n"); |
|||
} |
|||
} |
|||
call_out(exit, 0.01, 0); |
|||
return -1; |
|||
}</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |