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

Leetcode 278 - binary search algorithm and discussion

August 19, 2016

Leetcode 278 - binary search algorithm and discussion

1. http://blog.csdn.net/ebowtang/article/details/50735869

A. Good discussion on the deadloop prevention - make sure that +1 or -1 somewhere
B. discussion of ?

Good discussion of tips:
1. base case
2. overflow issue
http://www.cnblogs.com/airwindow/p/4791234.html

Wednesday, August 17, 2016

Become a Full-stack .NET Developer - pluralsight.com

August 17, 2016

Start to work on this 3 hours course on pluralsight.com.

Start from beginner level, enjoy the course. Take some notes.

Related course:
http://juliachencoding.blogspot.ca/2016/08/become-full-stack-net-developer.html


Leetcode workout:

1. Leetcode 278 - binary search algorithm and discussion

1. http://blog.csdn.net/ebowtang/article/details/50735869

A. Good discussion on the deadloop prevention - make sure that +1 or -1 somewhere
B. discussion of

Good discussion of tips:
1. base case
2. overflow issue
http://www.cnblogs.com/airwindow/p/4791234.html

Leetcode 125
1. study the code:
http://blog.csdn.net/DERRANTCM/article/details/47651267

Leetcode 125 - write code based on this version

short function name - isalnum - very good name

http://blog.csdn.net/nomasp/article/details/50623165

5 practices - interest journey !

1st practice: failed static analysis - miss a bug - easy to spot --, not++

https://gist.github.com/jianminchen/ed12d3004dbe85815f41fef047d48823

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:
https://gist.github.com/jianminchen/1e1724c79805d2f40195f08135ec4d02

highlights of practice 2:
1. Fail to pass the online judge
2. line 24 - 27, logic and reasoning has flaws
both are ok
both are failed
A is failed
B is failed
Those are 4 cases - in the specific order as well.

3rd practice:
https://gist.github.com/jianminchen/8f9ae3839499718790180aa58965d3de

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:

https://gist.github.com/jianminchen/c6a4318a06aa271de834e1e9c1afa7e4

break else if statement, make it more flat - line 60 - 65, only two if, avoid " else if" in 3rd practice.

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
https://gist.github.com/jianminchen/71421f776948086fd653e441725b3732

3. Hackthon - programming etc.

http://blog.csdn.net/DERRANTCM/article/category/6247554

http://blog.csdn.net/derrantcm/article/details/51512284

4. Review palindrom blog

JavaScript Style Guide Study - Airbnb

August 17, 2016

Plan to spend 3+ hours to go over the JavaScript Style Guide. 

1. Airbnb JavaScript Style Guide()  - English Version 

https://github.com/airbnb/javascript

2. Airbnb JavaScript Style Guide() - Chinese version

https://github.com/yuche/javascript#table-of-contents

36 Topics

Types
References
Objects
Arrays
Destructuring

Strings
Functions
Arrow Functions
Classes & Constructors
Modules


Iterators and Generators (NO. 11)
Properties
Variables
Hoisting
comparison Operators & Equality

Blocks
Comments
Whitespace
commas
Semicolons


Type Casting & Coercion (No. 21)
Naming Conventions
Accessors
Events
jQuery

ECMAScript 5 Compatibility
ECMAScript 6 Styles
Testing
Performance
Resources


In the Wild
Translation
The JavaScript Style Guide Guide
Chat With Us about JavaScript
Contributors
License

Reading while going through JavaScript style guide from Airbnb:

1. JavaScript spread syntax...  - 10 minutes

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator


2. Read JavaScript Array.From - 20 minutes

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from

3. Destructuring assignment  10 minutes

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment


4. Template literals - 10 minutes reading

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

new terms: back-tick(``), neither double nor single quotes


5. Default parameters -
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

Editorial notes:
1. Write down all new terms in the guide. Understand the terms.
2. Try to memorize all the guide. Design some drills to help.
3. Invest time first to read the good style code, and then, start to write more JavaScript code.
4. Focus on the document, understand why? What to avoid? Good or bad or Excellent idea?

Favorite blogs of day:
web front technologies 
1. https://github.com/qiu-deqing/FE-learning

Tuesday, August 16, 2016

Leetcode 380 - Insert, delete, getRandom() O(1) time - Practice 4

August 16, 2016

The first 3 practices:


http://juliachencoding.blogspot.ca/2016/08/leetcode-380-insert-delete-getrandom-o1.html

http://juliachencoding.blogspot.ca/2016/08/leetcode-380-insert-delete-getrandom-o1_16.html

http://juliachencoding.blogspot.ca/2016/08/leetcode-380-insert-delete-getrandom-o1_89.html

Read the blog:
http://www.guoting.org/leetcode/leetcode-380-insert-delete-getrandom-o1/

Java code from the above blog:
https://gist.github.com/jianminchen/9561828feda21bac20bc9ba4da0d13c8

4th practice using C#:

https://gist.github.com/jianminchen/7741fabf57413ccbb1080f6f691bb532

Highlights of 4th practice:

1. Thinking problem solving using an array:

 Insertion of array is to append the number at the end of array, O(1);

 Delete a number from the array, for example, at index position i of array with length n, then, all elements from i+1 to n-1 should be shifted to left one position - time complexity is O(n-i) = O(n)

 getRandom() - time complexity is O(1), just get a random number r from 0 to n, and then, return arr[r].
   Array value can be any integer number, and in the range of Int32.min - Int32.max, so given an integer value, it may not be in the array; if it is, to find the index of array to hold the value, we need to look up a hashmap with value/ index of array pair first. 

