My Blog List

Friday, September 11, 2015

bind, call, apply self-testing

name = "super-man";
var obj = {
  name :"mart-man",

  // general  hello : function(){
    console.log("hello  : " + this.name);
      this.test(function(){
        console.log("hello > callback : " + this.name);
      });
  },

  // bind  hello_bind : function(){
    console.log("hello_bind : " + this.name);
    this.test(function(one, two){
      console.log("hello_bind > callback : " + this.name);
    }.bind(this));
  },

  test : function(callback){
    callback();
  }
}

test = function(){
  console.log("out test");
}

console.log(1, obj.name);
console.log(2, name);
console.log(3);
obj.hello();
console.log(3.5);
var hello_outer = obj.hello;
hello_outer();
console.log(4);
obj.hello_bind();
console.log(5);
obj.hello.call(this);
obj.hello.apply({name:'kings-man', test:function(){console.log("third test")}});
console.log('end');


If you don't understand well why this logs printed.
I suguess this website and http://javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/

study it~!


1 "mart-man"
2 "super-man"
3
hello  : mart-man
hello > callback : super-man
3.5
hello  : super-man
out test
4
hello_bind : mart-man
hello_bind > callback : mart-man
5
hello  : super-man
out test
hello  : kings-man
third test
end




Wednesday, September 9, 2015

project euler 320 problem

https://projecteuler.net/problem=230



1. A
2. B
3. AB
4. BAB
5. ABBAB
6. BABABBAB
.......

At first, I tired to find some repeated pattern.  between A And B.
Maybe I took one hour to find it.
but the result was failed. there is no pattern and had not to think about findind pattern.
Finally I thought this way.

A :
14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679

B:
82148086513282306647093844609550582231725359408128
48111745028410270193852110555964462294895493038196


Fibonaci (n) = Fibonaci(n-2) + Fibonaci(n-1);

so make array to put length of F(n);

arr[1] = 100;
arr[2] = 100;
arr[3] = 200;
arr[4] = 300;
arr[5] = 500;

if i want to find 320 I should find from arr[5]
because 500 covered 320 index word.
and find 20 position from arr[4];
arr[4] is made of arr[2] + arr[3]
next find 20 position of arr[2]

if the index of arr[] is 1 or 2, the answer is arr[1 Or to].charIndex(20);

My java code is below.
(Actually It is dirty code, need to be refactored)



import java.math.BigInteger;
import java.util.ArrayList;

/**
 * 처음에는 BB가 중복되서 나오는 숫자의 일정한 패턴이 있는줄 알았다.
 * 알고보니 일정한 패턴은 없네..
 * 
 * 그래서 접근하는 방법은 역 Search 방법이다.
 * @author ddavid
 *
 */
public class number230 {
 
 public static String first ="1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
 public static String second ="8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196";
 
 public static BigInteger total = BigInteger.ZERO;
 
 public static void main(String args[]){

  
  long start= System.currentTimeMillis();
  real();
  
  System.out.println(System.currentTimeMillis()-start);
  
 }
 
 public static void DAB(long num){
  String subFirst = "";
  String subSecond = "";
  subFirst = first;
  subSecond = second;
  long val =  num;
  System.out.println("val : " + val);
  
  
  for (int k = 0; k <15; k++) {
  
   String temp =subFirst+subSecond;
   
   subFirst = subSecond;
   subSecond = temp;
   
   if(subSecond.length() > val){
    System.out.println(subSecond.charAt((int)val-1));
    break;
   }
  }
 }
 
 public static void realDAB(long num){
  long val =  num;  // 81420679895522450
//  long val =  35;  // 81420679895522450
  
  System.out.println("val  : " + val);
  ArrayList<Long> cntList  = new ArrayList<Long>();
  cntList.add((long)first.length());
  cntList.add((long)first.length());
  while (true) {
   long two = cntList.get(cntList.size()-1);
   long one = cntList.get(cntList.size()-2);
   
   long sum = one+two;
   cntList.add(sum);
   if(sum > val){
    break;
   }
  }
  find(cntList.size()-1, val, cntList, 0);
 }
 
 public static void real(){
  for (int i = 0; i < 18; i++) {
   long val =  (long)(127+19*i)* (long)Math.pow(7, i);  // 81420679895522450
//   long val =  35;  // 81420679895522450
   
   System.out.println("val  : " + val);
   ArrayList<Long> cntList  = new ArrayList<Long>();
   cntList.add((long)first.length());
   cntList.add((long)first.length());
   while (true) {
    long two = cntList.get(cntList.size()-1);
    long one = cntList.get(cntList.size()-2);
    
    long sum = one+two;
    cntList.add(sum);
    if(sum > val){
     break;
    }
   }
   find(cntList.size()-1, val, cntList, i);
  }
  System.out.println(total.toString());
 }
 
 public static void find(int index, long position, ArrayList<Long> cntList, int i){
  if(index == 1 || index ==0){
   System.out.println("index : " + index + "  position : " + position );
   
   if(index == 0){
    System.out.println(first.charAt((int)position -1));
    total= total .add(BigInteger.valueOf((long)Math.pow(10, i)).multiply(BigInteger.valueOf(Long.valueOf(first.charAt((int)position -1) +""))) );
   }else{
    System.out.println(Long.valueOf(second.charAt((int)position -1) +""));
    total= total .add(BigInteger.valueOf((long)Math.pow(10, i)).multiply(BigInteger.valueOf(Long.valueOf(second.charAt((int)position -1) +""))) );
   }
   return;
  }
  if(position <= cntList.get(index -2)){   // one 위치인경우 
   find(index-2,  position  , cntList, i);
  }else{ // last 위치인경우
   find(index-1, position - cntList.get(index -2), cntList, i);
  }
 }
}

Nodejs Mapper + transaction Opensource 진행과정3

github에서 opensource 를 작업한다고 시작한지 꽤 시간이 지난것 같다.

어쩌다 보니 최초 계획한대로 정리가 된것 같은데.. 오늘은 
query 부분에  Array 가 동적 바인딩이 되도록 진행했다.

처음 코딩을할때 널체크만 생각하고 query 조건들을 파싱하는구조로 잡았었는데
생각이 짧았었다 확장에서 있어 정말 유지보수가 힘든 구조였다.

재귀 방법으로 지금 다시 고민을 하고 있다.


앞으로 해야하는 작업이 조금더 더 깔끔하게 Queue들을 날릴수 없나에 대해 고민해보고
js 테스트 도구 등을 사용해서 단위 테스를 진행하도록 해야겠다.

그리고, 재귀 호출을 통해서 depth의 depth도 체크하는 구조를 만들어야 겠다.

지금 라이브러리로 사이트를 운영중이라서 사용해서는 안된 말은 아니다.
음..