Wednesday, December 13, 2017

Convert to string not using itoa similar function

Dec. 13, 2017

Introduction


It is challenging to review the code I wrote back to Oct. 2013. I like to review the code as I did review code 8 years ago. My last blog was written in the blog titled: Algorithm: possible triangle.

Time line: Oct 2013


Here is the code I wrote in October 2013.

Time line: Dec 2017


Here is the code I wrote in Dec. 13, 2017 using recursive function.

Here is the code I wrote in Dec. 13, 2017 using iterative solution. Need to work on a simple version, I like to practice and learn some APIs, so I kept the version for my learning.


Code review 


I like to review the code written in 2013, the code is here.

Analysis of algorithm


First, the analysis of the algorithm is good in 2013's code, but it takes 10 minutes to write, it should take less than 5 minutes.

The whole paragraph should be organized better. And the comment should be written in the following based on my understanding of depth first search, I learn a lot from Hackerrank contest recursive function failures.

Depth first search


Use depth first search to do the work, recursive function can be designed in the following:
Do at least as possible, smart programmer learns math in computer science first. Being lazy is the status of master of math in computer science. Read more book called basic mathematics in computer science and figure out the depth first search, only thing I need to do is to figure out the base case.

What is base case? One digit number, just output new char[]{(char) (n + '0')}.

What is recurrence formula?
We handle the rightmost digit first, so the string can be concatenated as recursive + base case. If the integer is negative number, '-' should be the first char in the string.

How to get rightmost digit? use % operator, and then work on subproblem which is n/ 10.

Good things in the writing on Dec. 13, 2017. Algorithm is specified using depth first search, recursive function. And then the base case is clearly explained in the paragraph. And then recurrence formula is easy to work on.

Code execution


Let me go over line by line.

1. Line 20, int[] should be char[]. Return type is char[].
2. line 23, return char array, should be new char[]{'0'}, not "0".
3. line 28, do not change function argument. Declare a new variable.
4. line 25 - 30 should be written in the following:
var isNegative = n < 0;
var postiveOne = isNegative? -n : n;
two lines instead of six lines.
5. line 31, comment: "at least I have one edge case, continue later. need idea?"
Remove the comment!
6. line 37, for(;;), use while(true)
7. line 39, digitOne variable should be rightMost.
8. line 48, 49, move at the beginning, emphasis that it is the base case.
    if( loopInt < 10)
   {
   }

Overall, the code is not structured very well. The code should be self-documented. And the code is written as an iterative solution, but recursive function should be quick and easy solution to write. I was not a very good programmer back in 2013, I have to face the reality after the review of the code.

What happened after 2013?


Let us read the answer Julia wrote Top 10 reasons to write a coding blog on quora.com in 2017.


Action items


In order to punish myself do not focus on learning basic mathematics in computer science, I decide to spend at least 5 hours to learn vocabulary in the book, at least collect five hundred words. And those words will be posted here.

This is the drill I learned when I watch China Open tennis professional players did, after they tried to hit Mercede sign, the athletes have to do push up because she/ he fails.

No comments:

Post a Comment