2017年2月16日 星期四

【Python】Codility in Python : Lesson 4 - Counting Elements【FrogRiverOne】

Counting Elements 練習題的第三題題目是【FrogRiverOne】
Find the earliest time when a frog can jump to the other side of a river.

方法一:使用 set 的簡易方式
```python def solution(X, A): S = set() count = len(A) for i in range(0, count): S.add(A[i]) if(len(S) == X): return i return -1 ```

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

2017年2月15日 星期三

【Python】Codility in Python : Lesson 4 - Counting Elements【PermCheck】

Counting Elements 練習題的第二題題目是【PermCheck】
Check whether array A is a permutation.

方法一:使用 set 判斷長度是否一致的簡易方式
```python def solutionBySet(A): S = set(A) return 1 if max(S) == len(S) and len(S) == len(A) else 0; ```

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

【Python】Codility in Python : Lesson 4 - Counting Elements【MissingInteger】

Counting Elements 練習題的第一題題目是【MissingInteger】
Find the minimal positive integer not occurring in a given sequence.

方法一:使用 in set 判斷內容值的簡易方式
```python def solutionByInSet(A): result = 1 s = set(A) while result in s: result += 1 return result ```

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

【Python】Codility in Python : Lesson 3 - Time Complexity【TapeEquilibrium】

Time Complexity 練習題的第三題題目是【TapeEquilibrium】
Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.

方法一:使用遞減後取最小值的簡易方式
```python def solution(A): total = sum(A) result = sys.maxsize for i in range(0, len(A)): total -= A[i] * 2 result = min(result, math.fabs(total)) return int(result) ```

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

2017年2月14日 星期二

【Python】Codility in Python : Lesson 3 - Time Complexity【PermMissingElem】

Time Complexity 練習題的第二題題目是【PermMissingElem】
Find the missing element in a given permutation.

方法一:使用 N+1 階三角範圍來取值
```python def solutionByScope(A): total = 0 scope = float((len(A) + 1)) * float((len(A) + 2)) / 2 for value in A: total += value return (int)(scope - total) ```

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

2017年2月13日 星期一

【Python】Codility in Python : Lesson 3 - Time Complexity【FrogJmp】

Time Complexity 練習題的第一題題目是【FrogJmp】
Count minimal number of jumps from position X to Y.

方法一:使用 math.ceil 的簡易方式
```python def solutionByCeil(X, Y, D): return int(math.ceil((Y - X) / float(D))) ```

方法二:使用餘數判斷回傳的結果
```python def solutionByMod(X, Y, D):   return (Y - X) / D if (Y - X) % D == 0 else ((Y - X) / D) + 1 ```

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

【UTF-8】解決 PHP 實作 HTML to Excel,在 ie11 檔名出現亂碼的問題

一般來說,在 PHP 中要把 HTML 中的 TABLE 轉為 EXCEL,
只要加上下列 header 描述,就能自訂匯出的檔案名稱
```php <?php header("Content-Disposition: attachment; filename=" . $fileName . ".xls;); ?> ```

但偏偏使用 ie11 時,就是會莫名其妙產生中文檔名變成亂碼的問題,
此時必須再加上下列設定,才能將亂碼恢復成正常的顯示
```php <?php header("Content-Disposition: attachment; filename=" . $fileName . ".xls; filename*=UTF-8''" . urlencode($fileName) . ".xls"); ?> ```

【Python】Codility in Python : Lesson 2 - Arrays【CyclicRotation】

Arrays 練習題的第二題題目是【OddOccurrencesInArray】
Task : Rotate an array to the right by a given number of steps.

方法一:使用 pop & insert 的簡易方式
```python def solutionByPop(A, K): if(K > 0 and len(A) > 1): for i in range(0, K): A.insert(0, A.pop()) return A ```

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

【Python】Codility in Python : Lesson 2 - Arrays【OddOccurrencesInArray】

Arrays 練習題的第一題題目是【OddOccurrencesInArray】
Task : Find value that occurs in odd number of elements.

方法一:使用 XOR 的簡易方式
```python def solutionByXOR(A): result = 0 for number in (A): result ^= number return result ```

方法二:使用 groupby 的簡易方式
```python def solutionByGroup(A): for k, v in groupby(sorted(A)): if(len(list(v)) == 1): return k return 0 ```

方法三:使用排序後兩兩比較的檢查方式
```python def solutionByLoop(A): if(len(A) == 1): return A[0] else: sortedList = sorted(A) count = len(sortedList) - 1 for i in range(0, count, 2): if sortedList[i] != sortedList[i+1]: return sortedList[i] return sortedList[count] ```

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

【Python】Codility in Python : Lesson 1 - Iterations【BinaryGap】

Iterations 練習題的第一題題目是【BinaryGap】
Task : Find longest sequence of zeros in binary representation of an integer.

方法一:使用 Regular Expression 的簡易方法
```python def solutionByRegex(N): return len(max(re.findall('(0*)1', bin(N)[2:]))) if N > 0 else 0 ```

方法二:使用 strip 和 split 的簡易方法
```python def solutionBySplit(N): return len(max(bin(N)[2:].strip('0').strip('1').split('1'))) ```

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