Send to KindleRuby Programming Challenge For Newbies
RPCFN: Short Circuit (#3)
By Gautam Rege
About Gautam Rege
Gautam Rege (twitter / blog) is based in Pune, one of the busiest IT hubs in India. He has done his computer engineering from Pune Institute of Computer Technology (PICT) and passed out in the year 2000. After working for a few services based companies like Zensar and Cybage he got exposure to product companies like Veritas (now Symantec). It was always his aim to be self-employed and when he found the right partner in his friend Sethupathi Asokan, they both started Josh Software Pvt. Ltd..
Gautam has this to say about the challenge:
RPCFN is a unique idea on RubyLearning – and I wish I had some such help when I was starting to learn Ruby. This is learning and fun! Now that everyone seems to have got onto the challenger track, I think its time to raise the bar — very slightly
Short-circuit has a twist in the tail – its not just learning Ruby but also seeing how easy it is to implement well-known algorithms quickly and efficiently. Welcome newbies to this 3rd Challenge – all the best!
Sponsor
This monthly programming challenge is sponsored by Josh Software Pvt. Ltd. Josh Software is an upcoming Rails start-up in Pune, India. ‘Josh’ in Hindi (an Indian language), means ‘enthusiasm’. The Josh team builds state-of-the-art web based applications for various clients in various industry verticals with all the ‘josh’. The work culture at Josh involves fun at work, sincerity in coding and quality deliverables.
Prizes
- The person with the best Ruby solution (if there is a tie between answers, then the one who posted first will be the winner) will be awarded any one of PeepCode’s Ruby on Rails screencasts.
- The other two prizes, selected randomly amongst the remaining working Ruby solutions, would be any one of:
- BDDCasts’ screencasts and any one of,
- Pragmatic’s The Ruby Object Model and Metaprogramming screencasts.
The three persons who win, can’t win again in the next immediate challenge but can still participate.
The Ruby Challenge

Given a closed electrical circuit, we need to identify the redundant elements. For the sake of simplicity, we shall assume only resistors between various points. Electricity will flow through the path of least resistance! Source of electricity is A and the end-point is G.
The idea of the program is to find the redundant resistors.

As shown in the diagram we can ‘translate’ load between points into any simple data structures. For example:
[ [ A, B, 50], [ A, D, 150], [ B, C, 250], [ B, E, 250], [ C, E, 350], [ C, D, 50], [ C, F, 100], [ D, F, 400], [ E, G, 200], [ F, G, 100], ]
Feel free to use ANY other data structure as long as assumptions are simple and understandable. The source and the end-point may be hard-coded in the script or can be taken as command line parameters, whichever you feel is convenient.
In the above example, the output expected MUST be the following array of arrays:
[ [ 'A', 'B', 50 ], [ 'B', 'C', 250], [ 'B', 'E', 250], [ 'C', 'E', 350], [ 'D', 'F', 400], [ 'E', 'G', 200], ]
Hint
- ‘Short Circuit’ is a cryptic clue to the solution for this challenge.
Requirements: This has to be a pure Ruby script, using only the Ruby Standard Libraries (meaning, no external Gems). You do not need to build a gem for this. Pure Ruby code is all that is needed.
How to Enter the Challenge
Read the Challenge Rules. By participating in this challenge, you agree to be bound by these Challenge Rules. It’s free and registration is optional. You can enter the challenge just by posting the following as a comment to this blog post:
- Your name:
- Country of Residence:
- GIST URL of your Solution (i.e. Ruby code) with explanation and / or test cases:
- Code works with Ruby 1.8 / 1.9 / Both:
- Email address (will not be published):
- Brief description of what you do (will not be published):
Note:
- As soon as we receive your GIST URL, we will fork your submission. This means that your solution is frozen and accepted. Please be sure that is the solution you want, as it is now recorded in time and is the version that will be evaluated.
- All solutions posted would be hidden to allow participants to come up with their own solutions.
- You should post your entries before midnight of 20th Nov. 2009 (Indian Standard Time). No new solutions will be accepted from 21st Nov. onwards.
- On 21st Nov. 2009 all the solutions will be thrown open for everyone to see and comment upon.
- The winning entries will be announced on this blog before end of Nov. The winners will be sent their prizes by email.
More details on the RPCFN?
Please refer to the RPCFN FAQ for answers to the following questions:
- What Is The Ruby Programming Challenge For Newbies (RPCFN)?
- How does RPCFN benefit you?
- Challenge Rules
- Best Solution
- Can I Submit A Ruby Programming Challenge Topic?
Donations
RPCFN is entirely financed by RubyLearning and sometimes sponsors, so if you enjoy solving Ruby problems and would like to give something back by helping with the running costs then any donations are gratefully received.
Acknowledgements
Special thanks to:
- Gautam Rege.
- Sponsor Josh Software Pvt. Ltd..
- The RubyLearning team, namely Dave Lilley (New Zealand), Jeff Savin (Canada), Michael Kohl (Austria), Peter Crawford (Italy), Satoshi Asakawa (Japan) and Victor Goff (USA).
Questions?
Contact Satish Talim at satish [dot] talim [at] gmail.com OR if you have any doubts / questions about the challenge (the current problem statement), please post them as comments to this post and the author will reply asap.
The Participants
There are two categories of participants. Some are vying for the prizes and some are participating for the fun of it.
In the competition
- James Daniels, USA
- Harshad R Wankhede, India
- Ben Marini, USA
- Robison WR Santos, Brazil
- Mark, USA
- Valério Farias, Brazil
- Prakash Sejwani, India
- Paul Smith, England
- Himansu Desai, USA
- Rohit Sasikumar, India
- Pat Harty, USA
- Todd Huss, USA – declared winner (best solution)
- Tom, France – declared winner (randomly selected)
- Javier Blanco Gutiérrez, Spain
- Sogo Ohta, Japan
- Rainer Thiel, New Zealand
- Andy Newport, New Zealand
- Antonio, Canada
- Glenn Goodrich, USA
- Sam Johnson, Canada – declared winner (randomly selected)
Just for Fun
- Aldric Giacomoni, USA
The Winners
![]()
Congratulations to the winners of this Ruby Challenge. They are:
- Todd Huss from USA (his Ruby Challenge solution) – the person with the best Ruby solution. He wins any one of PeepCode’s Ruby on Rails screencasts.
- Tom from France (his Ruby Challenge solution) – selected randomly amongst the remaining working Ruby solutions. He wins any one of BDDCasts’ screencasts.
- Sam Johnson from Canada (his Ruby Challenge solution) – selected randomly amongst the remaining working Ruby solutions. He wins any one of Pragmatic’s The Ruby Object Model and Metaprogramming screencasts.
Previous Challenge
RPCFN: Average Arrival Time For A Flight (#2) by Chris Strom.

This challenge is now closed. Gautam Rege has posted on his blog his observations on the code submitted for this challenge. Do have a look.
- The (#4) challenge by Michael Kohl, Austria is scheduled for 1st Dec. 2009.
- The (#5) challenge by Peter Cooper, UK is scheduled for 1st Jan. 2010.
- The (#6) challenge by John Trupiano, USA is scheduled for 1st Feb. 2010.
Technorati Tags: Ruby, The Ruby Programming Language, Ruby Programming Challenge For Newbies, Programming, RPCFN, Gautam Rege
Posted by Satish Talim


{ 81 comments… read them below or add one }
← Previous Comments
Just to clarify a bit for those that haven’t studied about electrical circuits.
This is the basic idea:
The electrical current that flows by a resistor is calculated as:
I = V/R, where V is the voltage around the resistor.
In any given node, the total currents that come into the node equals the total current that flows out of the node.
The redundant resistor between two nodes, are calculated, basically, by the following algorithm.
Short-circuit any DC sources. Imagine you are placing a new DC source between two points. Calculate the current that will be driven by the DC source. The redundant resistor is the one that when replacing all circuit would consume the same current as all circuit. That means that you could replace the entire circuit by one resistor and the source would drive the same current.
But I didn’t understand what exactly would be the expected array…
Hi Rodrigo,
In the given electrical circuit, as I have mentioned, the path of electric current would follow the path of least resistance — always. So, the remaining resistors would be redundant.
We are not supposed to re-design the circuit, simply identify the redundant resistors. We can display these resistors with [ start, end, resistance ].
Hi Gautam,
Can you please rephase you problem.
———————
The idea of the program is to find the redundant resistors.
———————
Thanks,
Pankaj Sisodiya
Hi Pankaj,
As I mentioned in my earlier comment, we need to identify the resistors through which the current will not flow. “Electricity will flow through the path of least resistance!” – so the remaining paths will not be used.
The expected output is an array of such paths as I have mentioned.
Hope this helps!
Solution (#1):
#1 James Daniels, USA
Original: http://gist.github.com/222262
Forked: http://gist.github.com/222836
Confirmed Ruby 1.8
I liked his approach but documenting the code would have helped.
I think the problem author has messed up here. In reality, electricity does not only follow the path of least resistance. It follows all paths of all resitances in proportion to their conductance.
What the problem author is looking for is the path of least resistance through the circuit. That path is ADCFG for a total resistance of 400 Ohms. Therefore the ‘redundant resistors’ are those connections which are not on the shortest path, i.e. AB, BC, BE, CE, DF, EG. Why the problem author wants the resistances of the reduntant resistors as part of the solution I don’t know, but it’s his problem
Hi Paul,
AND I have little interest in the electrical part of this problem statement.
You guessed right – I am not an electrical engineer
I am however a programmer and the algorithm for the shortest path is of keen interest, as part of this challenge. Hence I clearly mentioned that electricity would flow in the path of least resistance — please bear with the terminology here.
You have correctly spotted the shortest path algorithm. The ‘twist’ is in getting the remaining resistors. As part of the ideology of the challenge, the fun and learning is in getting to the solution — not the solution itself
Regards,
Gautam
I understand you Gautam – it’s just that electricity doesn’t do that, which is why I think your readers are having problems.
Exactly. Specially because I am an Electrical Engineering
Talking about routers would make more sense though
And I believe that finding the equivalent resistor would be too much for beginners also
NOW i understand the problem. Thank you.
I think the challenge also suffers from a disconnect between the apparently intended goal and the stated goal. To solve the problem, you need to go through the effort of finding the shortest path; rather than return this path as the solution, you need to indicate which segments are *not* part of the path.
That’s actually a pretty good test of your datastructure.
@Yossef What Martin said is exactly what is interesting. Twisting the problem around a little actually encourages more use of data structures and I guess that is what the challenge is all about.
@Paul, @Rodrigo – thanks for the clarifications regarding the flow of electricity. Its is really appreciated. The premise ‘electricity flows in the path of least resistance’ is indeed a death trap in today’s world but was an accepted premise earlier.
Again: The aim of the problem is not to understand electricity but to basic programmatic algorithms. Hence my assumption stated in the problem: electricity flows in the path of least resistance.
With no offense to any electrically literate people, I hope the problem statement is clear from the point of view of the algorithm
Are we to assume that there is a single path of least resistance?
Hi Paul,
For the sake of simplicity, you may assume this in your program. However, if you can take into consideration multiple shortest paths and hence different ‘redundant resistors’ it is more than expected.
I guess the term is ‘extra brownie points’
Glad to see your keen interest in the challenge.
I have two questions:
1) should the output of the script be the array object itself, or should we write the contents out to stdout and format like your example?
i.e. the difference between ending the script with
@RedudantPaths
vs something like:
@RedundantPaths.each {|path| “\[ \'#{path[0]}\’, \’#{path[1]}\’, #{path[2]}\],”
and 2) If we do include support for finding multiple paths, how should we format the output
(Maybe both of these can be answered by letting us know if you’re visually verifying our entries, or using an automated process)
Thanks!
Sam
Hi Sam,
We are using automated scripts for verifying and using an array of arrays as input for testing. We are expecting the array of arrays as output as mentioned. This could be printed on console or in an instance variable – no worries, as we have scripts which manage both.
Regarding multiple shortest path, you may choose the first one you have encountered.
+100 brownie points already for considering this
Cheers!
Nice question!
Otherwise I’m glad I hung around a little and took note of all the comments so far. They have certainly clarified the requirement for me. Thanks to you all.
Seems to me that you can think of this as a traveling-salesman type of problem. You need to figure out the “shortest” route (eg least resistance, no pun intended) from point A to point B. All paths that are not on this route are considered “redundant”.
While you did say that “electricity flows through the path of least resistance”, the electrical circuit metaphor is pretty misleading for EEs since electricity is like water and flows through multiple paths depending upon their resistance.
Hi Pete,
Probably lucky not to be crucified by all EEs because I have made a clear assumption in my problem statement that ‘electricity flows through the path of least resistance’. Though this may be wrong (as has already been pointed out earlier) I guess, its ok from the program point of view!
I hope you agree with me — you have the logic spot on!
- Gautam
You are right that you clearly stated the assumption, we EE’s definitely overlooked that statement and drew upon our EE background. Its good that this is being clarified very early (and shows the enthusiasm for the contest). Thanks for creating the challenge, I appreciate it.
Solution (#2):
#2 Harshad R Wankhede, India
Original: https://gist.github.com/932dda6ee34590624df8
Forked: https://gist.github.com/33a2fc37cd23c9a163ec
Code works with: Ruby 1.9.1p0
I like the comments at the start as it will help ANY one following to quickly understand the use of variables etc.
While the code may not be super readable, I didn’t think it was bad. The comments really help as Harshad explains a bit of what he is doing. Recursion always excites me.
Thanks Dave and Jeff for your comments,
I thought implementing Djikstra’s algorithm is the correct way, but I wanted to have a solution of my own. Something which I will enjoy and is quite new. I didn’t expect it to be a correct approach btw
- Harshad
Small typo in the expected output. The last element of the array should not be followed by a comma and the element E has a closing double rather than single quote. It should probably read:
[ 'E', 'G', 200]
It might make evaluating the entries easier if you did specify some input format… so testing each persons code against a few test cases will be easier.
Cheers! Looks like a fun one.
Thanks Tom. Typo corrected. BTW a trailing comma is ignored by Ruby, as shown in this example:
Thanks Tom for pointing this out.. this is much appreciated.
I checked solutions test code and it was corrected there already. Guess I forgot to update the problem statement.
Regards,
Gautam
Hi – Would like a clarification before I try to solve this. The suggested data structure ([ B, E, 250], [ C, E, 350] etc.) implies that the “direction” heading from closer to Source (first element) to closer to Earth (second element), is already known. The flow of current for this particular exercise (A->D->C->F->G) also hints that current (always) flows from ‘closer to Source’ to ‘closer to Earth’. This is not true – strictly speaking – and I wanted to clarify if our solutions are expected to solve the more general case. For example, for certain values of the resistors, the shortest resistance path can be A->B ->E->C->F->G. Note that for the E->C segment, current is moving from ‘closer to Earth’ to ‘closer to Source’ i.e. away from the normal or intuitive direction. Data structures that assume the direction of flow “before” calculating the shortest path will be incorrect for some values of the resistances. Thanks
Wow Himanshu!
Nevertheless, assuming the assumption in the problem statement ‘Electricity flows in the path of least resistance’, I think its all about algorithm and programing and NOT really about electricity and resistance.
Thanks for the insight on flow of electricity — this one new to me. For this particular program, I had not taking the earth into consideration.
Hope this makes sense and the program easier to understand !
Regards,
Gautam
Sorry Gautam – Long winded and confusing verbiage. Didn’t mean to talk about the science of electricity. Just trying to talk through something that may be obvious to all but a source of confusion for me. The algorithm should analyze both directions of a particular segment right?. In the above example, if we had [ A, B, 1], [ A, D, 3], [ B, C, 3], [ B, E, 1], [C, E, 1], [ C, D, 3], [ C, F, 1], [ D, F, 3] [ E, G, 4], [ F, G, 1], the path of least resistance is A -> B -> E -> C -> F -> G (with other resistors being redundant) i.e. when going from source to target, trace from C to E or from E to C, whichever one leads to the least total resistance.
Hi Himnashu,
Both directions should be considered. The array is simply a translation of nodes and resistance between them. So, trace can flow from C to E or E to C.
Solution (#3):
#3 Ben Marini, USA
Original: http://gist.github.com/223789
Forked: http://gist.github.com/239907
Code tested on Ruby 1.8
I thought the code to be a bit long. Over use of Classes – Edge, Vertex, Graph. The program did accomplish what was asked of it, and quite well.
Solution (#4):
#4 Robison WR Santos, Brazil
Original: https://gist.github.com/7563ec4042497f4ecf52
Forked: https://gist.github.com/81c44daaa747e1fcbf4c
Code works with Ruby 1.8
What I liked best about Robison’s, is his very clear and detailed explanation about how he went to solve the problem. Too bad he got one failure in the tests , I thought he had a pretty good algorithm and method for solving.
Solution (#5):
Mark, United States
Original: http://gist.github.com/224398
Forked: http://gist.github.com/239918
The code works well. A little recursion might be a shorter and more elegant solution. Oh, and I love commented code. Too bad this program had zero comments.
Solution (#6):
Valério Farias, Brazil.
Original: https://gist.github.com/ebea6c81253e73f32986/0c7eaf2b3c8196077149b2e2df727e51ae6b086e
Forked: https://gist.github.com/f4085dff1e241dc3e098
I created a class Graph. This class contain a method dijkstra
to find the smallest path in the graph.
The code works with Ruby 1.8
I liked this solution. One of the shortest of the non-recursive entries, so far. The code was well commented and therefore more readable than if it had been left alone. What I love about this job, if you can call it that, is you learn new things all the time. The 1 << 32 was a new one for me, but pretty slick. I'll remember that one!
Solution (#7):
Prakash Sejwani, India
The code is in ruby 1.8.7
Original: git@gist.github.com:02194fa01ba59132e128.git
Forked: https://gist.github.com/b3a39b13e07a13ad4055
Hmmm, this didn’t seem like a solution to solve the problem in general, but specifically the exact circuit given to it. This of course makes the program inflexible and in general, not much use.
Solution (#8):
Paul Smith, England
Original: https://gist.github.com/1b12e9187b5060c28c2e
Forked: https://gist.github.com/602034e12a217dff7c0e
Code Tested with Ruby 1.9
I liked this code, although not recursive. Was very well commented and readable. At least, it seemed to make sense. Also performed well on the tests.
Solution (#9):
Himansu Desai, USA
Original Gist URL: git@gist.github.com:ef455ce56ed80ff73f5e.git
Pastie: http://pastie.org/682803
Recursive, nice. Very well commented, nice. Passed all tests, nice. However, it seemed like more code than necessary to accomplish the desired results, but maybe that’s just me. Overall, a pretty strong solution.
Solution (#10):
Rohit Sasikumar, India
Original: https://gist.github.com/0ff485999bc8be8f7fc4
Forked: https://gist.github.com/2419497a5f573a691d69
Code works with Ruby 1.8
The algorithm is the one made famous by Dijkstra, but the implementation has an issue as it failed one of the auto_check tests. The other thing I wasn’t sure about was Rohit’s comment that the flow of the current making a difference. I’m not sure it should?
Solution (#11):
Pat Harty, USA
Original: https://gist.github.com/651744c264a87e91ad56
Forked: https://gist.github.com/26c9753a8b5fb4acee4d
Description of Code: Objects were created for each resistor and the end points that it connected (e.g. A B 50). These objects were kept in an Array. Traversed the circuit by taken the start point of A searching for ResistorLink objects that contained that value as one of the links then traveling to the other point and doing the search for that point adding the value of the resistors along the way. These points would be searched and traversed until either the endpoint ‘G’ was reached or there were no more valid points to travel to. At that point the route taken would be added to a deadend list so it would not be followed again. Also, a list was kept so the same point would not be traversed more than once. When a successful start to finish route was found it was added to an array of successful paths that would be searched through to find the path of least resistance after all possible routes had been taken. Then when the path of least resistance was found, the array of ResistorLink objects was searched and if they were found to be a part of the least resistant path they were marked as Used. Going through the array one last time searching for Unused ResistorLink objects gave us our list of redundant resistors.
In the description of the challenge it mentioned the results for the example must be an array of arrays. Wasn’t sure if output for all solutions was wanted in an array of array’s format or if that was specific to that example, but I don’t have the output in that format so hopefully that isn’t the case
Thanks for creating this challenge I had a really fun time working on it.
Well, this guy is a self-described Ruby newbie, which I guess is who we are targeting the challenge too. He tackled the problem, but used a fair amount of code to do so. Unfortunately, according to the auto_check, his program short-circuited a bit. On the positive side, Pat’s code was well commented.
Solution (#12):
Todd Huss, USA
Original: http://gist.github.com/228577
Forked: http://gist.github.com/228612
Code works with Ruby 1.8 and 1.9
Wow. Excellent solution. Todd went about solving this problem by meeting his intended goals of not using Dijkstra’s well-known shortest-path algorithm, but instead creating his own recursive algorithm. The results, simple, easy to read, and with a 100% pass-rate. This, so far, is hands-down, my favorite. The added bonus was solid RSpec code to match.
This solution is way ahead of the competition in terms of readability and elegance. Very, very nice!
Solution (#13):
Tom, France.
) find the least resistance path and return the unused edges.
Original: https://gist.github.com/8a70b216514ab5edab5d
Forked: https://gist.github.com/31f3f0dd0b9533277e92
Code works with Ruby 1.9 (not tested with 1.8).
Code explanation:
The main part if the PathFinder which tells the path going from the specified start to the end. Then the Electricity (hehe real bad name
Possible improvements:
- Use a closure to run the recursive stuff (I think the closure can retain the context [the list of node] instead of having a method parameter)
- Create an “Edge” class (start, end, resistance)
- Rename the Electricity class for a better name
Good implementation and passed all tests. Seemed like a lengthy solution, but not hard to read and included test cases. Solid and well done.
Solution (#14):
Javier Blanco Gutiérrez, Spain
Original: http://gist.github.com/223826
Forked: http://gist.github.com/232911
Works with both
Another solution using Dijkstra’s algorithm and again, a bit lengthy. Seems like results are good with Satoshi Asakawa’s slight code massage. Code could be commented a bit more, but not bad. Overall, another solid solution.
How are you going to handle the input of various test cases?
Are you going to make a YAML file? Or maybe, as your example suggests, a text file with an array of arrays?
I’d like to make your life -a little- easier.
Hi Aldric,
Our team has already written some autoeval script to automated testing. The input for these are the arrays as I have mentioned in the problem statement.
However, if you are using some different data structures, it would greatly help if you would also write some test cases using Test::Unit. So, you can evaluate your own results.
Your interest in making our life easier is much appreciated
Solution (#15):
Aldric Giacomoni, USA
Original: http://gist.github.com/226079
Forked: http://gist.github.com/235140
I am submitting this code for fun, not for the prize
Pretty clean solution. Worked on all our test cases. Uses Dijkstra’s algorithm. Decent comments and code readability. Overall, very nice.
I’m keen to get on and solve this problem but I’ve got to say it’s unlike any I’ve tried before. It seems a bit more computer-sciencey and a bit less web-scripty than what I’m used to. Can someone suggest a good source of information on the way to approach a problem like this? I can think of a few ways but I suspect they are unnecessarily complicated.
Thanks.
Brad, the comments so far have indicated that “shortest-path”, along with keywords like ‘algorithm’ might help you. Google will be your friend, and wikipedia will be as well
Thanks Aldric (again) for helping out!
@Brad, As has been hinted, the problem statement may look ‘electrical’ in nature but is in the end a standard algorithm that is freely available. A quick read of the comments earlier would give you enough clues on how to solve this problem and what the expectation is.
Solution (#16):
Sogo Ohta, Japan
Original: git@gist.github.com:dc8bf2d80c27756f31f4.git
https://gist.github.com/dc8bf2d80c27756f31f4/b020e6b021dd014da07d5da1cea757faf1c52565
Forked: https://gist.github.com/e0a5a786c2d90e82bf32
Works on Both
Small code footprint, but unfortunately, doesn’t seem to work in all cases. The code is not commented at all, decreasing the readability a bit. I like that he went about this trying to come up with his own solution rather than the already well-overused Dijkstra’s algorithm. Too bad it didn’t quite work as planned.
Solution (#17):
Rainer Thiel, New Zealand
Original: https://gist.github.com/5bc670cb26ca89ae7f3a
Forked: https://gist.github.com/b19a4f95fd09e2a5bed3
Wow ! A lot of time was spent on this solution and working or not, there was a lot of thought put into this as evident by the amount of code and extremely thorough documentation. Too bad this didn’t work, as I would’ve liked to have seen how it handled the auto_check test cases.
Hi Jeff,
Thanks for your look at my solution. What is the auto_check test that my solution failed? I thought i had something ok going there…
Sensei Satoshi Asakawa has a series of test cases that all solutions go thro’ and auto_check test was one of them.
Solution (#18):
Andy Newport, New Zealand
Original: http://gist.github.com/237354
Forked: http://gist.github.com/239937
Code tested with Ruby 1.8 on Windows
Once again, looks like a whole lot of code to achieve the results. At least, according to auto_check, no failures, so that’s a plus. Code is well-commented and not too hard to read. Algorithm is again, the Dijkstra algorithm.
Solution (#19):
Antonio, Canada
Original: git@gist.github.com:75968c5b5a0f0f8f7a83.git
Forked: https://gist.github.com/5f939c6052e0401d5b92
Tested extensively during writing, no test cases included.
Written on 1.8.6 tested on 1.8.6
Dijkstra’s algorithm. Well commented, yet again, a good amount of code to achieve the results.
Solution (#20):
Glenn Goodrich, Charlotte, NC, USA
Original: http://gist.github.com/222855
Forked: http://gist.github.com/239940
Glenn says above, “Don’t laugh too hard.” Well, I didn’t laugh. I thought his solution was well thought out and the code was nice to read. Its too bad the code was dependent on the data structure and failed the auto_check test in a few spots.
Solution (#21):
#21 Sam Johnson, Canada
Original: https://gist.github.com/5ed31a447fb4c03a40fc
Forked: https://gist.github.com/f26ca1ef60cef80ca9a7
Simple recursive solution that moves through all paths, and saves the cost for each path. This algorithm only works in a reasonable amount of time because there are so few nodes in this network.
Cool. Real short and sweet recursive solution. One of the better ones and passes all tests.
Andy Newport posted some interesting comments regarding the winning solutions – the debate of Recursive Vs Iterative approach
Very interesting read:
http://gautamrege.wordpress.com/2009/11/20/rpcfn3-short-circuit/
{ 18 trackbacks }