Try to find a small topic to start to do some research every day at least 20 minutes, and then build up more later on.
Today topic is about writing algorithm code - problem solving, 1st writing (study other's code and understand) vs 1st writing (with simple test case with a diagram, more focus, a small problem):
Here is the cycle Julia goes through for Leetcode 269 - Alien Dictionary:
Coding process ->
choose an algorithm to code ->
study 4 or 5 solution from over 10 solutions ->
cannot learn an algorithm just by reading, stop reading ->
write C# code based on one of them (Java, or C++) ->
add comment, make code readable, debugging ->
code works ->
wait 10 - 20 minutes ->
draw a diagram to work on a simple test case ->
rewrite the code 2nd time->
big difference, a new story to write - it takes close to one hour ->
interesting experience. ->
3rd rewrite ->
document the difference
First blog of Leetcode 269 Alien Dictionary:
http://juliachencoding.blogspot.ca/2016/06/leetcode-269-alien-dictionary.html
1st good writing in C#:
https://gist.github.com/jianminchen/85129ed50ce597f896b0f0c5a2fa5586
Afterwards, work out a simple test case, and then, write down the graph with detail data structure and data, write code based on the picture. New ideas come out naturally to improve:
1st writing focusing on the above diagram: C# implementation
https://gist.github.com/jianminchen/58d80aa86a027af7a3e52277d15c3733
3rd writing using C# code:
https://gist.github.com/jianminchen/8d4c1f601bae0ca7ef27e470dfe1e636
Highlight the differences 3rd time:
1. line 26, base case is updated: words.Length <= 1 instead of <1
2. Add comments what to find in the function alienOrder
1. spell error topological -> topoligical
2. add nodes variable as part of graph
3. getNodes function -
look into string.ToList() -> List<char>
study HashSet.UnionWith() input argument and its type IEnumerable<T>
4. rewrite graphSetup function tasks from line 62 - 79
explain very clearly to find an edge if there is one;
what to construct in the graph.
add precondition for the function.
5. rewrite the function comment for topologicalSort
6. missing line 163 - 164, run time error - array access - out-of-index
statistics:
Time spent: 3rd practice - more than 40 minutes
Quick tip:
{"wrt", "wrf", "er", "ett", "rftt"}
first char, comparison (all five words) => w -> e -> r
third char comparison (first two words - "wrt","wrf") => t->f
second char comparison (third, fourth words -"er","ett") => r-> t
So, the order is "wertf"
Comparison:
alienOrder - two things noticed documented in comment.
3rd writing - take some time to look up HashSet.UnionWith argument type: IEnumerable<T>, good time to learn something here.
Practice to write down what to do, in order to write good code, also need to explain what to do first. Good writing right side! Bravo!
Find a bug, 2nd version, left side, no edge case: return; should be continue; line 89
Also, write down tasks before writing code, good habit to train to focus on tasks when writing.
Use node in neighbors to make code more readable.
One algorithm a time. Take your time to master an algorithm.
Everyone asks me what’s next. I’m here to stay, taking it one game at a time.#CreateYourMark pic.twitter.com/CzDq5CIGMA— Angelique Kerber (@AngeliqueKerber) May 21, 2016
No comments:
Post a Comment