What is your favourite solution?

Back to General discussions forum

gardengnome     2022-02-09 20:24:05
User avatar

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. :)

zelevin     2022-02-09 20:25:22

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

Tic-Tac-Toe Minimax Algorithm (#203)

Vadim Pelyushenko     2022-03-16 21:03:09
User avatar

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.

CondorHunter     2022-03-22 13:37:53

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.

kostis_k     2022-03-31 05:31:48
User avatar

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.

gardengnome     2022-03-31 19:24:12
User avatar

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.

TestUser     2022-03-31 21:36:40
User avatar

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:

  • when really solving some problem (rather than verifying / testing something) very quick "glance" at other's solution (not to spoil it to myself) sometimes hints how wrong I am. For example recent problem with 2D turing machine - my solution had 3 distinct parts - flood-filling, backtracing and then restoring cells. Then I opened, perhaps, solution by Clive aka CSFPython I was astonished for it is almost exactly 3 times shorter. I still hope to think on improving some day.
  • also this is the way to learn unexpected things about programming languages I know and use (regretfully I'm not expert in most of them).
  • at last it is good chance to get introduced to some yet unknown language; in my case most notably it was Perl (first noticed it beeing used by 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.
sam_bandara     2022-06-20 03:31:15
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 :-)

zelevin     2022-06-21 23:41:54

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.

sam_bandara     2022-09-13 07:09:24
    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,

gardengnome     2022-09-13 10:57:46
User avatar

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).

neo_loquita     2022-10-24 13:42:08
User avatar

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 Josephu's Problem solution :c

Arif Wicaksono Indarto     2023-04-19 03:26:05

my favourite problem is sum a+b

oezguery     2023-08-10 11:06:09
User avatar

Hi!

My favorite task/solution so far has been integer factorization. My solution in Python is here.

What is new for me here is that the generator of prime numbers is infinite but together with takewhile I get a very fast solution.

Please login and solve 5 problems to be able to post at forum