Thread: Shortest Codes

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Shortest Codes


    what is the shortest way to write a python code to open a file and print the length of the longest line in the file?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480

    Target practice


    Here are "one liner" solutions in 3 languages. They all read stdin. jconsole from www.jsoftware.com
    Code:
    $ alias jc=/usr/local/j64-701/bin/jconsole
    
    $ jc      -js 'exit echo#{.[;._2[1!:1<3'
    $ gawk    '{b=length;a=a>b?a:b}END{print a}'
    > python3 -c 'print(max(map(len,open("con:")))-1)'
    $ python3 -c 'print(max(map(len,open("/dev/fd/0")))-1)'  # map by Nyktos
    $ python3 -c 'print(max(len(L)for L in open("/dev/fd/0"))-1)'  # /dev/fd/0, no readlines by Lux Perpetua
    python2 could be one character shorter. Puzzle remains open. (I assume those of you who know the answer are nice enough to not tell.)

    I've assumed the last line ends with a new line character, and that there's at least one line.
    Code:
    $ python3 -c 'import sys;print(max(len(L)for L in sys.stdin.readlines())-1)'  # original python solution
    These are some targets for you all to beat.
    Last edited by b49P23TIvg; January 21st, 2013 at 05:12 PM. Reason: Restate 1st python solution
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    New python3 solution in unix! 2 characters shorter.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Continuing your train of thought:
    Code:
    $ python3 -c 'print(max(len(L)for L in open("/dev/fd/0"))-1)'
    Last edited by Lux Perpetua; January 19th, 2013 at 01:10 AM.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    "best" python solution reduced!
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    You can use map in place of the generator expression to shorten it further:
    Code:
    print(max(map(len,open("con:")))-1)
    print(max(map(len,open("/dev/fd/0")))-1)

    Comments on this post

    • Dietrich agrees : nice thinking
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Opening a specific file, as a different interpretation of the problem statement:
    Code:
    $ alias jc=/usr/local/j64-701/bin/jconsole # www.jsoftware.com
    
    $ jc      -js "exit echo#{.[;._2 fread'filename'"
    $ gawk    '{b=length;a=a>b?a:b}END{print a}' filename
    $ python2 -c 'print max(map(len,open("filename")))-1'
    $ python3 -c 'print(max(map(len,open("filename")))-1)'
    Whoot! Found an historical feature of gawk.
    Whoot! Improved the j code.
    Last edited by b49P23TIvg; January 21st, 2013 at 05:19 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Shortened j sentence with monadic cut.
    Last edited by b49P23TIvg; January 21st, 2013 at 05:22 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo