Longest string challenge

From Rosetta Code
Revision as of 23:36, 12 August 2011 by rosettacode>Dgamey (Initial task and Icon solution)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Longest string challenge is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Please refer to talk page while this is in draft.

Finding the Longest Strings Challenge

This problem used to be given as a challenge to students learning [Icon]. It's a fun one to play with - especially if you also try to find solutions in other languages.

Problem statement

    Write a program that reads lines from standard input and, upon end of file, writes the longest line to standard output. 
    If there are ties for the longest line, the program writes out all the lines that tie. 
    If there is no input, the program should produce no output. 

Restrictions:

  • No comparison operators may be used.
  • No arithmetic operations, such as addition and subtraction, may be used.
  • The only datatypes you may are use are integer and string. In particular, you may not use lists.

Given the input:

    a
    bb
    ccc
    ddd
    ee
    f
    ggg

The output should be (possibly rearranged):


    ccc
    ddd
    ggg

Further information can be found at [1]. $endif

Icon and Unicon

<lang Icon>procedure main(arglist)

   local b,l,L 
   
   while b := read() do {
       /l := b 
       b ? ( move(*l), if move(1) then L := (l := b) || "\n" else if move(0) then L := (\L|"") || b || "\n") 
       }
   
   write(\L)

end</lang>