2017年4月24日 星期一

【Javascript】兩視窗資料傳遞的方法

這幾天遇到一個需求,必須另開視窗產生完超商代收的 barcode 資訊,
再回傳相關資訊給原本視窗,接下來再 POST 所有資訊到下一頁,
在這邊記錄一下問題的大致做法。

主流程:

<button type="button" onclick="genBarcode();>超商代收</button>
<script>
  function genBarcode() {
    var frmPay = document.getElementById("payInfo");
    frmPay.target = "POPUP";
    frmPay.action = "barcode.jsp";

    var popup = window.open("", "POPUP", "height=300,width=400");
    if (popup) {
      frmPay.submit();
    }
  }

  function nextStep(barcode) {
    var frmPay = document.getElementById("payInfo");
    frmPay.target = "";
    frmPay.action = 'nextStep.php';

    field = document.createElement('input');
    field.type = 'text';
    field.name = 'barcode';
    field.value = barcode;
    frmPay.appendChild(field);
    frmPay.submit();
  }
</script>

另開視窗頁面:

<script>
  var barcode = Math.random();
  document.getElementById("barcode").value = barcode;
  window.opener.nextStep(barcode);
</script>

2017年4月6日 星期四

【Python】Codility in Python : Lesson 13 - Fibonacci Numbers【Ladder】

Fibonacci Numbers 第一題:【Ladder】
Count the number of different ways of climbing to the top of a ladder.


You have to climb up a ladder. The ladder has exactly N rungs, numbered from 1 to N.
With each step, you can ascend by one or two rungs. More precisely:

with your first step you can stand on rung 1 or 2,
if you are on rung K, you can move to rungs K + 1 or K + 2,
finally you have to stand on rung N.
Your task is to count the number of different ways of climbing to the top of the ladder.

For example, given N = 4, you have five different ways of climbing, ascending by:

  1, 1, 1 and 1 rung,
  1, 1 and 2 rungs,
  1, 2 and 1 rung,
  2, 1 and 1 rungs, and
  2 and 2 rungs.

Given N = 5, you have eight different ways of climbing, ascending by:

  1, 1, 1, 1 and 1 rung,
  1, 1, 1 and 2 rungs,
  1, 1, 2 and 1 rung,
  1, 2, 1 and 1 rung,
  1, 2 and 2 rungs,
  2, 1, 1 and 1 rungs,
  2, 1 and 2 rungs, and
  2, 2 and 1 rung.

The number of different ways can be very large,
so it is sufficient to return the result modulo 2P, for a given integer P.

Write a function:

  def solution(A, B)

that, given two non-empty zero-indexed arrays A and B of L integers,
returns an array consisting of L integers specifying the consecutive answers;
position I should contain the number of different ways of climbing the ladder with A[I] rungs modulo 2B[I].

For example, given L = 5 and:

  A[0] = 4 B[0] = 3
  A[1] = 4 B[1] = 2
  A[2] = 5 B[2] = 4
  A[3] = 5 B[3] = 3
  A[4] = 1 B[4] = 1

the function should return the sequence [5, 1, 8, 0, 1], as explained above.

Assume that:
  L is an integer within the range [1..30,000];
  each element of array A is an integer within the range [1..L];
  each element of array B is an integer within the range [1..30].


方法一:參考 Code Says 的做法
Correctness:100%、Performance:100%

def solution(A, B):
  limit = len(A)
  result = [0] * len(A)
  B = [(1 << item) - 1 for item in B]

  # 取得費氏數列
  fib = [0] * (limit + 2)
  fib[1] = 1
  for i in range(2, limit + 2):
    fib[i] = fib[i - 1] + fib[i - 2]

  for i in range(limit):
    result[i] = fib[A[i] + 1] & B[i]

  return result

完整練習題 source code 請參閱:github

【Python】Codility in Python : Lesson 12 - Euclidean Algorithm【ChocolatesByNumbers】

Euclidean Algorithm 第一題:【ChocolatesByNumbers】
There are N chocolates in a circle. Count the number of chocolates you will eat.


Two positive integers N and M are given.
Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1.

You start to eat the chocolates. After eating a chocolate you leave only a wrapper.

You begin with eating chocolate number 0.
Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one.

More precisely, if you ate chocolate number X,
then you will next eat the chocolate with number (X + M) modulo N (remainder of division).

You stop eating when you encounter an empty wrapper.

For example, given integers N = 10 and M = 4.
You will eat the following chocolates: 0, 4, 8, 2, 6.

The goal is to count the number of chocolates that you will eat, following the above rules.

Write a function:

  def solution(N, M)

that, given two positive integers N and M, returns the number of chocolates that you will eat.

For example, given integers N = 10 and M = 4. the function should return 5, as explained above.

Assume that:
  N and M are integers within the range [1..1,000,000,000].


方法一:參考 Maurice Dassen 大大的做法
Correctness:100%、Performance:100%

def solution(N, M):
  x = 0
  while x != 1:
    x, y = N, M
    while y != 0:
      x, y = y, x % y
      N, M = N / x, M / x

  return N

完整練習題 source code 請參閱:github

2017年4月5日 星期三

【PHP】解決 SQLSTATE[HY000]: General error: 2031 No data supplied for parameters in prepared statement.

記錄本日工作時遇到的詭異問題,PHP 在撈 DB 資料時出現了下列錯誤訊息:
SQLSTATE[HY000]: General error: 2031 No data supplied for parameters in prepared statement.

原因是如果參數有兩個名稱重複時,就會造成這個錯誤

select * from student where sid = :id or tid = :id

將參數改為不同名稱就能解決這個問題

select * from student where sid = :sid or tid = :tid