Re: Golf: Length of longest line in a file
by Anonymous Monk on Jul 02, 2010 at 17:49 UTC
|
You could cheat I guess:
$ wc -L file | [reply] [Watch: Dir/Any] [d/l] |
|
++ That's a good way of cheating, especially since I learned a new usage of wc today. That eluded my quick google search earlier today, which only seemed to drum up awk solutions.
| [reply] [Watch: Dir/Any] |
|
wc will count tabs (\t) as 8 characters and newlines (\n) as 0 though,
don't know if it supposed to be like that, but it does:
$ hexdump test
0000000 0a09
0000002
$ wc -cm test
2 2 test
$ wc -L test
8 test
$
| [reply] [Watch: Dir/Any] [d/l] |
Re: Golf: Length of longest line in a file
by BrowserUk (Patriarch) on Jul 02, 2010 at 19:54 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
|
perl -nE "$s^=$_}{say length$s"
perl -lpe "\$Z[y///c]}{$_=@Z"
perl -nE "\$Z[y///c]}{say$#Z" # update: alternative 22
Instead of finding the maximum length by exploiting Perl's wonderful bitwise string operator, we instead exploit Perl's marvellous autovivification (exploiting autovivification is common in golf, as was done by Jasper in The golf course looks great, my swing feels good, I like my chances (Part V), for example).
For golf historians, notice that the use of y///c instead of length is known as Abigail's length horror but was actually first invented by Joseph N Hall in 1996.
And the use of a (one stroke) reference \ to trigger autovivification is known as Thelen's Device and was invented by Michael Thelen aka thelenm in 2002.
This history is discussed in The Lighter Side of Perl Culture (Part IV): Golf.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
perl -nE"$s^=$_}{say length$s" # Switch quotes for bash
perl -nE "\$Z[y///c]}{say$#Z" # Switch quotes for bash
perl -nE"\$Z[y///c]}{say$#Z" # Switch quotes for bash
perl -nE\$Z[y///c]}{say$#Z # cmd specific
1234567890123456789012345
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Golf: Length of longest line in a file
by salva (Canon) on Jul 02, 2010 at 19:20 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: Golf: Length of longest line in a file
by Fletch (Bishop) on Jul 02, 2010 at 20:40 UTC
|
Meh, everyone's beat mine.
#!/usr/bin/runhaskell
import IO
main=do t<-(hGetContents stdin);putStrLn$show$foldl max 0$map length$l
+ines t
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
#!/bin/sh
ghc -e 'getContents>>=print.maximum.map length.lines'
We also use some other functions of the Prelude for brevity.
Note that the code can't be in the same argument as -e.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Golf: Length of longest line in a file
by moritz (Cardinal) on Jul 04, 2010 at 16:06 UTC
|
I kinda like how Perl 6 code with few spaces comes in golfing distance, without the need for any ugly tricks:
# 12345679012345678901234
perl6 -e 'say 1+[max]lines».chars'
So that's 24 chars without the quotes, and no fancy command line options used .
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |