Friday, August 19, 2016

Leetcode 125 - valid palindrome - 5+ practice

August 19, 2016


problem statement: 


Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Summary of practice: 

After 2+ hours workout on coding, with  8th practice, Julia sets pragmatic goal - easy to control:
1. No nested if statement
2. No else statement
3. return early in the function 
4. work smart to work around the logic to fit in rule 1, 2, 3

Arguments of goal:
1. even if the code is written with a bug, easy to find, quick to find
2. at least more readable, aesthetic better


8th practice code: 





A table to summarize the workout:



ID Problem Practice  Summary  Statistics
1 a bug 1st practice  increment one, should decrement
2 a bug 2nd practice run time error, logic with a mistake
3 pass Leetcode online judge 3rd practice  Fix bugs in 1st and 2nd version. But code is too much with if/else if - 2 times, else if - 3 times, else - 1 time
4 pass Leetcode online judge 4th practice  Work on 3rd practice, remove else if statement if - 4 times, else - 1 time
5 5th practice  Remove extra checking in while staement (line 44) if - 4 times, else - 1 time
6 6th practice Break the rule, nested while loop, but it works better, code is more clean while - 2, if - 1 time
7 7th practice  skip left char, or skip right char or fail because unmatching pair of chars, else statement for rest if - 1 time, else if - 2 times, else - 1 time
8 8th practice Set pragmatic goal, no nested if statement, no else, based on 4th practice if - 4 times

Practice to win - have some strategy - 
1. Using nested while loops twice, 8 out of 10, no bug with excellent code. 
2. Use if, and else if 3 times, 3 out of 10, easy to create bug, hard to argue
3. Use all if, no else, no nested if, 10 out of 10. In future practices, figure out later!



9th practice: 



(after 3+ hours study on code styles, work on issues on practice #8)

9th practice C# code


5+ practices - a journey with online judge/ blog searching/ what to search after practice I - (try to find a smart person working on if/else statement)

Leetcode 125

1. Choose the study code

Leetcode 125 - write code based on this version

short function name - isalnum - very good name

a blog to read

5 practices - interest journey !

1st practice


failed static analysis - miss a bug - easy to spot --, not++

1st practice C# code

there is a bug in first writing, line 62, shoud be right--; not right++.
static analysis did not catch the bug - be careful next time.

2nd practice


2nd practice C# code

highlights of practice 2:
1. Fail to pass the online judge
2. line 24 - 27, logic and reasoning has flaws
logic with a mistake:
both are alphabetic/ number
first one is not 
else - 

should be: 
both are ok
both are failed
A is failed
B is failed
Those are 4 cases - in the specific order as well.

3rd practice

C# code 

highlights of 3rd practice:
1. Fix the bug first, add these line 59 -67 two "else if", one "else", pass online judge
2. But code "else if" from line 59 -67 can be shortened, avoidable.

4th practice


C# code

break Remove "else if" statement, make it more flat - line 60 - 65, only two if, avoid " else if" in 3rd practice.
In other words, avoid too complicated if checking

3 cases:
3 if statements:
1st if:
both are ok

Two ifs:
a is not
b is not

Good things in the 4th practice:
1. Inside 1st if statement, line 51 - 56, positive checking first (line 49), then negative.

Declare explanation variable for line 49, and line 51, make it more readable.
Good news! Julia sets pragmatic goal - easy to control - no else statement, no nested if statement.

Practice 8 based on practice 4:
https://gist.github.com/jianminchen/54200adc1b68928b46fef5a087721f57

End of Good news!

5th practice:


https://gist.github.com/jianminchen/8f9ae3839499718790180aa58965d3de

6th practice:

follow the flow of real processing - skip left/right if need, comparison failed, or continue to compare

7th practice:


Actionable Items:
1. If else is a maze, easy to make mistakes. Things can work on: 
  1. List of approaches - 
  2. Possible bugs - 
  3. What is best strategy to approach?  
  4. What can be trained on? 

Based on practice 4, work on practice 8th, and set practical goals which are easy to control, even it takes more time to write but easy to maintain/ share the code - help to reduce bugs. 


8th practice




Controllable goals: 
1. No else statement - how? practice 8, line 54 - 80, only 4 if statements (line 64, 67, 75, 78)
2. No nested if statement - declare short explanation variable, make a few of conditions checking. (two if statements, line 64 go first, even it is negative checking, line 67 after line 64)
3. let return case go first
4. hide else relationship - but with more careful static analysis 

Things to break:
1. Always check is True, no negative checking
2. Extra variable for explanation, summarize
3. Scope of variable - not ideal case
4. Avoid nested while loop, let outside while take care of business
5. ...

Problem with practice #8:   (After 3+ hour study and study
 http://juliachencoding.blogspot.ca/2016/08/leetcode-125-valid-palindrome-summary.html )
study guard clauses, 
http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html

1. first, the style does not match guard clauses style, 
    All guard clauses should stay together, and before the clause.

    Put line 75 if, line 78 if into guard clauses, and then, before the normal business:
    line 67, if(isComparable && isSame)

2. 4 if conditions are not the same level (not same abstraction level!)
     1st if:  isCompare &&  !isSame
     2nd if: isCompare && isSame 
     3rd if:  !arr[0]
     4rd if:  !arr[1]

 1st, 3rd, 4rd if are guard clause
 put the idea in next practice: 9th practice
     1st  if:  !isCompare && !arr[0]
     2nd if: !isCompare && !arr[1]
     3rd  if:  isCompare  && !isSame
     4th  if:  isCompare &&  isSame


Practice #9:



9th practice: (after 3+ hours study on code styles, work on issues on practice #8)
https://gist.github.com/jianminchen/ce79fbd9c3c97b628d8857f55e684aab

Editorial notes:
It is time consuming messing with if/else statement. 
Facts:
1. 2+ hours programming workout
2. Over 5 practice to experience up and downs, 
3. Hard to please a programmer if the code is working but take time to reasoning. 
4. Celebrate 2+ hour workout on Leetcode 125 - read

De Morgan's laws


Here is the table of 8 practice: 


ID Problem Practice  Summary  Statistics
1 a bug 1st practice  increment one, mistake: decrement  one
2 a bug 2nd practice run time error, logic with a mistake
3 pass Leetcode online judge 3rd practice  Fix bugs in 1st and 2nd version. But code is too much with if/else if - 2 times, else if - 3 times, else - 1 time
4 pass Leetcode online judge 4th practice  Work on 3rd practice, remove else if statement if - 4 times, else - 1 time
5 5th practice  Remove extra checking in while statement (line 44) if - 4 times, else - 1 time
6 6th practice Break the rule, nested while loop, but it works better, code is more clean while - 2, if - 1 time
7 7th practice  skip left char, or skip right char or fail because unmatching pair of chars, else statement for rest if - 1 time, else if - 2 times, else - 1 time
8 8th practice Set pragmatic goal, no nested if statement, no else, based on 4th practice if - 4 times

Practice to win - have some strategy - 
1. Using nested while loops twice, 8 out of 10, no bug with excellent code. 
2. Use if, and else if 3 times, 3 out of 10, easy to create bug, hard to argue
3. Use all if, no else, no nested if, 10 out of 10. In future practices, figure out!

Google search and find some related topic:
if/else statement - 
1. http://programmers.stackexchange.com/questions/206816/clarification-of-avoid-if-else-advice

No comments:

Post a Comment