|
As some of you know, code-golfing means taking a problem & coding it in a way that takes the fewest characters. You can check out some examples of golfing at StackExchange CodeGolf, but fair warning quiet a lot of them will make your head explode. What I propose is that we have a Golfing challenge of our own here. Take any problem from any of the units, and golf it. You can use homeworks that have been closed - That means (as of right now) questions from homework 1 are allowed, but not from homework 2. Solutions that I find delightful might get rewarded some karma by me, so get Golfing Tips
For more tips on Golfing in Python, check out this thread - Tips for Golfing in Python Format
PS : This question was inspired because of this question by @robmurtha
showing 10 of 11
show 1 more comments
|
|
Here's a function that takes a webpage and extracts all the links in the format we're currently using:
It assumes that there has already been an (To clarify, this is a variation of the |
|
A Python interpreter (not contained in any of the units, but still fun IMHO) 57 Characters
This is a read-eval-print loop. |
|
Finding the factorial of a number. 28 Characters
|
|
Find last, avoiding 52 Characters
|
|
Finding the $%n$%-th Fibonacci number. 38 Characters
|
|
ps4-4 "Better Splitting" 125 Characters
I kept the long function name I provide an explanation in this thread. 1
88 Characters
Converted from comment to answer and back again, but which is the preferred way to keep answers together? The commenting won't scale for longer code blocks, new answers are not kept together. Maybe 1 forum question for 1 actual problem with the tags One downside that I see is that it would mix the Pythonic answers with the mangled code-golf anwers. Maybe we can have a convention to start all short solutions from a Pythonic one. 1
And I thought my solution was short... :) Well done. What I don't understand is, why 84 Characters
As to your organizational questions, maybe @elssar in his capacity as creator of this thread should make a decision. I think you are right about the
And it makes sense to have a unified tag for |
|
unit6-10 "Palindromes", using the recursive strategy sketched in the specification 63 Characters
unit6-10 "Palindromes", not using the recursive strategy sketched in the specification 33 Characters
|
|
Looks like I'll have to get the ball rolling. Finding the factorial of a number. 36 Characters
Explanation here Rewriting using lambda 31 Characters
|
Cool challenge! I'll enjoy watching the solutions, but I'm not even going to try (yet). Maybe in the weeks to come.
Although the challenge sounds like fun, I think I'll give it a miss until I understand what good, readable code is so that I know when what I'm writing is poor code from a maintenance point of view. I don't want to get into bad habits!
@fnenu Think of it as a way to challenge yourself to find innovative solutions. Short code doesn't necessarily mean bad bad.
Isn't just using short variables a bad thing? I guess the problems themselves might be interesting to try.
@fnenu it's a bad thing if you make it a habit. Also, sometimes it's a good thing, like n, x for number or s for string. i, j for iterating in loops or for indexes in arrays/lists/strings.
Why not choose an assignment and then have everyone golf the same one? It's hard to compare entries if they solve different problems.
@jesyspa because it's not an objective challenge. It's just a place to have fun with your code & show off a little.
for simple problems, like computing simple mathematical functions, using short names (even one-letter names) is fine and the resulting shortest expression is, in my opinion, still fun to look at because you get to understand it after a bit of looking at it.
for more complicated problems, it's not as much fun, because often the solution is so obfuscated, it's not even interesting to read it (unless it's a truly remarkable thing it is doing... i've seen some one-liners used to separate different humans voices in a recording... now that's really neat!)
Short names aren't so bad; what's bad is naming all your variables things like IOOIIIOO and OIOOOIIOI.
When I was a student at K-State, one of the systems programmers quit or was fired. He "kicked the wastebasket" on the way out, running a small piece of code that remapped all tokens in the operating system routines to such names. The code still ran, but...
I think it took about two days to fully recover the system.
@kilaws: Double fun in cases where only the first six letters are considered during lookup. :D
Heh, reminds me of a part, I think at the start, from the autobiography from Ejovi Nuwere, where he was hired to revoke all access a programmer or sys admin had while he was getting fired.