2017年11月1日 星期三

【JavaScript】google-code-prettify 的 run_prettify.js 主題樣式

GitHub:[https://github.com/google/code-prettify](https://github.com/google/code-prettify)
展示網址:[Gallery of themes for code prettify](https://rawgit.com/google/code-prettify/master/styles/index.html) ```html ``` ```html ``` ```html ``` ```html ```

2017年5月11日 星期四

【PHP】X-Frame-Options 回應標頭

這幾天使用 Checkmarx 做白箱弱掃時,Javascript 的部份一直出現一個中度風險的項目:

【Client Cross Frame Scripting Attack】

Google 了一下似乎在 PHP 內的 header 加上 X-Frame-Options 相關設定就能解決此問題

在 PHP 頁面中各別設定:

完全禁止任何 iframe 請求
```php header("X-Frame-Options: DENY") ``` 只允許同 Domain 來源的請求
```php header("X-Frame-Options: SAMEORIGIN") ``` 只允許某網址的請求
```php header("X-Frame-Options: ALLOW-FROM http://www.google.com") ```

在 Apache 中設定:

```php Header always append X-Frame-Options DENY ```
雖然此方應該能有效避免【Client Cross Frame Scripting Attack】這類攻擊,
但在 Checkmarx 的白箱弱掃時,似乎還是會誤判為無效,待持續觀察 ...


2017年4月24日 星期一

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

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

主流程: ```html <button type="button" onclick="genBarcode();>超商代收</button> ```

另開視窗頁面: ```html ```

2017年4月6日 星期四

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

markdown 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%
```python 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%
```python 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.

原因是如果參數有兩個名稱重複時,就會造成這個錯誤
```sql select * from student where sid = :id or tid = :id ```

將參數改為不同名稱就能解決這個問題
```sql select * from student where sid = :sid or tid = :tid ```

2017年3月18日 星期六

【Hadoop】Hadoop 相關系列產品簡介

Apache Hadoop:用於處理大數據的分散式開源框架
Apache Hama:基於 BSP 計算技術的框架,可用於圖形、矩陣的大數據計算
Apache HBase:分散式 NoSQL 資料庫系統
Apache HCatalog:Hadoop 的資料表和儲存管理層,提供資料的關聯檢視
Apache Hive:HDFS 上面的分散式資料倉儲系統
Apache Mahout:機器學習開源框架
Apache Oozie:用於 Hadoop 的工作流程調度引擎
Apache Sqoop:結構化數據與 Hadoop 之間的資料轉換工具
Apache Pig:Hadoop 的大數據分析平台
Apache Whirr:運行於雲端服務的類別庫
Apache Zookeeper:提供開源的分散式配置服務、同步服務、協調服務
Apache Ambari:提供 Web UI 和 REST API,可簡化 Hadoop 叢集的管理和監控
Apache Avro:二進位的資料序列化系統
Apache Bigtop:Hadoop 生態系統的開發、打包和測試系統
Apache Cassandra:分散式 NoSQL 資料庫系統
Apache Chukwa:日誌收集、處理、分析系統
Apache Crunch:提供 Java API,能夠簡化撰寫、測試和執行 MapReduce Pipeline 程序
Apache Flume:分散式日誌收集系統
Apache Giraph:可擴展的分散式圖形處理系統

【Spark】Apache Spark 2.0 筆記

Spark 2.0 的特性

更簡單(Easier: ANSI SQL and Streamlined APIs)

      
  • Unifying DataFrames and Datasets in Scala/Java
  •   
  • SparkSession
  •   
  • Simpler, more performant Accumulator API
  •   
  • DataFrame-based Machine Learning API emerges as the primary ML API
  •   
  • Machine learning pipeline persistence
  •   
  • Distributed algorithms in R
  •   
  • User-defined functions (UDFs) in R

更快速(Faster: Apache Spark as a Compiler)

      
  • 搭載了第二代 Tungsten 引擎,此技術官方稱為「whole-stage code generation」

更聰明(Smarter: Structured Streaming)

      
  • Integrated API with batch jobs
  •   
  • Transactional interaction with storage systems
  •   
  • Rich integration with the rest of Spark

Spark 2.0 官方介紹影片:SPARK EAST SUMMIT in New York(2016/02/16 )

版本釋出記錄:

Spark 2.2.0(2017/07/11)
Spark 2.1.1(2017/05/02)
Spark 2.1.0(2016/12/28)
Spark 2.0.2(2016/11/14)
Spark 2.0.1(2016/10/03)
Spark 2.0.0(2016/07/26)

【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%
```python 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年3月3日 星期五

【Python】Codility in Python : Lesson 11 - Sieve of Eratosthenes【CountSemiprimes】

Sieve of Eratosthenes 第一題:【CountSemiprimes】
Count the semiprime numbers in the given range [a..b]

A prime is a positive integer X that has exactly two distinct divisors: 1 and X.
The first few prime integers are 2, 3, 5, 7, 11 and 13.

A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers.
The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

You are given two non-empty zero-indexed arrays P and Q, each consisting of M integers.
These arrays represent queries about the number of semiprimes within specified ranges.

Query K requires you to find the number of semiprimes within the range (P[K], Q[K]),
where 1 ≤ P[K] ≤ Q[K] ≤ N.

For example, consider an integer N = 26 and arrays P, Q such that:

  P[0] = 1 Q[0] = 26
  P[1] = 4 Q[1] = 10
  P[2] = 16 Q[2] = 20

The number of semiprimes within each of these ranges is as follows:

(1, 26) is 10,
(4, 10) is 4,
(16, 20) is 0.

Write a function:

  def solution(N, P, Q)

that, given an integer N and two non-empty zero-indexed arrays P and Q consisting of M integers,
returns an array consisting of M elements specifying the consecutive answers to all the queries.

For example, given an integer N = 26 and arrays P, Q such that:

  P[0] = 1 Q[0] = 26
  P[1] = 4 Q[1] = 10
  P[2] = 16 Q[2] = 20

the function should return the values [10, 4, 0], as explained above.

Assume that:
  N is an integer within the range [1..50,000];
  M is an integer within the range [1..30,000];
  each element of arrays P, Q is an integer within the range [1..N];
  P[i] ≤ Q[i].


方法一:用半質數相乘取出結果
Correctness:100%、Performance:40%
```python from math import sqrt def solution(N, P, Q): S = [0] * (N+1) # 先取得半質數列表 prime = [ p for p in range(2, int(N / 2) + 1) if 0 not in [ p % d for d in range(2, int(sqrt(p))+1)] ]   # 標註所有相乘結果 for i in prime: for j in prime: k = i * j if k <= N: S[k] = 1 # 取得範圍內有多少註記 result = [] for i in range(0, len(P)): result.append(len(list(filter(lambda x: x == 1, S[P[i]:Q[i]+1])))) return result ```

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