Talk:Longest string challenge: Difference between revisions

Line 71:
::: I'm not sure about ''explicitly'' storing, but the spirit of the task *is* to avoid ''explicit'' math and comparisons. In particular, avoiding explicit less/greater comparisons. --[[User:Dgamey|Dgamey]] 05:19, 14 August 2011 (UTC)
:: Icon has no boolean type. Expressions succeed (returning a value) or fails and do not. It's a form of short circuit evaluation. For example, it is not possible in Icon to run out of bounds of a list, string, etc. as doing so fails. 'move(1)' returns the next character in the string being scanned or fails if there is none. --[[User:Dgamey|Dgamey]] 05:19, 14 August 2011 (UTC)
 
==Request for Python explanation==
Hi Ledrug,<br>
Could you give me a brief explanation as to why your later Python example of:
<lang python>import fileinput
 
# return len(a) - len(b) if positive, 0 otherwise
def longer(a, b):
while len(a) and len(b):
a, b = a[1:], b[1:]
return len(a)
 
longest, lines = '', ''
for x in fileinput.input():
if longer(x, longest):
lines, longest = x, x
elif not longer(longest, x):
lines += x
 
print(lines, end='')</lang>
 
Replaces:
<lang python>import fileinput
import operator as op
maxlen, maxlines = 0, ''
for line in fileinput.input():
ll = len(line)
if op.gt(ll, maxlen):
maxlen, maxlines = ll, line
elif op.eq(ll, maxlen):
maxlines = op.concat(maxlines, line)
print(maxlines, end='')</lang>
It would help me understand the task a little better. Thanks. --[[User:Paddy3118|Paddy3118]] 06:39, 14 August 2011 (UTC)
Anonymous user