So, in order to make it O(1) for the deletion, we can use extra space to speed up time to O(1). The idea is to move last number in the array to the position of deleted element.

Extra space - a hashmap to store each number in the array as key, and the value is the position in the array.

4. Introduce new design in the practice? 

arrayDeletionO1 - line 150 API - array deletion, arrayDeletionO1 (Dictionary<Int32, Int32> arr, Dictionary<Int32, Int32> map, int val)

- implement the array deletion in time complexity O(1)

move - line 173 API - array move - move an entry from the dictionary from one location to another location. move(Dictionary<Int32, Int32> dict, int from, int to)

mapUpdateForArrayDeletion - line 129 - hashmap update - mapUpdateForArrayDeletion(Dictionary<Int32, Int32> arr, Dictionary<Int32, Int32> map, int val)

Cons and pros for modified deletion for the array-like data structure:
1. Cons: If the array is sorted, then the order is lost. No longer in the order.
2. Study Array API for C, C++, C#, JavaScript, none of them has API like this. Why?

Array - JavaScript
http://juliachencoding.blogspot.ca/2016/07/javascript-array-mozilla-60-minutes.html

C# array
http://juliachencoding.blogspot.ca/2016/06/array-class-c-c-javascript-java.html

3. For the deletion, update can be done directly instead of first deletion, and then move of the last one to the deleted item's position. line 182, line 183 can be merged to one line - update.

4th practice using C#:
https://gist.github.com/jianminchen/7741fabf57413ccbb1080f6f691bb532

4. JavaScript array is sparse, the array is not contiguous. For Leetcode 380, Julia thought about array deletion design, if the array is leaving as sparse after deletion, then, getRandom() can not be O(1). If the sparse slot is selected by random algorithm, need to select another one.

Related articles:
 1. Uber map article:
http://goo.gl/nmRv18

Google search keyword:
red-black tree - C++ implements the map
http://stackoverflow.com/questions/5288320/why-is-stdmap-implemented-as-a-red-black-tree

2. Uber algorithm question: 
https://goo.gl/fWq132

Leetcode 380 - Insert, delete, getRandom() O(1) time - Practice 3

August 16, 2016

After first 2 practices, Julia read the blog:
http://www.guoting.org/leetcode/leetcode-380-insert-delete-getrandom-o1/


3rd practice using C#:

https://gist.github.com/jianminchen/9a8aef6220d285aac3f22abac984e0b3


Leetcode 380 - Insert, delete, getRandom() O(1) time - Practice 2

August 16, 2016

 First practice on Leetcode 380:

http://juliachencoding.blogspot.ca/2016/08/leetcode-380-insert-delete-getrandom-o1.html

 Add one more function - design API - Julia likes to design API, after she tried to memorize all APIs of JavaScript array.

Second practice - one function:

https://gist.github.com/jianminchen/84a0945eb49d3ae7af36b4ca5f0f0c41

Design API - move an entry from the dictionary from one location
to another location
assume that dictionary contains the entry of from
assume that to position is available in the dictionary


public bool move(Dictionary<Int32, Int32> dict, int from, int to)

Blog:
1. CSS style -
http://codeguide.bootcss.com/

2. http://www.ruanyifeng.com/blog/2012/10/javascript_module.html

Leetcode 380 - Insert, delete, getRandom() O(1) time - Practice 1

August 8, 2016

Problem statement:
Design a data structure that supports all following operations in average O(1) time.
  1. insert(val): Inserts an item val to the set if not already present.
  2. remove(val): Removes an item val from the set if present.
  3. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
Go over the website Java code, write a C# version: 
http://www.programcreek.com/2014/08/leetcode-insert-delete-getrandom-o1-java/

Java code from the above webpage:
https://gist.github.com/jianminchen/b6ab09a8a047db42da4ad663036d621a

C# code written by Julia
https://gist.github.com/jianminchen/9ebfbe7462c17689344578be31c804c0

Highlights of practice:

1. Spent 20+ minutes to write the function remove(int val)
2. Put test cases into main function, test the code to understand the problem.
3. Did not understand the design, why two hashmaps are used.
4. Concern about function remove - line 133 - 165
    The function is not readable - one function does so many things - break Single Responsibility Principle.








Sunday, August 14, 2016

Microsoft Development Service - watch and learn

August 14, 2016

 Great time to watch and learn. First thing in the Sunday morning is to relax and learn.

Blogs to read:
http://stories.visualstudio.com/bing-continuous-delivery/

Videos:

1. Full Keynote: The Future of Microsoft Tools and Services for the New Role of Developers
https://www.youtube.com/watch?v=Gks7Ngzt5Yw

2. Keynote: Visual Studio 2015 - Any App, Any Developer
https://www.youtube.com/watch?v=Nj1luMpj7mI

Blogs to read:

1. Leetcode 380: (code study: Leetcode blogs)
http://www.guoting.org/leetcode/leetcode-380-insert-delete-getrandom-o1/

2. Leetcode 380 - Insert Delete GetRandom O(1)
https://leetcode.com/problems/insert-delete-getrandom-o1/

3. Read the article
http://www.programcreek.com/2014/08/leetcode-insert-delete-getrandom-o1-java/

4. Read Uber map article:
http://goo.gl/nmRv18

Google search keyword:
red-black tree - C++ implements the map

2.
https://goo.gl/fWq132