Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, March 25, 2016

HackerRank: Two string - thinking in Java

March 25, 2016

Read other people's ideas. Understand other people by reading their code. Julia likes to read some Java programming language code for 1-2 hours, she came cross people's code, amazed by ideas from people working in Facebook, Amazon, and amazed that people have GOLD prize on HackerRank.

Julia likes to be able to write very readable, most understandable code with shortest time as possible.

problem statement:

https://www.hackerrank.com/challenges/two-strings

Here are a few Java solution she likes:

Solution 1:
https://gist.github.com/jianminchen/6f029bb909ed7d85c012

Solution 2:
source code reference: 
https://www.hackerrank.com/winger

People like to use bit operation, most likely they are expert and then have some Gold in HackerRank. 

bit operation
https://gist.github.com/jianminchen/3d2acf64725cd73c79db


use one integer to store 26 characters, 1 bit one char from a to z.

private static int f(String a) {
        int r = 0;
        for (char c : a.toCharArray()) {
            r |= 1 << (c - 'a');    // Julia's comment: 1 left shift (c-'a') times 
        }
        return r;
    }

Solution 3:

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

Solution 4:

It is interesting to read InputStream, need to warm up on Java class

https://gist.github.com/jianminchen/21ffc64093fd5952647c

Solution 5:
use HashSet, Set, Character, String classes
https://gist.github.com/jianminchen/1eb02a940f7fa4b6a3f7


Thursday, December 17, 2015

Book Reading: Effective Java

Dec. 17, 2015

Effective Java is such a great book, I had wonderful time to read 1-2 hours while enjoying the travel on train back to my home  town this October, 2015.

But, Julia had to motivate her more to complete reading the book - at least spend over 10 hours this holiday break to catch up some best ideas about programming.

Dec. 17, 2015  (20 minutes reading)
Item 52: Refer to objects by their interfaces (julia's rating 1-10: 10)

Dec. 18, 2015 ( 2 hours reading 8:00pm-10:00pm)
Item 23: Don't use raw types in new code
Item 24: Eliminate unchecked warnings
Item 25: Prefer lists to arrays
Item 40: Design method signatures carefully
Item 45: Minimize the scope of local variables
Item 46: Prefer for-each loops to traditional for loops

Dec. 19, 2015 ( 2 hours reading 8:30am-10:30am)

Item 6: Eliminate obsolete object reference (read the item twice)
Actions: take sample code, and think about similar example, write a one in C#, check in github
Notes:
3 cases for memory leaks:
1. whenever a class manages its own memory,
2. caches  - WeakHashMap
3. listeners and other callbacks
Careful code inspection or with the aid of a debugging tool known as a heap profiler
Action item 2: Try heap profiler in C#

Item 8: Obey the general contract when overriding equals (read again, 30 minutes)

Further reading:
WeakHashMap
http://www.ibm.com/developerworks/library/j-jtp11225/

Read some code, and then, go back to the book.
http://www.codeproject.com/Articles/595160/Understand-Liskov-Substitution-Principle-LSP

http://www.codeproject.com/Articles/648987/Violating-Liskov-Substitution-Principle-LSP

http://www.codeproject.com/Articles/597870/Liskov-Substitution



Friday, September 25, 2015

Study time - watch videos, and read articles

10/18/2015

REST+JSON API Design - Best Practices for Developers

https://www.youtube.com/watch?v=hdSrT4yjS1g

How To Design A Good API and Why it Matters (Julia's rating: A+)
https://www.youtube.com/watch?v=aAb7hSCtvGw

-- more notes: How To Design A Good API and Why it Matters
Watched again on 10/21/2015, notes taken to share:
54:40/1:00:18
Use Appropriate Parameter and Return Types
. Favor interface types over classes for input
- provides flexibility, performance
. Use most specific possible input parameter type
- Moves error from runtime to compile time (Julia's comment: Great tip, reduce time on debugging, fix things in compiling time!)
. Don't use string if a better type exists
- Strings are cumbersome, error-prone, and slow
. Don't use floating point for monetary values
- Binary floating point causes inexact results!
- Use double (64 bits) ranther than float (32bits)
-Precision loss is real, performance loss negligible

watched again on 10/21/2015, notes take to share:
49:19/1:00:18
Don't violate the Principle of Least Astonishment

Read more on this article:
http://programmers.stackexchange.com/questions/187457/what-is-the-principle-of-least-astonishment

-- end of notes for

Josh Bloch, Lord of the APIs - A Brief, Opinionated History of the API
https://www.youtube.com/watch?v=ege-kub1qtk

https://www.youtube.com/watch?v=4YxnxmQS41s

9/27/2015
SQL injection Myths & Fallacies: Best practices of defense

https://www.youtube.com/watch?v=o4dJ7hdA8fs

Quickly go over the book "SQL antipatterns" in short future.

9/24/2015

Effective Java - Still Effective After All These Years

Julia’s favorite time: first 15 minutes with two great coding examples; watch again later.


9/25/2015

Back to my life without writing code, enjoy the videos to catch up things about internet and technology, my favorite topic in the video: google CEO comment about competition, company vs. government, what the differences, How CEO thinks. 

Eric Schmidt & Jared Cohen: The impact of Internet and Technology


https://www.youtube.com/watch?v=OwJTCHxjcjQ

Get curious about .net framework - work on .NET framework and enjoy benefit last 5 years; should spend 10 hours to watch the .NET framework videos. Will spend more time when I take vacation from Oct. 2 - Oct. 16, 2015

https://www.youtube.com/watch?v=ywN0vTFJNcw&list=PLnrS3jsiKkdKcChakQPJi1XdKyDt_2jPa

9/28/2015
first time using search in github, and seach Sudoku, and find some code to read:

https://github.com/dartist/sudoku_solver/blob/master/benchmark/sudoku.cs

http://norvig.com/sudoku.html

http://aspadvice.com/blogs/rbirkby/archive/2007/08/23/Silverlight-Sudoku-with-LINQ.aspx