The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
Dev Shed Forums
> Programming Languages
> Python Programming
Shortest Codes
Discuss Shortest Codes in the Python Programming forum on Dev Shed. Shortest Codes Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Dev Shed Forums Sponsor:
January 17th, 2013, 06:10 PM
Registered User
Join Date: Jan 2013
Posts: 1
Time spent in forums: 29 m 29 sec
Reputation 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?
January 17th, 2013, 10:00 PM
Contributing User
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.
__________________
[code]
Code tags [/code] are essential for python code!
Last edited by b49P23TIvg : January 21st, 2013 at 05:12 PM .
Reason: Restate 1st python solution
January 18th, 2013, 09:31 PM
Contributing User
New python3 solution in unix! 2 characters shorter.
January 19th, 2013, 01:08 AM
Contributing User
Join Date: Feb 2004
Location: San Francisco Bay
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 .
January 19th, 2013, 08:25 AM
Contributing User
"best" python solution reduced!
January 19th, 2013, 05:47 PM
Contributing User
Join Date: Dec 2012
Posts: 74
Time spent in forums: 1 Day 2 h 22 m 37 sec
Reputation Power: 2
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
January 19th, 2013, 07:24 PM
Contributing User
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 .
January 21st, 2013, 05:17 PM
Contributing User
Shortened j sentence with monadic cut.
Last edited by b49P23TIvg : January 21st, 2013 at 05:22 PM .
Developer Shed Advertisers and Affiliates
Thread Tools
Search this Thread
Display Modes
Rate This Thread
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off