Archive for 8월, 2009

Insert increasing sequence of numbers in Emacs

8월 26th, 2009

When coding in Emacs, I needed to insert an increasing sequence of integers as follows.

  1.  pdriver_object->MajorFunction[0] = NULL;
  2.  pdriver_object->MajorFunction[1] = NULL;
  3.  pdriver_object->MajorFunction[2] = NULL;
  4.  pdriver_object->MajorFunction[3] = NULL;
  5.  pdriver_object->MajorFunction[4] = NULL;
  6.  pdriver_object->MajorFunction[5] = NULL;

It’s easy to copy and paste the lines. Inserting a sequence of integers was the problem and I wrote a small Emacs Lisp code for that.

  1. (defun f (n)
  2.  (let ((ns (number-to-string n)))
  3.   (if (<= n 30)
  4.     (progn
  5.      (insert ns)
  6.      (next-line)
  7.      (backward-char (length ns))
  8.      (f (1+ n))))))
  9. (f 30)

This was my first practical elisp code. It required more time than typing in numbers, it’s very satisfactory. :)

Let’s Make A Deal! Programming Praxis

8월 5th, 2009

Let’s Make A Deal! Programming Praxis.

Monty Hall 문제로 잘 알려져 있는 확률 문제를 실제로 시뮬레이션 해보는 프로그램.

아래는 OCaml 코드

  1. open Random;;
  2. open Array;;
  3. open Printf;;
  4.  
  5. let rec shuffle arr n =
  6.  if n = 0 then arr
  7.  else begin
  8.   let a = Random.int (length arr) in
  9.   let b = Random.int (length arr) in
  10.   let va = Array.get arr a in
  11.   Array.set arr a (Array.get arr b);
  12.   Array.set arr b va;
  13.   shuffle arr (n-1)
  14.  end
  15.  
  16. exception Found
  17. let find_unchosen_goat arr choice =
  18.  let ug = ref 0 in
  19.  try
  20.   Array.iteri
  21.    (fun i _ ->
  22.     if i != choice && Array.get arr i != 1 then begin ug := i; raise Found end
  23.     else ug := i)
  24.    arr;
  25.   0
  26.  with _ -> !ug
  27.  
  28. let find_change_choice arr old_choice unchosen_goat =
  29.  let cc = ref 0 in
  30.  try
  31.   Array.iteri
  32.    (fun i _ ->
  33.     if i != old_choice && i != unchosen_goat then begin cc := i; raise Found end
  34.     else cc := i)
  35.    arr;
  36.   0
  37.  with _ -> !cc
  38.  
  39. let check_choice arr choice = Array.get arr choice = 1
  40.  
  41. let deal change =
  42.  let doors = shuffle [|0;0;1;|] 10 in
  43.  let choice = Random.int 3 in
  44.  let unchosen_goat = find_unchosen_goat doors choice in
  45.  let last_choice =
  46.   if change then find_change_choice doors choice unchosen_goat
  47.   else choice
  48.  in
  49.  check_choice doors last_choice
  50.  
  51. (** Written by myself – simulate the whole game *)
  52. let simulate ngames =
  53.  let switch_game_result = Array.map (fun _ -> deal true) (Array.init ngames (fun i -> i)) in
  54.  let switch_wins = Array.fold_left (fun wins result -> if result then wins+1 else wins) 0 switch_game_result in
  55.  let stay_game_result = Array.map (fun _ -> deal false) (Array.init ngames (fun i -> i)) in
  56.  let stay_wins = Array.fold_left (fun wins result -> if result then wins+1 else wins) 0 stay_game_result in
  57.  Printf.printf "Winning rate for switch choice : %f\n" ((float_of_int switch_wins) /. (|>float_of_int ngames));
  58.  Printf.printf "Winning rate for stay choice : %f\n" ((float_of_int stay_wins) /. (|>float_of_int ngames))
  59.  
  60. (** Translation of suggested solution – simplified simulation*)
  61. let monty n =
  62.  Random.self_init ();
  63.  let rec monty n switch stay =
  64.   let auto,pick = Random.int 3, Random.int 3 in
  65.   if n = 0 then (switch,stay)
  66.   else
  67.    if auto = pick then monty (n-1) switch (stay+1)
  68.    else monty (n-1) (switch+1) stay
  69.  in
  70.  monty n 0 0
  71.  
  72. (** More simplified *)
  73. let monty2 n =
  74.  Random.self_init ();
  75.  let rec monty2 n switch =
  76.   let pick = Random.int 3 in
  77.   if n = 0 then switch
  78.   else
  79.    if pick = 0 then monty2 (n-1) switch
  80.    else monty2 (n-1) (switch+1)
  81.  in
  82.  monty2 n 0

결과를 보면 선택을 바꾼 경우 이길 확률이 2/3가 된다는 것을 볼 수 있다.

# monty2 1000;;
- : int = 674
# monty2 10000;;
- : int = 6657
# monty2 100000;;
- : int = 66647