Back to General discussions forum
I am genuinely curious what people consider their favourite own solution here on CodeAbbey. It could be their solution to a particularly challenging problem, or something where they learnt something new, a clever algorithm, a short solution, and so on. I really like to look at and understand the solutions of others to learn and get inspired.
Obviously don’t post any solutions themselves here.
I make a start: one-line Python code for #33 Parity Control. That was the first time I thought that I might like Python after all. :)
it was posted in this topic and replied to message there, but admin decided to try moving post manually as experiment, sorry for inconvenience
I'd say in general life we prefer to write clear, not clever :)
I've had a few arguments about this. :) My reason for writing terse code is not because I like clever for clever's sake and not because I think it would run faster. The reason is that writing short code gives me fewer chances to make mistakes.
Some of the arguments are summarized here (not by me): http://wiki.c2.com/?OnceAndOnlyOnce
Anyway, speaking of our own favorite solutions (and also one-liners), for me, this is
My favorite solution amongst my own would be for the Dungeons And Dragons Dice (#53) problem. Because, at least as I understand, my solution perfectly models the probability that a sequence is generated by a particular dice set (well, okay, not actually 100% perfectly because there will be rounding errors with representation of probabilities as doubles, but it models it fairly precisely when there haven't been many rolls). It could also do with some refactoring, but at least I'm very happy with the core idea for the solution.
The proper bracket sequences problem (#140) felt immensely satisfying to solve. I spent probably at least 40 hours pouring over notes, looking for patterns, trying crazy ideas. In the end the solution was silly simple, but it gave me confidence that I could solve seemingly complex problems through perseverance. Similar thing with Towers of Hanoi.
First of all, I know my input will have little value because most of my solutions are in Mathematica (only just recently in Haskell), but let's chip in anyway. I have a few "favorite" pieces of codes, attributed mainly to elegance or brevity, but I 'll just mention my solution to Problem 100 "Crossing the road", not because it's up there at the top ten, but because it made me sweat. I must have been trying to solve this for almost three months on and off (I realize that's how much it took some people to get a certificate, but hey...), I had to learn Mathematica IO (file handling) for the first time in order to comb through 60GB of permutations before deciding to, you know, maybe try sth different. Needless to say I came out knowing the difference between depth first search and breadth first search... Also, because I have the habit of solving the problems in more or less numerical order, I went on to solve problem 101 in approximately 5 minutes and I was literally pulling my hair out! I also learned that day to not judge a problem's difficulty by the number of solvers. It is a metric allright but not an absolute one, so it gave me the confidence to tackle more difficult ones. So let's call Problem 100 a turning point for me, as much as it is so numerically :)
And since I'm here I'll make the leap and talk about my worst one, for Problem 162 "Rubik's cube". I knew already from the size of my solution that it was the stupid way to go, the Rodion's notes on the problem sealed the deal!
I know the OP is talking about "own" solutions, but I also have to wonder: how often do people look at other's solutions? I mean, by the time you solve a problem, you 've put some thought to it, done the research if necessary, and feeling pretty confident by the time those sweet green letters appear, to bother with someone elses approach, or is it just me and my niche language choices? I 've always considered much easier to write some piece of code than to understand someone else's.
Love reading all your responses - they show how different and diverse our motivations are. More please! :)
In response to kostis_k
, I do look at the solutions of others regularly.
I’m mainly curious about two things: the algorithmic approach, and the programming style.
I have always learnt and improved my programming this way – study good examples, and then implement it myself.
Though I also sometimes try solving some of problems, I dare not call any of solutions satisfactory :) But wanted to add "my 5 kopeks" to the latest thoughts:
I know my input will have little value because most of my solutions are in Mathematica
From the other point of view, it is of great value exactly for the same reason. Your solutions provide others with opportunity to look "at different angle". Good "reference set" of code snippets for whoever decides to learn more about Mathematica (I feel it important though never could get much used to it). It is similar to "TF/IDF" algorithm - what is rare, is more valuable - though not matching every request, of course :)
how often do people look at other's solutions?
Besides points mentioned by Mathias aka gardengnome
, I would add two... or rather three:
Clive aka CSFPython
I was astonished for it is almost exactly 3 times shorter. I still
hope to think on improving some day.Quandray
- and much later by pearlbarley
; I recalled trying to learn it
many years ago - and started trying again - and while I doubt it is great for everyday use, it appears
convenient for server-side scripts time from time.The Adventure of Morse Code (#277)
This guy "zelevin" fantastic man, just 5 lines of code. For me I took 38 lines to do the same thing :-)
Sam:
Thanks for the kind words!
My solution (for my own problem!) is compact just to demonstrate the simplicity of the most straightforward approach. From the perspective of algorithmic efficiency, I happen to think that your solution is vastly superior.
The Problem of Too Many Clues Problem #307
Gardengnome, what a nice logic mate. What I confused was, why do you pop out the candidate if the antecedent is in?
Cheers,
Have a look at the wikipedia article about material implication. In the first paragraph it states a formula P -> Q is true unless P is true and Q is false. I don't filter rules when the antecedent is in. Rather I filter rules out when the antecedent is in (true) AND the consequent is not in (false).
I do not consider myself good at coding, and to me feels really good to solve something that you consider "hard"
just to notice that there is an easier or better way to do it hahaha.
It has happen to me a lot of times, but the time that I remember with more nostalgia, is when I solved Josephus problem.
After submitting I saw that there is a formula to solve that!...
my favourite problem is sum a+b