Saturday, March 19, 2016

HackerRank: Two string - thinking in JavaScript over 10 ways

March 19, 2016

Introduction 



 A new drill to learn JavaScript is not to read a book, instead of thinking in JavaScript and study how people use the language to solve a simple string algorithm.

  It is the excellence learning experience to read JavaScript 14 submissions.

  First step is to go over each solution, and learn from others how to solve problem using JavaScript.

  Julia used to spend months to read definitive JavaScript guide book, but she noticed that learning was slow and less motivated. She likes to learn JavaScript in much more fun ways.

Problem Statement link is here.


Over 10 JavaScript Solutions



 JavaScript solutions she chooses to read, the submission link filtered by JavaScript language is here.


Solution 1:

study code 1 is here.


Review Array.shift function, link on w3schools.com is here

JavaScript Array.splice method on w3schools.com is here

indexof is here

process.stdin - try to figure out what it is on stackoverflow.com. 

The link is here - how to work with process stdin-on

The link is here - how to detect an empty stdin stream 


Solution 2:

Study code 2 is here. 



Solution 3:

https://gist.github.com/jianminchen/25edcfe155c8859a922c

(continued on March 21, 2016)

Solution 4:

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

Solution 5:

Structured code, using prototype, simulated class function, new/ this etc., two sliding pointers, make me laugh

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

read one more article to entertain the idea of using new/ this in JavaScript:

http://stackoverflow.com/questions/5224295/javascript-the-good-parts-how-to-not-use-new-at-all

Solution 6: 

JavaScript, things I like: using j== first.length to determine YES/ NO, the big problem of coding - repeat same code in if/ else code.
https://gist.github.com/jianminchen/8899cb7587b0389e6ff6

line 6 - line 26 - Julia likes to swap two strings to ensure str1.length <  str2.length

Solution 7: 

swap two string if need. 
https://gist.github.com/jianminchen/10f5cfa5c1ab22070922           

Solution 8: 

write a function to add unique chars into the array - Julia likes the function. 

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

object literal {} - act like a hashmap
search function time complexity O(N^2) - not efficient

A discussion on object literal -
http://stackoverflow.com/questions/17486854/how-to-create-a-method-in-object-literal-notation

Solution 9:

use jagged array - good practice
https://gist.github.com/jianminchen/8dca6d1fd8fe96b7323c

Solution 10: 

using object literal - creating a hashtable, and then, first string goes into as Hashtable - add it if not in; second string, add one on existing one.
https://gist.github.com/jianminchen/ee9fbf9612c8ada8ccae
code can be refactored, early return line 23,  not wait until on line 30.

Solution 11:

Array.prototype.ForEach
https://gist.github.com/jianminchen/69119913341c9bea8b0c
https://gist.github.com/jianminchen/bfe92a2556aab268d250

read this blog to understand the function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
https://msdn.microsoft.com/en-us/library/ff679980(v=vs.94).aspx

still concern about value, index, array arguments in ForEach function.

spent 10 minutes to go over the topic:
http://neversaw.us/2011/01/16/not-your-fathers-javascript/

Good article, stopped here: (continue later)

http://neversaw.us/2011/01/16/not-your-fathers-javascript/#binding-is-great

Array iteration:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Iteration_methods (20 minutes reading)

Too much detail, just 5 minutes reading:

http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/
http://www.less-broken.com/blog/2010/12/lightweight-javascript-dictionaries.html

Solution 12:

Using string to contain 26 characters, then look up each one in two string, use Array.indexOf method

Study code is here

Solution 13:

Use RegExp expression

Study code using RegExp is here.


Solution 14:

Use Binary search

Code submission is here.


Statistics: Hour spent: 8 hours

Follow up 


April 4, 2016

Julia needs to warm up JavaScript every week, she likes the drill; After 8 hours practice using the drill, she noticed that the code she wrote before was so bad, immediately, she spent 2 days to rewrite some of them, in middle of March.

No comments:

Post a Comment